diff --git a/cmd/ocm/create/cluster/cmd.go b/cmd/ocm/create/cluster/cmd.go index 1ac76183..5250278d 100644 --- a/cmd/ocm/create/cluster/cmd.go +++ b/cmd/ocm/create/cluster/cmd.go @@ -371,6 +371,34 @@ func getFlavourOptions(connection *sdk.Connection) ([]arguments.Option, error) { return options, nil } +func GetDefaultClusterFlavors(connection *sdk.Connection, flavour string) (dMachinecidr *net.IPNet, dPodcidr *net.IPNet, + dServicecidr *net.IPNet, dhostPrefix int) { + flavourGetResponse, err := connection.ClustersMgmt().V1().Flavours().Flavour(flavour).Get().Send() + if err != nil { + flavourGetResponse, _ = connection.ClustersMgmt().V1().Flavours().Flavour("osd-4").Get().Send() + } + + network, ok := flavourGetResponse.Body().GetNetwork() + if !ok { + return nil, nil, nil, 0 + } + _, dMachinecidr, err = net.ParseCIDR(network.MachineCIDR()) + if err != nil { + dMachinecidr = nil + } + _, dPodcidr, err = net.ParseCIDR(network.PodCIDR()) + if err != nil { + dPodcidr = nil + } + _, dServicecidr, err = net.ParseCIDR(network.ServiceCIDR()) + if err != nil { + dServicecidr = nil + } + dhostPrefix, _ = network.GetHostPrefix() + + return dMachinecidr, dPodcidr, dServicecidr, dhostPrefix +} + func getVersionOptions(connection *sdk.Connection) ([]arguments.Option, error) { options, _, err := getVersionOptionsWithDefault(connection, "") return options, err @@ -595,6 +623,11 @@ func preRun(cmd *cobra.Command, argv []string) error { return err } + if args.interactive { + machineCIDR, podCIDR, serviceCIDR, hostPrefix := GetDefaultClusterFlavors(connection, args.flavour) + args.machineCIDR, args.podCIDR, args.serviceCIDR, args.hostPrefix = *machineCIDR, *podCIDR, *serviceCIDR, hostPrefix + } + err = promptNetwork(fs) if err != nil { return err diff --git a/pkg/arguments/interactive.go b/pkg/arguments/interactive.go index 198ca22d..ace983ce 100644 --- a/pkg/arguments/interactive.go +++ b/pkg/arguments/interactive.go @@ -295,12 +295,15 @@ func PromptIPNet(fs *pflag.FlagSet, flagName string) error { if flag.Changed { return nil } - + // We set the default value here (if nil) so that shown on the console when the user does not provide any input + // (awkward because https://github.com/golang/go/issues/39516). + if flag.DefValue == "" { + flag.DefValue = flag.Value.String() + } prompt := &survey.Input{ Message: getQuestion(flag), Help: flag.Usage, - // TODO respect flag default, if set - // (awkward because https://github.com/golang/go/issues/39516). + Default: flag.DefValue, } var response string // Set() flag as side effect of validation => prompts again if invalid.