Skip to content

Commit 8dd0f1e

Browse files
committed
add ability to pass nolimit value to memory and cpus flags
1 parent 5501afa commit 8dd0f1e

File tree

6 files changed

+70
-16
lines changed

6 files changed

+70
-16
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.NoLimit {
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.NoLimit {
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

+39-10
Original file line numberDiff line numberDiff line change
@@ -1045,11 +1045,8 @@ func validateCPUCount(drvName string) {
10451045
cpuCount = viper.GetInt(cpus)
10461046
}
10471047

1048-
if cpuCount < minimumCPUS {
1049-
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})
1050-
}
1051-
1052-
if !driver.IsKIC((drvName)) {
1048+
if !driver.IsKIC(drvName) {
1049+
validateMeetsMinimumCPURequirements(cpuCount, minimumCPUS)
10531050
return
10541051
}
10551052

@@ -1063,16 +1060,23 @@ func validateCPUCount(drvName string) {
10631060

10641061
}
10651062

1063+
if viper.GetString(cpus) == constants.NoLimit {
1064+
cpuCount = si.CPUs
1065+
viper.Set(cpus, cpuCount)
1066+
}
1067+
1068+
validateMeetsMinimumCPURequirements(cpuCount, minimumCPUS)
1069+
10661070
if si.CPUs < cpuCount {
10671071

10681072
if driver.IsDockerDesktop(drvName) {
10691073
out.Styled(style.Empty, `- Ensure your {{.driver_name}} daemon has access to enough CPU/memory resources.`, out.V{"driver_name": drvName})
10701074
if runtime.GOOS == "darwin" {
1071-
out.Styled(style.Empty, `- Docs https://docs.docker.com/docker-for-mac/#resources`, out.V{"driver_name": drvName})
1075+
out.Styled(style.Empty, `- Docs https://docs.docker.com/docker-for-mac/#resources`)
10721076
}
10731077
if runtime.GOOS == "windows" {
10741078
out.String("\n\t")
1075-
out.Styled(style.Empty, `- Docs https://docs.docker.com/docker-for-windows/#resources`, out.V{"driver_name": drvName})
1079+
out.Styled(style.Empty, `- Docs https://docs.docker.com/docker-for-windows/#resources`)
10761080
}
10771081
}
10781082

@@ -1093,6 +1097,12 @@ func validateCPUCount(drvName string) {
10931097
}
10941098
}
10951099

1100+
func validateMeetsMinimumCPURequirements(cpuCount int, minimumCPUs int) {
1101+
if cpuCount < minimumCPUS {
1102+
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})
1103+
}
1104+
}
1105+
10961106
// validateFlags validates the supplied flags against known bad combinations
10971107
func validateFlags(cmd *cobra.Command, drvName string) {
10981108
if cmd.Flags().Changed(humanReadableDiskSize) {
@@ -1237,13 +1247,32 @@ func validateChangedMemoryFlags(drvName string) {
12371247
if !driver.HasResourceLimits(drvName) {
12381248
out.WarningT("The '{{.name}}' driver does not respect the --memory flag", out.V{"name": drvName})
12391249
}
1240-
req, err := util.CalculateSizeInMB(viper.GetString(memory))
1241-
if err != nil {
1242-
exitIfNotForced(reason.Usage, "Unable to parse memory '{{.memory}}': {{.error}}", out.V{"memory": viper.GetString(memory), "error": err})
1250+
var req int
1251+
var err error
1252+
memString := viper.GetString(memory)
1253+
if memString == constants.NoLimit {
1254+
sysLimit, containerLimit, err := memoryLimits(drvName)
1255+
if err != nil {
1256+
klog.Warningf("Unable to query memory limits: %+v", err)
1257+
}
1258+
req = noLimitMemory(sysLimit, containerLimit)
1259+
} else {
1260+
req, err = util.CalculateSizeInMB(memString)
1261+
if err != nil {
1262+
exitIfNotForced(reason.Usage, "Unable to parse memory '{{.memory}}': {{.error}}", out.V{"memory": memString, "error": err})
1263+
}
12431264
}
12441265
validateRequestedMemorySize(req, drvName)
12451266
}
12461267

1268+
func noLimitMemory(sysLimit int, containerLimit int) int {
1269+
if containerLimit != 0 {
1270+
return containerLimit
1271+
}
1272+
// Recommend 1GB to handle OS/VM overhead
1273+
return sysLimit - 1024
1274+
}
1275+
12471276
// This function validates if the --registry-mirror
12481277
// args match the format of http://localhost
12491278
func validateRegistryMirror() {

cmd/minikube/cmd/start_flags.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ func initMinikubeFlags() {
135135
startCmd.Flags().Bool(interactive, true, "Allow user prompts for more information")
136136
startCmd.Flags().Bool(dryRun, false, "dry-run mode. Validates configuration, but does not mutate system state")
137137

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).")
138+
startCmd.Flags().String(cpus, "2", "Number of CPUs allocated to Kubernetes. Use 'nolimit' to use the maximum number of CPUs.")
139+
startCmd.Flags().String(memory, "", "Amount of RAM to allocate to Kubernetes (format: <number>[<unit>], where unit = b, k, m or g). Use 'nolimit' to use the maximum amount of memory.")
140140
startCmd.Flags().String(humanReadableDiskSize, defaultDiskSize, "Disk size allocated to the minikube VM (format: <number>[<unit>], where unit = b, k, m or g).")
141141
startCmd.Flags().Bool(downloadOnly, false, "If true, only download and cache files for later use - don't install or start anything.")
142142
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.")
@@ -298,10 +298,15 @@ func getMemorySize(cmd *cobra.Command, drvName string) int {
298298

299299
mem := suggestMemoryAllocation(sysLimit, containerLimit, viper.GetInt(nodes))
300300
if cmd.Flags().Changed(memory) || viper.IsSet(memory) {
301+
memString := viper.GetString(memory)
301302
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})
303+
if memString == constants.NoLimit {
304+
mem = noLimitMemory(sysLimit, containerLimit)
305+
} else {
306+
mem, err = pkgutil.CalculateSizeInMB(memString)
307+
if err != nil {
308+
exit.Message(reason.Usage, "Generate unable to parse memory '{{.memory}}': {{.error}}", out.V{"memory": memString, "error": err})
309+
}
305310
}
306311
if driver.IsKIC(drvName) && mem > containerLimit {
307312
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)})

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+
// NoLimit is the value that can be passed into the memory and cpus flags to specifiy to use maximum resources
118+
NoLimit = "nolimit"
117119
)
118120

119121
var (

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 `nolimit` will use maximum available resources:
111+
```
112+
minikube start --memory=nolimit --cpus=nolimit
113+
```

0 commit comments

Comments
 (0)