Improve reportcard by refactoring and commenting

golint: 99%
	pkg/minikube/localpath/localpath.go
		Line 64: warning: comment on exported function SanitizeCacheDir should be of the form "SanitizeCacheDir ..." (golint)
gocyclo: 99%
	pkg/minikube/cluster/cluster.go
		Line 422: warning: cyclomatic complexity 16 of function createHost() is high (> 15) (gocyclo)
	cmd/minikube/cmd/start.go
		Line 276: warning: cyclomatic complexity 18 of function runStart() is high (> 15) (gocyclo)
		Line 742: warning: cyclomatic complexity 18 of function validateFlags() is high (> 15) (gocyclo)
pull/6218/head
Anders F Björklund 2020-01-06 13:38:33 +01:00
parent 06b1cf393a
commit ea7a2d7e33
3 changed files with 55 additions and 30 deletions

View File

@ -313,11 +313,7 @@ func runStart(cmd *cobra.Command, args []string) {
exit.WithError("Failed to generate config", err)
}
if !driver.BareMetal(driverName) && !driver.IsKIC(driverName) {
if err := cluster.CacheISO(config); err != nil {
exit.WithError("Failed to cache ISO", err)
}
}
cacheISO(&config, driverName)
if viper.GetBool(nativeSSH) {
ssh.SetDefaultClient(ssh.Native)
@ -389,6 +385,14 @@ func updateDriver(driverName string) {
}
}
func cacheISO(config *cfg.MachineConfig, driverName string) {
if !driver.BareMetal(driverName) && !driver.IsKIC(driverName) {
if err := cluster.CacheISO(*config); err != nil {
exit.WithError("Failed to cache ISO", err)
}
}
}
func enableAddons() {
for _, a := range addonList {
err := cmdcfg.Set(a, "true")
@ -704,7 +708,7 @@ func minikubeCmd() string {
return "minikube"
}
// validerUser validates minikube is run by the recommended user (privileged or regular)
// validateUser validates minikube is run by the recommended user (privileged or regular)
func validateUser(drvName string) {
u, err := user.Current()
if err != nil {
@ -738,13 +742,16 @@ func validateUser(drvName string) {
}
}
// validateFlags validates the supplied flags against known bad combinations
func validateFlags(cmd *cobra.Command, drvName string) {
// validateDiskSize validates the disk size matches the minimum recommended
func validateDiskSize() {
diskSizeMB := pkgutil.CalculateSizeInMB(viper.GetString(humanReadableDiskSize))
if diskSizeMB < pkgutil.CalculateSizeInMB(minimumDiskSize) && !viper.GetBool(force) {
exit.WithCodeT(exit.Config, "Requested disk size {{.requested_size}} is less than minimum of {{.minimum_size}}", out.V{"requested_size": diskSizeMB, "minimum_size": pkgutil.CalculateSizeInMB(minimumDiskSize)})
}
}
// validateMemorySize validates the memory size matches the minimum recommended
func validateMemorySize() {
memorySizeMB := pkgutil.CalculateSizeInMB(viper.GetString(memory))
if memorySizeMB < pkgutil.CalculateSizeInMB(minimumMemorySize) && !viper.GetBool(force) {
exit.UsageT("Requested memory allocation {{.requested_size}} is less than the minimum allowed of {{.minimum_size}}", out.V{"requested_size": memorySizeMB, "minimum_size": pkgutil.CalculateSizeInMB(minimumMemorySize)})
@ -753,8 +760,32 @@ func validateFlags(cmd *cobra.Command, drvName string) {
out.T(out.Notice, "Requested memory allocation ({{.memory}}MB) is less than the default memory allocation of {{.default_memorysize}}MB. Beware that minikube might not work correctly or crash unexpectedly.",
out.V{"memory": memorySizeMB, "default_memorysize": pkgutil.CalculateSizeInMB(defaultMemorySize)})
}
}
// validateCPUCount validates the cpu count matches the minimum recommended
func validateCPUCount(local bool) {
var cpuCount int
if local {
// Uses the gopsutil cpu package to count the number of physical cpu cores
ci, err := cpu.Counts(false)
if err != nil {
glog.Warningf("Unable to get CPU info: %v", err)
} else {
cpuCount = ci
}
} else {
cpuCount = viper.GetInt(cpus)
}
if cpuCount < minimumCPUS && !viper.GetBool(force) {
exit.UsageT("Requested cpu count {{.requested_cpus}} is less than the minimum allowed of {{.minimum_cpus}}", out.V{"requested_cpus": cpuCount, "minimum_cpus": minimumCPUS})
}
}
// validateFlags validates the supplied flags against known bad combinations
func validateFlags(cmd *cobra.Command, drvName string) {
validateDiskSize()
validateMemorySize()
if driver.BareMetal(drvName) {
if viper.GetString(cfg.MachineProfile) != constants.DefaultMachineName {
exit.WithCodeT(exit.Config, "The 'none' driver does not support multiple profiles: https://minikube.sigs.k8s.io/docs/reference/drivers/none/")
@ -771,20 +802,9 @@ func validateFlags(cmd *cobra.Command, drvName string) {
if runtime != "docker" {
out.WarningT("Using the '{{.runtime}}' runtime with the 'none' driver is an untested configuration!", out.V{"runtime": runtime})
}
}
// Uses the gopsutil cpu package to count the number of physical cpu cores
ci, err := cpu.Counts(false)
if err != nil {
glog.Warningf("Unable to get CPU info: %v", err)
} else {
cpuCount = ci
}
} else {
cpuCount = viper.GetInt(cpus)
}
if cpuCount < minimumCPUS && !viper.GetBool(force) {
exit.UsageT("Requested cpu count {{.requested_cpus}} is less than the minimum allowed of {{.minimum_cpus}}", out.V{"requested_cpus": cpuCount, "minimum_cpus": minimumCPUS})
}
validateCPUCount(driver.BareMetal(drvName))
// check that kubeadm extra args contain only whitelisted parameters
for param := range extraOptions.AsMap().Get(bsutil.Kubeadm) {

View File

@ -419,13 +419,8 @@ func showRemoteOsRelease(driver drivers.Driver) {
glog.Infof("Provisioned with %s", osReleaseInfo.PrettyName)
}
func createHost(api libmachine.API, config cfg.MachineConfig) (*host.Host, error) {
if config.VMDriver == driver.VMwareFusion && viper.GetBool(cfg.ShowDriverDeprecationNotification) {
out.WarningT(`The vmwarefusion driver is deprecated and support for it will be removed in a future release.
Please consider switching to the new vmware unified driver, which is intended to replace the vmwarefusion driver.
See https://minikube.sigs.k8s.io/docs/reference/drivers/vmware/ for more information.
To disable this message, run [minikube config set ShowDriverDeprecationNotification false]`)
}
// showHostInfo shows host information
func showHostInfo(config cfg.MachineConfig) {
if driver.BareMetal(config.VMDriver) {
info, err := getHostInfo()
if err == nil {
@ -439,6 +434,16 @@ func createHost(api libmachine.API, config cfg.MachineConfig) (*host.Host, error
} else {
out.T(out.StartingVM, "Creating {{.driver_name}} VM (CPUs={{.number_of_cpus}}, Memory={{.memory_size}}MB, Disk={{.disk_size}}MB) ...", out.V{"driver_name": config.VMDriver, "number_of_cpus": config.CPUs, "memory_size": config.Memory, "disk_size": config.DiskSize})
}
}
func createHost(api libmachine.API, config cfg.MachineConfig) (*host.Host, error) {
if config.VMDriver == driver.VMwareFusion && viper.GetBool(cfg.ShowDriverDeprecationNotification) {
out.WarningT(`The vmwarefusion driver is deprecated and support for it will be removed in a future release.
Please consider switching to the new vmware unified driver, which is intended to replace the vmwarefusion driver.
See https://minikube.sigs.k8s.io/docs/reference/drivers/vmware/ for more information.
To disable this message, run [minikube config set ShowDriverDeprecationNotification false]`)
}
showHostInfo(config)
def := registry.Driver(config.VMDriver)
if def.Empty() {
return nil, fmt.Errorf("unsupported/missing driver: %s", config.VMDriver)

View File

@ -61,8 +61,7 @@ func MachinePath(machine string, miniHome ...string) string {
return filepath.Join(miniPath, "machines", machine)
}
// SanitizeCacheDir
// # ParseReference cannot have a : in the directory path
// SanitizeCacheDir returns a path without special characters
func SanitizeCacheDir(image string) string {
if runtime.GOOS == "windows" && hasWindowsDriveLetter(image) {
// not sanitize Windows drive letter.
@ -70,6 +69,7 @@ func SanitizeCacheDir(image string) string {
glog.Infof("windows sanitize: %s -> %s", image, s)
return s
}
// ParseReference cannot have a : in the directory path
return strings.Replace(image, ":", "_", -1)
}