From 73a4b92d8d5527a952c770e9b048356216416c7a Mon Sep 17 00:00:00 2001 From: Praveen Kumar Date: Mon, 8 May 2023 09:12:05 +0530 Subject: [PATCH] Config: Make preset config aware of cpu/memory 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, [...] ``` --- pkg/crc/config/config.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pkg/crc/config/config.go b/pkg/crc/config/config.go index 958320066d..eb5d39e176 100644 --- a/pkg/crc/config/config.go +++ b/pkg/crc/config/config.go @@ -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" ) @@ -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