Skip to content

Commit 68d187f

Browse files
authored
Merge pull request #17491 from spowelljr/noLimit
Kic: Add "no-limit" option to cpus & memory flags
2 parents d437eed + b9c6c6e commit 68d187f

File tree

7 files changed

+52
-25
lines changed

7 files changed

+52
-25
lines changed

cmd/minikube/cmd/start.go

+26-14
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,11 @@ func validateRequestedMemorySize(req int, drvName string) {
11491149
exitIfNotForced(reason.RsrcInsufficientSysMemory, "System only has {{.size}}MiB available, less than the required {{.req}}MiB for Kubernetes", out.V{"size": sysLimit, "req": minUsableMem})
11501150
}
11511151

1152+
// if --memory=no-limit, ignore remaining checks
1153+
if req == 0 && driver.IsKIC(drvName) {
1154+
return
1155+
}
1156+
11521157
if req < minUsableMem {
11531158
exitIfNotForced(reason.RsrcInsufficientReqMemory, "Requested memory allocation {{.requested}}MiB is less than the usable minimum of {{.minimum_memory}}MB", out.V{"requested": req, "minimum_memory": minUsableMem})
11541159
}
@@ -1208,6 +1213,21 @@ func validateCPUCount(drvName string) {
12081213
availableCPUs = ci
12091214
}
12101215

1216+
if availableCPUs < 2 {
1217+
if drvName == oci.Docker && runtime.GOOS == "darwin" {
1218+
exitIfNotForced(reason.RsrcInsufficientDarwinDockerCores, "Docker Desktop has less than 2 CPUs configured, but Kubernetes requires at least 2 to be available")
1219+
} else if drvName == oci.Docker && runtime.GOOS == "windows" {
1220+
exitIfNotForced(reason.RsrcInsufficientWindowsDockerCores, "Docker Desktop has less than 2 CPUs configured, but Kubernetes requires at least 2 to be available")
1221+
} else {
1222+
exitIfNotForced(reason.RsrcInsufficientCores, "{{.driver_name}} has less than 2 CPUs available, but Kubernetes requires at least 2 to be available", out.V{"driver_name": driver.FullName(viper.GetString("driver"))})
1223+
}
1224+
}
1225+
1226+
// if --cpus=no-limit, ignore remaining checks
1227+
if cpuCount == 0 && driver.IsKIC(drvName) {
1228+
return
1229+
}
1230+
12111231
if cpuCount < minimumCPUS {
12121232
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})
12131233
}
@@ -1226,19 +1246,6 @@ func validateCPUCount(drvName string) {
12261246

12271247
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})
12281248
}
1229-
1230-
// looks good
1231-
if availableCPUs >= 2 {
1232-
return
1233-
}
1234-
1235-
if drvName == oci.Docker && runtime.GOOS == "darwin" {
1236-
exitIfNotForced(reason.RsrcInsufficientDarwinDockerCores, "Docker Desktop has less than 2 CPUs configured, but Kubernetes requires at least 2 to be available")
1237-
} else if drvName == oci.Docker && runtime.GOOS == "windows" {
1238-
exitIfNotForced(reason.RsrcInsufficientWindowsDockerCores, "Docker Desktop has less than 2 CPUs configured, but Kubernetes requires at least 2 to be available")
1239-
} else {
1240-
exitIfNotForced(reason.RsrcInsufficientCores, "{{.driver_name}} has less than 2 CPUs available, but Kubernetes requires at least 2 to be available", out.V{"driver_name": driver.FullName(viper.GetString("driver"))})
1241-
}
12421249
}
12431250

