Skip to content

Commit

Permalink
config: use config notifier to call UpdateDefaults()
Browse files Browse the repository at this point in the history
There are calls to config.UpdateDefaults in pkg/crc/api/ to reload the
configuration settings every time a config is set/unset. This ensures the
default cpus/memory values are correct when the preset changes.

Then there is code in pkg/crc/config.Set()/Unset() to reset the cpus/memory
values when the preset changes if they are out of spec.

This commit groups in UpdateDefaults all the work that is needed when
the preset changes. It moves the Set/Unset UpdateDefaults calls from
pkg/crc/api to pkg/crc/config

This should fix crc-org#3652
  • Loading branch information
cfergeau committed May 17, 2023
1 parent 717f2a9 commit d08edb7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
2 changes: 0 additions & 2 deletions pkg/crc/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ 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 @@ -220,7 +219,6 @@ 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
24 changes: 14 additions & 10 deletions pkg/crc/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,7 @@ func RegisterSettings(cfg *Config) {
}

func presetChanged(cfg *Config, _ string, _ interface{}) {
// The `memory` and `cpus` values are preset-dependent.
// When they are lower than the preset requirements, this code
// automatically resets them to their default value.
if err := revalidateSettingsValue(cfg, Memory); err != nil {
logging.Debugf("error validating Memory config value: %v", err)
}
if err := revalidateSettingsValue(cfg, CPUs); err != nil {
logging.Debugf("error validating CPUs config value: %v", err)
}
UpdateDefaults(cfg)
}

func defaultCPUs(cfg Storage) int {
Expand Down Expand Up @@ -189,8 +181,20 @@ func revalidateSettingsValue(cfg *Config, key string) error {
return nil
}

func UpdateDefaults(cfg *Config) {
func UpdateDefaults(cfg *Config) error {
RegisterSettings(cfg)

// The `memory` and `cpus` values are preset-dependent.
// When they are lower than the preset requirements, this code
// automatically resets them to their default value.
if err := revalidateSettingsValue(cfg, Memory); err != nil {
return err
}
if err := revalidateSettingsValue(cfg, CPUs); err != nil {
return err
}

return nil
}

func BundleHelpMsg(cfg *Config) string {
Expand Down

0 comments on commit d08edb7

Please sign in to comment.