Skip to content

Commit

Permalink
Config: Make preset config aware of cpu/memory
Browse files Browse the repository at this point in the history
Preset property is bound with cpu/memory/bundle and have default
values for it but our `set` function doesn't have map around those
settings and a user can do following without any error message.
```
$ crc config set preset microshift
$ crc config set cpus 2
$ crc config set preset openshift
```
and when user try to perform `crc start` then error message shown.

With this PR we are making the set function bit self aware of the
preset and it's bound properties (cpu/memory) and make sure if those
property doesn't validated for the preset then unset those so default
values going to take over.

with this PR following now works as expected.
```
$ curl -X POST -d '{"properties":{"preset": "microshift"}}' --unix-socket ~/.crc/crc-http.sock http:/c/api/config
{"Properties":["preset"]}
$ curl -X POST -d '{"properties":{"cpus": "3"}}' --unix-socket ~/.crc/crc-http.sock http:/c/api/config
{"Properties":["cpus"]}
$ curl -X POST -d '{"properties":{"preset": "openshift"}}' --unix-socket ~/.crc/crc-http.sock http:/c/api/config
{"Properties":["preset"]}
$ curl -X GET --unix-socket ~/.crc/crc-http.sock http:/c/api/config | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   894  100   894    0     0  56924      0 --:--:-- --:--:-- --:--:-- 59600
{
  "Configs": {
    "bundle": "/Users/prkumar/.crc/cache/crc_vfkit_4.12.13_arm64.crcbundle",
    "consent-telemetry": "no",
    "cpus": 4,
    "disable-update-check": true,
    "disk-size": 31,
    "enable-cluster-monitoring": false,
    "enable-experimental-features": false,
    "enable-shared-dirs": true,
    "host-network-access": false,
    "http-proxy": "",
    "https-proxy": "",
    "ingress-http-port": 80,
    "ingress-https-port": 443,
    "kubeadmin-password": "",
    "memory": 9216,

[...]
```
  • Loading branch information
praveenkumar committed May 8, 2023
1 parent f9a0a4a commit 73a4b92
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/crc/config/config.go
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"

"github.com/crc-org/crc/pkg/crc/preset"
"github.com/crc-org/crc/pkg/crc/validation"
"github.com/spf13/cast"
)

Expand Down Expand Up @@ -95,6 +96,25 @@ func (c *Config) Set(key string, value interface{}) (string, error) {
return "", fmt.Errorf(invalidType, value, key)
}

// Preset is mapped with `memory`, `cpus` and `bundle` and
// we want to make sure if cpu or memory is less for a preset
// then default is set automatic.
if setting.Name == Preset {
preset := preset.ParsePreset(value.(string))
mem := c.Get(Memory)
if err := validation.ValidateMemory(mem.AsInt(), preset); err != nil {
if _, err := c.Unset(Memory); err != nil {
return "", err
}
}
cpu := c.Get(CPUs)
if err := validation.ValidateCPUs(cpu.AsInt(), preset); err != nil {
if _, err := c.Unset(CPUs); err != nil {
return "", err
}
}
}

// Make sure if user try to set same value which
// is default then just unset the value which
// anyway make it default and don't update it
Expand Down

0 comments on commit 73a4b92

Please sign in to comment.