From dbfd16f21e35a4d3ec7287d34dd97287a11314ea Mon Sep 17 00:00:00 2001 From: Medya Gh Date: Tue, 14 Jul 2020 15:02:54 -0700 Subject: [PATCH] add helper fucns for drivers --- cmd/minikube/cmd/start.go | 84 +++++++++++++++++++-------------- cmd/minikube/cmd/start_flags.go | 4 +- pkg/minikube/driver/driver.go | 28 +++++++++++ pkg/minikube/node/advice.go | 5 +- 4 files changed, 82 insertions(+), 39 deletions(-) diff --git a/cmd/minikube/cmd/start.go b/cmd/minikube/cmd/start.go index 535aa731de..b91fa376ac 100644 --- a/cmd/minikube/cmd/start.go +++ b/cmd/minikube/cmd/start.go @@ -763,35 +763,6 @@ func memoryLimits(drvName string) (int, int, error) { return sysLimit, containerLimit, nil } -func myabeAdviceDockerResources(containerLimit int, sysLimit int, cpus int, drvName string) { - if drvName == oci.Docker && runtime.GOOS != "linux" { - if containerLimit < 1991 { - out.T(out.Conflict, `Your Docker Desktop has only {{.container_limit}} memory. Increase memory to at least 2.5 GB or more: - - Docker Icon > Settings > Resources > Memory - - `, out.V{"container_limit": containerLimit}) - // for users with more than 8 GB advice 3 GB - } else if containerLimit < 2997 && sysLimit > 8000 { - out.T(out.Tip, `Your system has {{.system_limit}}mb memory but Docker has only {{.container_limit}}mb. For a better performance increase to at least 3 GB. - - Docker Icon > Settings > Resources > Memory - -`, out.V{"container_limit": containerLimit, "system_limit": sysLimit}) - } - if cpus < 2 { - out.T(out.Conflict, `Your Docker Desktop has less than 2 CPUs. Increase CPUs for Docker Desktop. - - Docker icon > Settings > Resources > CPUs - -`, out.V{"container_limit": containerLimit}) - out.T(out.Documentation, "https://docs.docker.com/config/containers/resource_constraints/") - exit.UsageT("Ensure your {{.driver_name}} system has enough CPUs. The minimum allowed is 2 CPUs.", out.V{"driver_name": viper.GetString("driver")}) - - } - } -} - // suggestMemoryAllocation calculates the default memory footprint in MB func suggestMemoryAllocation(sysLimit int, containerLimit int, nodes int) int { if mem := viper.GetInt(memory); mem != 0 { @@ -831,11 +802,13 @@ func suggestMemoryAllocation(sysLimit int, containerLimit int, nodes int) int { } // validateMemorySize validates the memory size matches the minimum recommended -func validateMemorySize() { - req, err := util.CalculateSizeInMB(viper.GetString(memory)) +func validateMemorySize(req int, drvName string) { + + sysLimit, containerLimit, err := memoryLimits(drvName) if err != nil { - exit.WithCodeT(exit.Config, "Unable to parse memory '{{.memory}}': {{.error}}", out.V{"memory": viper.GetString(memory), "error": err}) + glog.Warningf("Unable to query memory limits: %v", err) } + if req < minUsableMem && !viper.GetBool(force) { exit.WithCodeT(exit.Config, "Requested memory allocation {{.requested}}MB is less than the usable minimum of {{.minimum}}MB", out.V{"requested": req, "mininum": minUsableMem}) @@ -844,12 +817,28 @@ func validateMemorySize() { out.T(out.Notice, "Requested memory allocation ({{.requested}}MB) is less than the recommended minimum {{.recommended}}MB. Kubernetes may crash unexpectedly.", out.V{"requested": req, "recommended": minRecommendedMem}) } + + if driver.IsDockerDesktop(drvName) { + if containerLimit < 1991 { + out.T(out.Tip, `Increase Docker for Desktop memory to at least 2.5 GB or more: + + Docker for Desktop > Settings > Resources > Memory + +`) + } else if containerLimit < 2997 && sysLimit > 8000 { // for users with more than 8 GB advice 3 GB + out.T(out.Tip, `Your system has {{.system_limit}}MB memory but Docker has only {{.container_limit}}MB. For a better performance increase to at least 3 GB. + + Docker for Desktop > Settings > Resources > Memory + +`, out.V{"container_limit": containerLimit, "system_limit": sysLimit}) + } + } } // validateCPUCount validates the cpu count matches the minimum recommended -func validateCPUCount(local bool) { +func validateCPUCount(drvName string) { var cpuCount int - if local { + if driver.BareMetal(drvName) { // Uses the gopsutil cpu package to count the number of physical cpu cores ci, err := cpu.Counts(false) if err != nil { @@ -863,6 +852,25 @@ func validateCPUCount(local bool) { 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}) } + + if driver.IsKIC((drvName)) { + si, err := cachedKicSystemInfo(drvName) + if err != nil { + out.WarningT("Failed to verify '{{.driver_name}} info', ensure your {{.driver_name}} is running healthy.", out.V{"driver_namee": drvName}) + } + if si.CPUs < 2 { + if drvName == oci.Docker { + out.T(out.Conflict, `Your Docker Desktop has less than 2 CPUs. Increase CPUs for Docker Desktop. + + Docker icon > Settings > Resources > CPUs + + `) + } + out.T(out.Documentation, "https://docs.docker.com/config/containers/resource_constraints/") + exit.UsageT("Ensure your {{.driver_name}} system has enough CPUs. The minimum allowed is 2 CPUs.", out.V{"driver_name": driver.NameForHumans(viper.GetString("driver"))}) + + } + } } // validateFlags validates the supplied flags against known bad combinations @@ -879,14 +887,18 @@ func validateFlags(cmd *cobra.Command, drvName string) { } if cmd.Flags().Changed(cpus) { - validateCPUCount(driver.BareMetal(drvName)) if !driver.HasResourceLimits(drvName) { out.WarningT("The '{{.name}}' driver does not respect the --cpus flag", out.V{"name": drvName}) } } + validateCPUCount(drvName) if cmd.Flags().Changed(memory) { - validateMemorySize() + req, err := util.CalculateSizeInMB(viper.GetString(memory)) + if err != nil { + exit.WithCodeT(exit.Config, "Unable to parse memory '{{.memory}}': {{.error}}", out.V{"memory": viper.GetString(memory), "error": err}) + } + validateMemorySize(req, drvName) if !driver.HasResourceLimits(drvName) { out.WarningT("The '{{.name}}' driver does not respect the --memory flag", out.V{"name": drvName}) } diff --git a/cmd/minikube/cmd/start_flags.go b/cmd/minikube/cmd/start_flags.go index 5c1c5e55cd..0fc72c9095 100644 --- a/cmd/minikube/cmd/start_flags.go +++ b/cmd/minikube/cmd/start_flags.go @@ -233,13 +233,15 @@ func generateClusterConfig(cmd *cobra.Command, existing *config.ClusterConfig, k exit.WithCodeT(exit.Config, "Generate unable to parse memory '{{.memory}}': {{.error}}", out.V{"memory": viper.GetString(memory), "error": err}) } if driver.IsKIC(drvName) && mem > containerLimit { - exit.UsageT("{{.driver_name}} service has only {{.container_limit}}mb memory but you specified {{.specified_memory}}mb", out.V{"container_limit": containerLimit, "specified_memory": mem, "driver_name": drvName}) + exit.UsageT("{{.driver_name}} has only {{.container_limit}}MB memory but you specified {{.specified_memory}}mb", out.V{"container_limit": containerLimit, "specified_memory": mem, "driver_name": driver.NameForHumans(drvName)}) } } else { glog.Infof("Using suggested %dMB memory alloc based on sys=%dMB, container=%dMB", mem, sysLimit, containerLimit) } + validateMemorySize(mem, drvName) + diskSize, err := pkgutil.CalculateSizeInMB(viper.GetString(humanReadableDiskSize)) if err != nil { exit.WithCodeT(exit.Config, "Generate unable to parse disk size '{{.diskSize}}': {{.error}}", out.V{"diskSize": viper.GetString(humanReadableDiskSize), "error": err}) diff --git a/pkg/minikube/driver/driver.go b/pkg/minikube/driver/driver.go index 9809727180..18d5e4c09a 100644 --- a/pkg/minikube/driver/driver.go +++ b/pkg/minikube/driver/driver.go @@ -24,6 +24,7 @@ import ( "strings" "github.com/golang/glog" + "k8s.io/minikube/pkg/drivers/kic/oci" "k8s.io/minikube/pkg/minikube/config" "k8s.io/minikube/pkg/minikube/registry" ) @@ -105,6 +106,17 @@ func IsKIC(name string) bool { return name == Docker || name == Podman } +// IsKIC checks if the driver is a Docker for Desktop (Docker on windows or MacOs) +// for linux and exotic archs, this will be false +func IsDockerDesktop(name string) bool { + if IsKIC(name) { + if runtime.GOOS == "darwin" || runtime.GOOS == "windows" { + return true + } + } + return false +} + // IsMock checks if the driver is a mock func IsMock(name string) bool { return name == Mock @@ -156,6 +168,22 @@ func NeedsShutdown(name string) bool { return false } +// NameForHumans will return the human-known and formatted name for the driver +func NameForHumans(name string) string { + switch name { + case oci.Docker: + if IsDockerDesktop(name) { + return "Docker for Desktop" + } + return "Docker Service" + + case oci.Podman: + return "Podman Service" + default: + return strings.Title(name) + } +} + // FlagHints are hints for what default options should be used for this driver type FlagHints struct { ExtraOptions []string diff --git a/pkg/minikube/node/advice.go b/pkg/minikube/node/advice.go index b7e84e3b8f..cc43c85fa4 100644 --- a/pkg/minikube/node/advice.go +++ b/pkg/minikube/node/advice.go @@ -23,6 +23,7 @@ import ( "github.com/spf13/viper" "k8s.io/minikube/pkg/drivers/kic/oci" "k8s.io/minikube/pkg/minikube/bootstrapper/kubeadm" + "k8s.io/minikube/pkg/minikube/driver" "k8s.io/minikube/pkg/minikube/exit" "k8s.io/minikube/pkg/minikube/out" ) @@ -45,7 +46,7 @@ func MaybeExitWithAdvice(err error) { if errors.Is(err, oci.ErrCPUCountLimit) { out.ErrLn("") - out.ErrT(out.Conflict, "{{.name}} doesn't have enough CPUs. ", out.V{"name": viper.GetString("driver")}) + out.ErrT(out.Conflict, "{{.name}} doesn't have enough CPUs. ", out.V{"name": driver.NameForHumans(viper.GetString("driver"))}) if runtime.GOOS != "linux" && viper.GetString("driver") == "docker" { out.T(out.Warning, "Please consider changing your Docker Desktop's resources.") out.T(out.Documentation, "https://docs.docker.com/config/containers/resource_constraints/") @@ -54,7 +55,7 @@ func MaybeExitWithAdvice(err error) { if cpuCount == 2 { out.T(out.Tip, "Please ensure your system has {{.cpu_counts}} CPU cores.", out.V{"cpu_counts": viper.GetInt(cpus)}) } else { - out.T(out.Tip, "Please ensure your {{.driver_name}} system has access to {{.cpu_counts}} CPU cores or reduce the number of the specified CPUs", out.V{"driver_name": viper.GetString("driver"), "cpu_counts": viper.GetInt(cpus)}) + out.T(out.Tip, "Please ensure your {{.driver_name}} system has access to {{.cpu_counts}} CPU cores or reduce the number of the specified CPUs", out.V{"driver_name": driver.NameForHumans(viper.GetString("driver")), "cpu_counts": viper.GetInt(cpus)}) } } exit.UsageT("Ensure your {{.driver_name}} system has enough CPUs. The minimum allowed is 2 CPUs.", out.V{"driver_name": viper.GetString("driver")})