12441251
// validateFlags validates the supplied flags against known bad combinations
@@ -1505,13 +1512,18 @@ func validateChangedMemoryFlags(drvName string) {
15051512
var req int
15061513
var err error
15071514
memString := viper.GetString(memory)
1508-
if memString == constants.MaxResources {
1515+
if memString == constants.NoLimit && driver.IsKIC(drvName) {
1516+
req = 0
1517+
} else if memString == constants.MaxResources {
15091518
sysLimit, containerLimit, err := memoryLimits(drvName)
15101519
if err != nil {
15111520
klog.Warningf("Unable to query memory limits: %+v", err)
15121521
}
15131522
req = noLimitMemory(sysLimit, containerLimit, drvName)
15141523
} else {
1524+
if memString == constants.NoLimit {
1525+
exit.Message(reason.Usage, "The '{{.name}}' driver does not support --memory=no-limit", out.V{"name": drvName})
1526+
}
15151527
req, err = util.CalculateSizeInMB(memString)
15161528
if err != nil {
15171529
exitIfNotForced(reason.Usage, "Unable to parse memory '{{.memory}}': {{.error}}", out.V{"memory": memString, "error": err})

cmd/minikube/cmd/start_flags.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ func initMinikubeFlags() {
160160
startCmd.Flags().Bool(interactive, true, "Allow user prompts for more information")
161161
startCmd.Flags().Bool(dryRun, false, "dry-run mode. Validates configuration, but does not mutate system state")
162162

163-
startCmd.Flags().String(cpus, "2", fmt.Sprintf("Number of CPUs allocated to Kubernetes. Use %q to use the maximum number of CPUs.", constants.MaxResources))
164-
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))
163+
startCmd.Flags().String(cpus, "2", fmt.Sprintf("Number of CPUs allocated to Kubernetes. Use %q to use the maximum number of CPUs. Use %q to not specify a limit (Docker/Podman only)", constants.MaxResources, constants.NoLimit))
164+
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. Use %q to not specify a limit (Docker/Podman only)", constants.MaxResources, constants.NoLimit))
165165
startCmd.Flags().String(humanReadableDiskSize, defaultDiskSize, "Disk size allocated to the minikube VM (format: <number>[<unit>], where unit = b, k, m or g).")
166166
startCmd.Flags().Bool(downloadOnly, false, "If true, only download and cache files for later use - don't install or start anything.")
167167
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.")
@@ -337,6 +337,12 @@ func generateClusterConfig(cmd *cobra.Command, existing *config.ClusterConfig, k
337337
}
338338

339339
func getCPUCount(drvName string) int {
340+
if viper.GetString(cpus) == constants.NoLimit {
341+
if driver.IsKIC(drvName) {
342+
return 0
343+
}
344+
exit.Message(reason.Usage, "The '{{.name}}' driver does not support --cpus=no-limit", out.V{"name": drvName})
345+
}
340346
if viper.GetString(cpus) != constants.MaxResources {
341347
return viper.GetInt(cpus)
342348
}
@@ -370,7 +376,9 @@ func getMemorySize(cmd *cobra.Command, drvName string) int {
370376
if cmd.Flags().Changed(memory) || viper.IsSet(memory) {
371377
memString := viper.GetString(memory)
372378
var err error
373-
if memString == constants.MaxResources {
379+
if memString == constants.NoLimit && driver.IsKIC(drvName) {
380+
mem = 0
381+
} else if memString == constants.MaxResources {
374382
mem = noLimitMemory(sysLimit, containerLimit, drvName)
375383
} else {
376384
mem, err = pkgutil.CalculateSizeInMB(memString)

pkg/drivers/kic/kic.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,16 @@ func (d *Driver) Create() error {
8383
ClusterLabel: oci.ProfileLabelKey + "=" + d.MachineName,
8484
NodeLabel: oci.NodeLabelKey + "=" + d.NodeConfig.MachineName,
8585
CPUs: strconv.Itoa(d.NodeConfig.CPU),
86-
Memory: strconv.Itoa(d.NodeConfig.Memory) + "mb",
86+
Memory: strconv.Itoa(d.NodeConfig.Memory),
8787
Envs: d.NodeConfig.Envs,
8888
ExtraArgs: append([]string{"--expose", fmt.Sprintf("%d", d.NodeConfig.APIServerPort)}, d.NodeConfig.ExtraArgs...),
8989
OCIBinary: d.NodeConfig.OCIBinary,
9090
APIServerPort: d.NodeConfig.APIServerPort,
9191
GPUs: d.NodeConfig.GPUs,
9292
}
93+
if params.Memory != "0" {
94+
params.Memory += "mb"
95+
}
9396

9497
networkName := d.NodeConfig.Network
9598
if networkName == "" {

pkg/drivers/kic/oci/oci.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func checkRunning(p CreateParams) func() error {
147147
}
148148

149149
// CreateContainerNode creates a new container node
150-
func CreateContainerNode(p CreateParams) error {
150+
func CreateContainerNode(p CreateParams) error { //nolint to suppress cyclomatic complexity
151151
// on windows os, if docker desktop is using Windows Containers. Exit early with error
152152
if p.OCIBinary == Docker && runtime.GOOS == "windows" {
153153
info, err := DaemonInfo(p.OCIBinary)
@@ -203,11 +203,11 @@ func CreateContainerNode(p CreateParams) error {
203203
// podman mounts var/lib with no-exec by default https://github.com/containers/libpod/issues/5103
204204
runArgs = append(runArgs, "--volume", fmt.Sprintf("%s:/var:exec", p.Name))
205205

206-
if memcgSwap {
206+
if memcgSwap && p.Memory != NoLimit {
207207
runArgs = append(runArgs, fmt.Sprintf("--memory-swap=%s", p.Memory))
208208
}
209209

210-
if memcg {
210+
if memcg && p.Memory != NoLimit {
211211
runArgs = append(runArgs, fmt.Sprintf("--memory=%s", p.Memory))
212212
}
213213

@@ -218,10 +218,10 @@ func CreateContainerNode(p CreateParams) error {
218218
// ignore apparmore github actions docker: https://github.com/kubernetes/minikube/issues/7624
219219
runArgs = append(runArgs, "--security-opt", "apparmor=unconfined")
220220

221-
if memcg {
221+
if memcg && p.Memory != NoLimit {
222222
runArgs = append(runArgs, fmt.Sprintf("--memory=%s", p.Memory))
223223
}
224-
if memcgSwap {
224+
if memcgSwap && p.Memory != NoLimit {
225225
// Disable swap by setting the value to match
226226
runArgs = append(runArgs, fmt.Sprintf("--memory-swap=%s", p.Memory))
227227
}
@@ -244,7 +244,7 @@ func CreateContainerNode(p CreateParams) error {
244244
}
245245
}
246246

247-
if cpuCfsPeriod && cpuCfsQuota {
247+
if cpuCfsPeriod && cpuCfsQuota && p.CPUs != NoLimit {
248248
runArgs = append(runArgs, fmt.Sprintf("--cpus=%s", p.CPUs))
249249
}
250250

pkg/drivers/kic/oci/types.go

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const (
3939
nodeRoleLabelKey = "role.minikube.sigs.k8s.io"
4040
// CreatedByLabelKey is applied to any container/volume that is created by minikube created_by.minikube.sigs.k8s.io=true
4141
CreatedByLabelKey = "created_by.minikube.sigs.k8s.io"
42+
// NoLimit is the value that specifies that no resource limit should be set
43+
NoLimit = "0"
4244
)
4345

4446
// CreateParams are parameters needed to create a container

pkg/minikube/constants/constants.go

+2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ const (
138138
TimeFormat = time.RFC822
139139
// MaxResources is the value that can be passed into the memory and cpus flags to specify to use maximum resources
140140
MaxResources = "max"
141+
// NoLimit is the value that can be passed into the memory and cpus flags to specify to not set the resource limit on the container (Docker & Podman only)
142+
NoLimit = "no-limit"
141143

142144
// DefaultCertExpiration is the amount of time in the future a certificate will expire in by default, which is 3 years
143145
DefaultCertExpiration = time.Hour * 24 * 365 * 3

pkg/minikube/machine/start.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ func showHostInfo(h *host.Host, cfg config.ClusterConfig) {
397397
}
398398
if driver.IsKIC(cfg.Driver) { // TODO:medyagh add free disk space on docker machine
399399
register.Reg.SetStep(register.CreatingContainer)
400-
out.Step(style.StartingVM, "Creating {{.driver_name}} {{.machine_type}} (CPUs={{.number_of_cpus}}, Memory={{.memory_size}}MB) ...", out.V{"driver_name": cfg.Driver, "number_of_cpus": cfg.CPUs, "memory_size": cfg.Memory, "machine_type": machineType})
400+
out.Step(style.StartingVM, "Creating {{.driver_name}} {{.machine_type}} (CPUs={{if not .number_of_cpus}}no-limit{{else}}{{.number_of_cpus}}{{end}}, Memory={{if not .memory_size}}no-limit{{else}}{{.memory_size}}MB{{end}}) ...", out.V{"driver_name": cfg.Driver, "number_of_cpus": cfg.CPUs, "memory_size": cfg.Memory, "machine_type": machineType})
401401
return
402402
}
403403
register.Reg.SetStep(register.CreatingVM)

0 commit comments

Comments
 (0)