From d337716b23d62216ff560f966a12d6accbbfd64e Mon Sep 17 00:00:00 2001 From: Matt Rickard Date: Fri, 12 Aug 2016 15:19:24 -0700 Subject: [PATCH] Making minikube start flags configurable by Viper config This allows most flags for the minikube start command to be configurable by viper as well. If a flag is present, it will take precedence over the value supplied in viper. Viper doesn't handle string slices correctly (see https://github.com/spf13/viper/issues/200) so the string slices that we pass in as flags such as docker-env and insecure-registry are still only handled by flags Delete custom flag for human readable disk size Since the value can now be passed in multiple ways (environment variable, flag, or config file), moving the conversion out of the flag and straight into cmd/minikube/cmd/start.go This changes the helptext for gendocs --- cmd/minikube/cmd/flags.go | 40 ------------------------- cmd/minikube/cmd/start.go | 62 +++++++++++++++++++++++---------------- docs/minikube_start.md | 2 +- 3 files changed, 38 insertions(+), 66 deletions(-) delete mode 100644 cmd/minikube/cmd/flags.go diff --git a/cmd/minikube/cmd/flags.go b/cmd/minikube/cmd/flags.go deleted file mode 100644 index 332bdb0c7b6a..000000000000 --- a/cmd/minikube/cmd/flags.go +++ /dev/null @@ -1,40 +0,0 @@ -/* -Copyright 2016 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package cmd - -import "github.com/docker/go-units" - -// unitValue represents an int64 flag specified with units as a string. -type unitValue int64 - -func newUnitValue(v int64) *unitValue { - return (*unitValue)(&v) -} - -func (u *unitValue) Set(s string) error { - v, err := units.FromHumanSize(s) - *u = unitValue(v) - return err -} - -func (u *unitValue) Type() string { - return "unit" -} - -func (u *unitValue) String() string { - return units.HumanSize(float64(*u)) -} diff --git a/cmd/minikube/cmd/start.go b/cmd/minikube/cmd/start.go index 76a6967ced82..fbce14c2bae8 100644 --- a/cmd/minikube/cmd/start.go +++ b/cmd/minikube/cmd/start.go @@ -27,6 +27,7 @@ import ( "github.com/docker/machine/libmachine/host" "github.com/golang/glog" "github.com/spf13/cobra" + "github.com/spf13/viper" cfg "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api" "k8s.io/minikube/pkg/minikube/cluster" "k8s.io/minikube/pkg/minikube/constants" @@ -34,17 +35,20 @@ import ( "k8s.io/minikube/pkg/util" ) +const ( + isoURL = "iso-url" + memory = "memory" + cpus = "cpus" + humanReadableDiskSize = "disk-size" + vmDriver = "vm-driver" + kubernetesVersion = "kubernetes-version" + hostOnlyCIDR = "host-only-cidr" +) + var ( - minikubeISO string - memory int - cpus int - disk = newUnitValue(20 * units.GB) - vmDriver string - dockerEnv []string - insecureRegistry []string - registryMirror []string - kubernetesVersion string - hostOnlyCIDR string + registryMirror []string + dockerEnv []string + insecureRegistry []string ) // startCmd represents the start command @@ -62,18 +66,18 @@ func runStart(cmd *cobra.Command, args []string) { defer api.Close() config := cluster.MachineConfig{ - MinikubeISO: minikubeISO, - Memory: memory, - CPUs: cpus, - DiskSize: int(*disk / units.MB), - VMDriver: vmDriver, + MinikubeISO: viper.GetString(isoURL), + Memory: viper.GetInt(memory), + CPUs: viper.GetInt(cpus), + DiskSize: calculateDiskSizeInMB(viper.GetString(humanReadableDiskSize)), + VMDriver: viper.GetString(vmDriver), DockerEnv: dockerEnv, InsecureRegistry: insecureRegistry, RegistryMirror: registryMirror, - HostOnlyCIDR: hostOnlyCIDR, + HostOnlyCIDR: viper.GetString(hostOnlyCIDR), } kubernetesConfig := cluster.KubernetesConfig{ - KubernetesVersion: kubernetesVersion, + KubernetesVersion: viper.GetString(kubernetesVersion), } var host *host.Host @@ -124,6 +128,14 @@ func runStart(cmd *cobra.Command, args []string) { fmt.Println("Kubectl is now configured to use the cluster.") } +func calculateDiskSizeInMB(humanReadableDiskSize string) int { + diskSize, err := units.FromHumanSize(humanReadableDiskSize) + if err != nil { + glog.Errorf("Invalid disk size: %s", err) + } + return int(diskSize / units.MB) +} + // setupKubeconfig reads config from disk, adds the minikube settings, and writes it back. // activeContext is true when minikube is the CurrentContext // If no CurrentContext is set, the given name will be used. @@ -167,16 +179,16 @@ func setupKubeconfig(name, server, certAuth, cliCert, cliKey string) error { } func init() { - startCmd.Flags().StringVar(&minikubeISO, "iso-url", constants.DefaultIsoUrl, "Location of the minikube iso") - startCmd.Flags().StringVar(&vmDriver, "vm-driver", constants.DefaultVMDriver, fmt.Sprintf("VM driver is one of: %v", constants.SupportedVMDrivers)) - startCmd.Flags().IntVar(&memory, "memory", constants.DefaultMemory, "Amount of RAM allocated to the minikube VM") - startCmd.Flags().IntVar(&cpus, "cpus", constants.DefaultCPUS, "Number of CPUs allocated to the minikube VM") - diskFlag := startCmd.Flags().VarPF(disk, "disk-size", "", "Disk size allocated to the minikube VM (format: [], where unit = b, k, m or g)") - diskFlag.DefValue = constants.DefaultDiskSize - startCmd.Flags().StringVar(&hostOnlyCIDR, "host-only-cidr", "192.168.99.1/24", "The CIDR to be used for the minikube VM (only supported with Virtualbox driver)") + startCmd.Flags().String(isoURL, constants.DefaultIsoUrl, "Location of the minikube iso") + startCmd.Flags().String(vmDriver, constants.DefaultVMDriver, fmt.Sprintf("VM driver is one of: %v", constants.SupportedVMDrivers)) + startCmd.Flags().Int(memory, constants.DefaultMemory, "Amount of RAM allocated to the minikube VM") + startCmd.Flags().Int(cpus, constants.DefaultCPUS, "Number of CPUs allocated to the minikube VM") + startCmd.Flags().String(humanReadableDiskSize, constants.DefaultDiskSize, "Disk size allocated to the minikube VM (format: [], where unit = b, k, m or g)") + startCmd.Flags().String(hostOnlyCIDR, "192.168.99.1/24", "The CIDR to be used for the minikube VM (only supported with Virtualbox driver)") startCmd.Flags().StringSliceVar(&dockerEnv, "docker-env", nil, "Environment variables to pass to the Docker daemon. (format: key=value)") startCmd.Flags().StringSliceVar(&insecureRegistry, "insecure-registry", nil, "Insecure Docker registries to pass to the Docker daemon") startCmd.Flags().StringSliceVar(®istryMirror, "registry-mirror", nil, "Registry mirrors to pass to the Docker daemon") - startCmd.Flags().StringVar(&kubernetesVersion, "kubernetes-version", constants.DefaultKubernetesVersion, "The kubernetes version that the minikube VM will (ex: v1.2.3) \n OR a URI which contains a localkube binary (ex: https://storage.googleapis.com/minikube/k8sReleases/v1.3.0/localkube-linux-amd64)") + startCmd.Flags().String(kubernetesVersion, constants.DefaultKubernetesVersion, "The kubernetes version that the minikube VM will (ex: v1.2.3) \n OR a URI which contains a localkube binary (ex: https://storage.googleapis.com/minikube/k8sReleases/v1.3.0/localkube-linux-amd64)") + viper.BindPFlags(startCmd.Flags()) RootCmd.AddCommand(startCmd) } diff --git a/docs/minikube_start.md b/docs/minikube_start.md index 465749c0ebfa..d2dc8b58ac1d 100644 --- a/docs/minikube_start.md +++ b/docs/minikube_start.md @@ -16,7 +16,7 @@ minikube start ``` --cpus int Number of CPUs allocated to the minikube VM (default 1) - --disk-size value Disk size allocated to the minikube VM (format: [], where unit = b, k, m or g) (default 20g) + --disk-size string Disk size allocated to the minikube VM (format: [], where unit = b, k, m or g) (default "20g") --docker-env value Environment variables to pass to the Docker daemon. (format: key=value) (default []) --host-only-cidr string The CIDR to be used for the minikube VM (only supported with Virtualbox driver) (default "192.168.99.1/24") --insecure-registry value Insecure Docker registries to pass to the Docker daemon (default [])