Skip to content
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

Merged
merged 2 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/crc/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ func (h *Handler) SetConfig(c *context) error {
if len(multiError.Errors) != 0 {
return multiError
}
crcConfig.UpdateDefaults(h.Config)
return c.JSON(http.StatusOK, client.SetOrUnsetConfigResult{
Properties: successProps,
})
Expand Down Expand Up @@ -219,6 +220,7 @@ func (h *Handler) UnsetConfig(c *context) error {
if len(multiError.Errors) != 0 {
return multiError
}
crcConfig.UpdateDefaults(h.Config)
return c.JSON(http.StatusOK, client.SetOrUnsetConfigResult{
Properties: successProps,
})
Expand Down
31 changes: 31 additions & 0 deletions pkg/crc/config/config.go
Original file line number Diff line number Diff line change
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,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 {
Copy link
Contributor

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.

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
Copy link
Member

@anjannath anjannath May 8, 2023

Choose a reason for hiding this comment

The 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 config view ?

Copy link
Member Author

Choose a reason for hiding this comment

The 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 config view.

Copy link
Member

Choose a reason for hiding this comment

The 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 Configuration property '%s' restored to default value '%s' (since you are in the file ;) )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anjannath better to keep it that way.

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down
5 changes: 3 additions & 2 deletions pkg/crc/config/viper_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe also add a comment here, // default values are not written to config file

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


assert.Equal(t, SettingValue{
Value: 4,
Expand Down