-
Notifications
You must be signed in to change notification settings - Fork 243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Config: Make preset config aware of cpu/memory #3636
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,36 @@ 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 | ||
// ~/.crc/crc.json (viper config) file. | ||
if setting.defaultValue == castValue { | ||
if _, err := c.Unset(key); err != nil { | ||
return "", err | ||
} | ||
return "", nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think we should print a message letting the user know the config is restored to its default, since we won't see the config option in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @anjannath Currently also we are not seeing the default configs as part of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right, do you prefer to keep it that way, or show a message saying There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @anjannath better to keep it that way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also think we need to inform the user that we changed their configuration settings. I'll add that. |
||
} | ||
|
||
if setting.isSecret { | ||
if err := c.secretStorage.Set(key, castValue); err != nil { | ||
return "", err | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,13 +115,14 @@ func TestViperConfigLoadDefaultValue(t *testing.T) { | |
Value: 4, | ||
IsDefault: true, | ||
}, config.Get(cpus)) | ||
|
||
_, err = config.Set(cpus, 4) | ||
assert.NoError(t, err) | ||
|
||
bin, err := os.ReadFile(configFile) | ||
assert.NoError(t, err) | ||
assert.JSONEq(t, `{"cpus":4}`, string(bin)) | ||
// Setting default value will not update | ||
// write to the config so expected would be {} | ||
assert.JSONEq(t, `{}`, string(bin)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe also add a comment here, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
assert.Equal(t, SettingValue{ | ||
Value: 4, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we also use disk-size? This is now identical, but would suggest to create an issue to follow-up.