Skip to content

Commit e8b3db9

Browse files
authored
Merge pull request #11692 from spowelljr/addNoLimit
Add ability to pass 'max' value to memory and cpus flags
2 parents 02abe78 + 5559a84 commit e8b3db9

File tree

15 files changed

+116
-64
lines changed

15 files changed

+116
-64
lines changed

cmd/minikube/cmd/config/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ var settings = []Setting{
7676
{
7777
name: "cpus",
7878
set: SetInt,
79-
validations: []setFn{IsPositive},
79+
validations: []setFn{IsValidCPUs},
8080
callbacks: []setFn{RequiresRestartMsg},
8181
},
8282
{

cmd/minikube/cmd/config/validations.go

+12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"strings"
2626

2727
units "github.com/docker/go-units"
28+
"k8s.io/minikube/pkg/minikube/constants"
2829
"k8s.io/minikube/pkg/minikube/cruntime"
2930
"k8s.io/minikube/pkg/minikube/driver"
3031
"k8s.io/minikube/pkg/minikube/out"
@@ -53,8 +54,19 @@ func IsValidDiskSize(name string, disksize string) error {
5354
return nil
5455
}
5556

57+
// IsValidCPUs checks if a string is a valid number of CPUs
58+
func IsValidCPUs(name string, cpus string) error {
59+
if cpus == constants.MaxResources {
60+
return nil
61+
}
62+
return IsPositive(name, cpus)
63+
}
64+
5665
// IsValidMemory checks if a string is a valid memory size
5766
func IsValidMemory(name string, memsize string) error {
67+
if memsize == constants.MaxResources {
68+
return nil
69+
}
5870
_, err := units.FromHumanSize(memsize)
5971
if err != nil {
6072
return fmt.Errorf("invalid memory size: %v", err)

cmd/minikube/cmd/start.go

+43-31
Original file line numberDiff line numberDiff line change
@@ -1030,56 +1030,49 @@ func validateRequestedMemorySize(req int, drvName string) {
10301030

10311031
// validateCPUCount validates the cpu count matches the minimum recommended & not exceeding the available cpu count
10321032
func validateCPUCount(drvName string) {
1033-
var cpuCount int
1034-
if driver.BareMetal(drvName) {
1033+
var availableCPUs int
10351034

1036-
// Uses the gopsutil cpu package to count the number of logical cpu cores
1037-
ci, err := cpu.Counts(true)
1035+
cpuCount := getCPUCount(drvName)
1036+
isKIC := driver.IsKIC(drvName)
1037+
1038+
if isKIC {
1039+
si, err := oci.CachedDaemonInfo(drvName)
10381040
if err != nil {
1039-
klog.Warningf("Unable to get CPU info: %v", err)
1040-
} else {
1041-
cpuCount = ci
1041+
si, err = oci.DaemonInfo(drvName)
1042+
if err != nil {
1043+
exit.Message(reason.Usage, "Ensure your {{.driver_name}} is running and is healthy.", out.V{"driver_name": driver.FullName(drvName)})
1044+
}
10421045
}
1046+
availableCPUs = si.CPUs
10431047
} else {
1044-
cpuCount = viper.GetInt(cpus)
1048+
ci, err := cpu.Counts(true)
1049+
if err != nil {
1050+
exit.Message(reason.Usage, "Unable to get CPU info: {{.err}}", out.V{"err": err})
1051+
}
1052+
availableCPUs = ci
10451053
}
10461054

10471055
if cpuCount < minimumCPUS {
10481056
exitIfNotForced(reason.RsrcInsufficientCores, "Requested cpu count {{.requested_cpus}} is less than the minimum allowed of {{.minimum_cpus}}", out.V{"requested_cpus": cpuCount, "minimum_cpus": minimumCPUS})
10491057
}
10501058

1051-
if !driver.IsKIC((drvName)) {
1052-
return
1053-
}
1054-
1055-
si, err := oci.CachedDaemonInfo(drvName)
1056-
if err != nil {
1057-
out.Styled(style.Confused, "Failed to verify '{{.driver_name}} info' will try again ...", out.V{"driver_name": drvName})
1058-
si, err = oci.DaemonInfo(drvName)
1059-
if err != nil {
1060-
exit.Message(reason.Usage, "Ensure your {{.driver_name}} is running and is healthy.", out.V{"driver_name": driver.FullName(drvName)})
1061-
}
1062-
1063-
}
1064-
1065-
if si.CPUs < cpuCount {
1066-
1059+
if availableCPUs < cpuCount {
10671060
if driver.IsDockerDesktop(drvName) {
10681061
out.Styled(style.Empty, `- Ensure your {{.driver_name}} daemon has access to enough CPU/memory resources.`, out.V{"driver_name": drvName})
10691062
if runtime.GOOS == "darwin" {
1070-
out.Styled(style.Empty, `- Docs https://docs.docker.com/docker-for-mac/#resources`, out.V{"driver_name": drvName})
1063+
out.Styled(style.Empty, `- Docs https://docs.docker.com/docker-for-mac/#resources`)
10711064
}
10721065
if runtime.GOOS == "windows" {
10731066
out.String("\n\t")
1074-
out.Styled(style.Empty, `- Docs https://docs.docker.com/docker-for-windows/#resources`, out.V{"driver_name": drvName})
1067+
out.Styled(style.Empty, `- Docs https://docs.docker.com/docker-for-windows/#resources`)
10751068
}
10761069
}
10771070

1078-
exitIfNotForced(reason.RsrcInsufficientCores, "Requested cpu count {{.requested_cpus}} is greater than the available cpus of {{.avail_cpus}}", out.V{"requested_cpus": cpuCount, "avail_cpus": si.CPUs})
1071+
exitIfNotForced(reason.RsrcInsufficientCores, "Requested cpu count {{.requested_cpus}} is greater than the available cpus of {{.avail_cpus}}", out.V{"requested_cpus": cpuCount, "avail_cpus": availableCPUs})
10791072
}
10801073

10811074
// looks good
1082-
if si.CPUs >= 2 {
1075+
if availableCPUs >= 2 {
10831076
return
10841077
}
10851078

@@ -1236,13 +1229,32 @@ func validateChangedMemoryFlags(drvName string) {
12361229
if !driver.HasResourceLimits(drvName) {
12371230
out.WarningT("The '{{.name}}' driver does not respect the --memory flag", out.V{"name": drvName})
12381231
}
1239-
req, err := util.CalculateSizeInMB(viper.GetString(memory))
1240-
if err != nil {
1241-
exitIfNotForced(reason.Usage, "Unable to parse memory '{{.memory}}': {{.error}}", out.V{"memory": viper.GetString(memory), "error": err})
1232+
var req int
1233+
var err error
1234+
memString := viper.GetString(memory)
1235+
if memString == constants.MaxResources {
1236+
sysLimit, containerLimit, err := memoryLimits(drvName)
1237+
if err != nil {
1238+
klog.Warningf("Unable to query memory limits: %+v", err)
1239+
}
1240+
req = noLimitMemory(sysLimit, containerLimit)
1241+
} else {
1242+
req, err = util.CalculateSizeInMB(memString)
1243+
if err != nil {
1244+
exitIfNotForced(reason.Usage, "Unable to parse memory '{{.memory}}': {{.error}}", out.V{"memory": memString, "error": err})
1245+
}
12421246
}
12431247
validateRequestedMemorySize(req, drvName)
12441248
}
12451249

1250+
func noLimitMemory(sysLimit int, containerLimit int) int {
1251+
if containerLimit != 0 {
1252+
return containerLimit
1253+
}
1254+
// Recommend 1GB to handle OS/VM overhead
1255+
return sysLimit - 1024
1256+
}
1257+
12461258
// This function validates if the --registry-mirror
12471259
// args match the format of http://localhost
12481260
func validateRegistryMirror() {

cmd/minikube/cmd/start_flags.go

+37-6
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ import (
2323

2424
"github.com/blang/semver"
2525
"github.com/pkg/errors"
26+
"github.com/shirou/gopsutil/v3/cpu"
2627
"github.com/spf13/cobra"
2728
"github.com/spf13/viper"
2829
"k8s.io/klog/v2"
2930
"k8s.io/minikube/pkg/drivers/kic"
31+
"k8s.io/minikube/pkg/drivers/kic/oci"
3032
"k8s.io/minikube/pkg/minikube/bootstrapper/bsutil"
3133
"k8s.io/minikube/pkg/minikube/bootstrapper/bsutil/kverify"
3234
"k8s.io/minikube/pkg/minikube/cni"
@@ -135,8 +137,8 @@ func initMinikubeFlags() {
135137
startCmd.Flags().Bool(interactive, true, "Allow user prompts for more information")
136138
startCmd.Flags().Bool(dryRun, false, "dry-run mode. Validates configuration, but does not mutate system state")
137139

138-
startCmd.Flags().Int(cpus, 2, "Number of CPUs allocated to Kubernetes.")
139-
startCmd.Flags().String(memory, "", "Amount of RAM to allocate to Kubernetes (format: <number>[<unit>], where unit = b, k, m or g).")
140+
startCmd.Flags().String(cpus, "2", fmt.Sprintf("Number of CPUs allocated to Kubernetes. Use %q to use the maximum number of CPUs.", constants.MaxResources))
141+
startCmd.Flags().String(memory, "", fmt.Sprintf("Amount of RAM to allocate to Kubernetes (format: <number>[<unit>], where unit = b, k, m or g). Use %q to use the maximum amount of memory.", constants.MaxResources))
140142
startCmd.Flags().String(humanReadableDiskSize, defaultDiskSize, "Disk size allocated to the minikube VM (format: <number>[<unit>], where unit = b, k, m or g).")
141143
startCmd.Flags().Bool(downloadOnly, false, "If true, only download and cache files for later use - don't install or start anything.")
142144
startCmd.Flags().Bool(cacheImages, true, "If true, cache docker images for the current bootstrapper and load them into the machine. Always false with --driver=none.")
@@ -290,6 +292,30 @@ func generateClusterConfig(cmd *cobra.Command, existing *config.ClusterConfig, k
290292
return createNode(cc, kubeNodeName, existing)
291293
}
292294

295+
func getCPUCount(drvName string) int {
296+
if viper.GetString(cpus) != constants.MaxResources {
297+
return viper.GetInt(cpus)
298+
}
299+
300+
if !driver.IsKIC(drvName) {
301+
ci, err := cpu.Counts(true)
302+
if err != nil {
303+
exit.Message(reason.Usage, "Unable to get CPU info: {{.err}}", out.V{"err": err})
304+
}
305+
return ci
306+
}
307+
308+
si, err := oci.CachedDaemonInfo(drvName)
309+
if err != nil {
310+
si, err = oci.DaemonInfo(drvName)
311+
if err != nil {
312+
exit.Message(reason.Usage, "Ensure your {{.driver_name}} is running and is healthy.", out.V{"driver_name": driver.FullName(drvName)})
313+
}
314+
}
315+
316+
return si.CPUs
317+
}
318+
293319
func getMemorySize(cmd *cobra.Command, drvName string) int {
294320
sysLimit, containerLimit, err := memoryLimits(drvName)
295321
if err != nil {
@@ -298,10 +324,15 @@ func getMemorySize(cmd *cobra.Command, drvName string) int {
298324

299325
mem := suggestMemoryAllocation(sysLimit, containerLimit, viper.GetInt(nodes))
300326
if cmd.Flags().Changed(memory) || viper.IsSet(memory) {
327+
memString := viper.GetString(memory)
301328
var err error
302-
mem, err = pkgutil.CalculateSizeInMB(viper.GetString(memory))
303-
if err != nil {
304-
exit.Message(reason.Usage, "Generate unable to parse memory '{{.memory}}': {{.error}}", out.V{"memory": viper.GetString(memory), "error": err})
329+
if memString == constants.MaxResources {
330+
mem = noLimitMemory(sysLimit, containerLimit)
331+
} else {
332+
mem, err = pkgutil.CalculateSizeInMB(memString)
333+
if err != nil {
334+
exit.Message(reason.Usage, "Generate unable to parse memory '{{.memory}}': {{.error}}", out.V{"memory": memString, "error": err})
335+
}
305336
}
306337
if driver.IsKIC(drvName) && mem > containerLimit {
307338
exit.Message(reason.Usage, "{{.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.FullName(drvName)})
@@ -384,7 +415,7 @@ func generateNewConfigFromFlags(cmd *cobra.Command, k8sVersion string, drvName s
384415
KicBaseImage: viper.GetString(kicBaseImage),
385416
Network: viper.GetString(network),
386417
Memory: getMemorySize(cmd, drvName),
387-
CPUs: viper.GetInt(cpus),
418+
CPUs: getCPUCount(drvName),
388419
DiskSize: getDiskSize(),
389420
Driver: drvName,
390421
ListenAddress: viper.GetString(listenAddress),

pkg/minikube/constants/constants.go

+2
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ const (
114114

115115
// TimeFormat is the format that should be used when outputting time
116116
TimeFormat = time.RFC1123
117+
// MaxResources is the value that can be passed into the memory and cpus flags to specify to use maximum resources
118+
MaxResources = "max"
117119
)
118120

119121
var (

site/content/en/docs/commands/start.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ minikube start [flags]
3030
--cache-images If true, cache docker images for the current bootstrapper and load them into the machine. Always false with --driver=none. (default true)
3131
--cni string CNI plug-in to use. Valid options: auto, bridge, calico, cilium, flannel, kindnet, or path to a CNI manifest (default: auto)
3232
--container-runtime string The container runtime to be used (docker, cri-o, containerd). (default "docker")
33-
--cpus int Number of CPUs allocated to Kubernetes. (default 2)
33+
--cpus string Number of CPUs allocated to Kubernetes. Use "max" to use the maximum number of CPUs. (default "2")
3434
--cri-socket string The cri socket path to be used.
3535
--delete-on-failure If set, delete the current cluster if start fails and try again. Defaults to false.
3636
--disable-driver-mounts Disables the filesystem mounts provided by the hypervisors
@@ -73,7 +73,7 @@ minikube start [flags]
7373
--kvm-numa-count int Simulate numa node count in minikube, supported numa node count range is 1-8 (kvm2 driver only) (default 1)
7474
--kvm-qemu-uri string The KVM QEMU connection URI. (kvm2 driver only) (default "qemu:///system")
7575
--listen-address string IP Address to use to expose ports (docker and podman driver only)
76-
--memory string Amount of RAM to allocate to Kubernetes (format: <number>[<unit>], where unit = b, k, m or g).
76+
--memory string Amount of RAM to allocate to Kubernetes (format: <number>[<unit>], where unit = b, k, m or g). Use "max" to use the maximum amount of memory.
7777
--mount This will start the mount daemon and automatically mount files into minikube.
7878
--mount-string string The argument to pass the minikube mount command on start.
7979
--namespace string The named space to activate after start (default "default")

site/content/en/docs/faq/_index.md

+6
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,9 @@ For the docker and podman driver, use `--listen-address` flag:
105105
minikube start --listen-address=0.0.0.0
106106
```
107107

108+
## How can I allocate maximum resources to minikube?
109+
110+
Setting the `memory` and `cpus` flags on the start command to `max` will use maximum available resources:
111+
```
112+
minikube start --memory=max --cpus=max
113+
```

translations/de.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
"Allow user prompts for more information": "",
5252
"Alternative image repository to pull docker images from. This can be used when you have limited access to gcr.io. Set it to \\\"auto\\\" to let minikube decide one for you. For Chinese mainland users, you may use local gcr.io mirrors such as registry.cn-hangzhou.aliyuncs.com/google_containers": "Alternatives Bild-Repository zum Abrufen von Docker-Images. Dies ist hilfreich, wenn Sie nur eingeschränkten Zugriff auf gcr.io haben. Stellen Sie \\\"auto\\\" ein, dann wählt minikube eins für sie aus. Nutzer vom chinesischen Festland können einen lokalen gcr.io-Mirror wie registry.cn-hangzhou.aliyuncs.com/google_containers verwenden.",
5353
"Amount of RAM allocated to the minikube VM (format: \u003cnumber\u003e[\u003cunit\u003e], where unit = b, k, m or g)": "Größe des der minikube-VM zugewiesenen Arbeitsspeichers (Format: \u003cNummer\u003e [\u003cEinheit\u003e], wobei Einheit = b, k, m oder g)",
54-
"Amount of RAM to allocate to Kubernetes (format: \u003cnumber\u003e[\u003cunit\u003e], where unit = b, k, m or g).": "",
5554
"Amount of time to wait for a service in seconds": "",
5655
"Amount of time to wait for service in seconds": "",
5756
"Another hypervisor, such as VirtualBox, is conflicting with KVM. Please stop the other hypervisor, or use --driver to switch to it.": "",
@@ -260,7 +259,6 @@
260259
"Failed to stop node {{.name}}": "",
261260
"Failed to update cluster": "",
262261
"Failed to update config": "",
263-
"Failed to verify '{{.driver_name}} info' will try again ...": "",
264262
"Failed unmount: {{.error}}": "",
265263
"File permissions used for the mount": "",
266264
"Filter to use only VM Drivers": "",
@@ -395,7 +393,6 @@
395393
"None of the known repositories is accessible. Consider specifying an alternative image repository with --image-repository flag": "Keines der bekannten Repositories ist zugänglich. Erwägen Sie, ein alternatives Image-Repository mit der Kennzeichnung --image-repository anzugeben",
396394
"Noticed you have an activated docker-env on {{.driver_name}} driver in this terminal:": "",
397395
"Noticed you have an activated podman-env on {{.driver_name}} driver in this terminal:": "",
398-
"Number of CPUs allocated to Kubernetes.": "",
399396
"Number of CPUs allocated to the minikube VM": "Anzahl der CPUs, die der minikube-VM zugeordnet sind",
400397
"Number of lines back to go within the log": "",
401398
"OS release is {{.pretty_name}}": "",
@@ -700,6 +697,7 @@
700697
"Unable to find control plane": "",
701698
"Unable to generate docs": "",
702699
"Unable to generate the documentation. Please ensure that the path specified is a directory, exists \u0026 you have permission to write to it.": "",
700+
"Unable to get CPU info: {{.err}}": "",
703701
"Unable to get bootstrapper: {{.error}}": "Bootstrapper kann nicht abgerufen werden: {{.error}}",
704702
"Unable to get command runner": "",
705703
"Unable to get control plane status: {{.error}}": "",
@@ -943,4 +941,4 @@
943941
"{{.profile}} profile is not valid: {{.err}}": "",
944942
"{{.type}} is not yet a supported filesystem. We will try anyways!": "",
945943
"{{.url}} is not accessible: {{.error}}": ""
946-
}
944+
}

translations/es.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
"Allow user prompts for more information": "Permitir que el usuario solicite más información",
5353
"Alternative image repository to pull docker images from. This can be used when you have limited access to gcr.io. Set it to \\\"auto\\\" to let minikube decide one for you. For Chinese mainland users, you may use local gcr.io mirrors such as registry.cn-hangzhou.aliyuncs.com/google_containers": "Repositorio de imágenes alternativo del que extraer imágenes de Docker. Puedes usarlo cuando tengas acceso limitado a gcr.io. Si quieres que minikube elija uno por ti, solo tienes que definir el valor como \"auto\". Los usuarios de China continental pueden utilizar réplicas locales de gcr.io, como registry.cn-hangzhou.aliyuncs.com/google_containers",
5454
"Amount of RAM allocated to the minikube VM (format: \u003cnumber\u003e[\u003cunit\u003e], where unit = b, k, m or g)": "Cantidad de RAM asignada a la VM de minikube (formato: \u003cnúmero\u003e[\u003cunidad\u003e], donde unidad = b, k, m o g)",
55-
"Amount of RAM to allocate to Kubernetes (format: \u003cnumber\u003e[\u003cunit\u003e], where unit = b, k, m or g).": "",
5655
"Amount of time to wait for a service in seconds": "Cantidad de tiempo para esperar por un servicio en segundos",
5756
"Amount of time to wait for service in seconds": "Cantidad de tiempo para esperar un servicio en segundos",
5857
"Another hypervisor, such as VirtualBox, is conflicting with KVM. Please stop the other hypervisor, or use --driver to switch to it.": "Otro hipervisor, por ejemplo VirtualBox, está en conflicto con KVM. Por favor detén el otro hipervisor, o usa --driver para cambiarlo.",
@@ -265,7 +264,6 @@
265264
"Failed to stop node {{.name}}": "",
266265
"Failed to update cluster": "",
267266
"Failed to update config": "",
268-
"Failed to verify '{{.driver_name}} info' will try again ...": "",
269267
"Failed unmount: {{.error}}": "",
270268
"File permissions used for the mount": "",
271269
"Filter to use only VM Drivers": "",
@@ -400,7 +398,6 @@
400398
"None of the known repositories is accessible. Consider specifying an alternative image repository with --image-repository flag": "No se puede acceder a ninguno de los repositorios conocidos. Plantéate indicar un repositorio de imágenes alternativo con la marca --image-repository.",
401399
"Noticed you have an activated docker-env on {{.driver_name}} driver in this terminal:": "",
402400
"Noticed you have an activated podman-env on {{.driver_name}} driver in this terminal:": "",
403-
"Number of CPUs allocated to Kubernetes.": "",
404401
"Number of CPUs allocated to the minikube VM": "Número de CPU asignadas a la VM de minikube",
405402
"Number of lines back to go within the log": "",
406403
"OS release is {{.pretty_name}}": "",
@@ -705,6 +702,7 @@
705702
"Unable to find control plane": "",
706703
"Unable to generate docs": "",
707704
"Unable to generate the documentation. Please ensure that the path specified is a directory, exists \u0026 you have permission to write to it.": "",
705+
"Unable to get CPU info: {{.err}}": "",
708706
"Unable to get bootstrapper: {{.error}}": "No se ha podido obtener el programa previo: {{.error}}",
709707
"Unable to get command runner": "",
710708
"Unable to get control plane status: {{.error}}": "",
@@ -947,4 +945,4 @@
947945
"{{.profile}} profile is not valid: {{.err}}": "",
948946
"{{.type}} is not yet a supported filesystem. We will try anyways!": "",
949947
"{{.url}} is not accessible: {{.error}}": ""
950-
}
948+
}

0 commit comments

Comments
 (0)