|
1 | 1 | package config |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
| 4 | + "fmt" |
| 5 | + |
| 6 | + kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1" |
| 7 | + "github.com/replicatedhq/kotskinds/multitype" |
| 8 | + "github.com/tiendc/go-deepcopy" |
5 | 9 | ) |
6 | 10 |
|
| 11 | +func (m *appConfigManager) GetConfig(config kotsv1beta1.Config) (kotsv1beta1.Config, error) { |
| 12 | + return filterAppConfig(config) |
| 13 | +} |
| 14 | + |
7 | 15 | func (m *appConfigManager) GetConfigValues() (map[string]string, error) { |
8 | 16 | return m.appConfigStore.GetConfigValues() |
9 | 17 | } |
10 | 18 |
|
11 | | -func (m *appConfigManager) SetConfigValues(ctx context.Context, values map[string]string) error { |
12 | | - return m.appConfigStore.SetConfigValues(values) |
| 19 | +func (m *appConfigManager) SetConfigValues(config kotsv1beta1.Config, configValues map[string]string) error { |
| 20 | + filteredValues := make(map[string]string) |
| 21 | + |
| 22 | + // only include values for enabled groups and items |
| 23 | + for _, g := range config.Spec.Groups { |
| 24 | + for _, i := range g.Items { |
| 25 | + if isItemEnabled(g.When) && isItemEnabled(i.When) { |
| 26 | + value, ok := configValues[i.Name] |
| 27 | + if ok { |
| 28 | + filteredValues[i.Name] = value |
| 29 | + } |
| 30 | + for _, c := range i.Items { |
| 31 | + value, ok := configValues[c.Name] |
| 32 | + if ok { |
| 33 | + filteredValues[c.Name] = value |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return m.appConfigStore.SetConfigValues(filteredValues) |
| 41 | +} |
| 42 | + |
| 43 | +// filterAppConfig filters out disabled groups and items based on their 'when' condition |
| 44 | +func filterAppConfig(config kotsv1beta1.Config) (kotsv1beta1.Config, error) { |
| 45 | + // deepcopy the config to avoid mutating the original config |
| 46 | + var updatedConfig kotsv1beta1.Config |
| 47 | + if err := deepcopy.Copy(&updatedConfig, &config); err != nil { |
| 48 | + return kotsv1beta1.Config{}, fmt.Errorf("deepcopy: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + filteredGroups := make([]kotsv1beta1.ConfigGroup, 0) |
| 52 | + |
| 53 | + for _, group := range config.Spec.Groups { |
| 54 | + if !isItemEnabled(group.When) { |
| 55 | + continue |
| 56 | + } |
| 57 | + filteredItems := make([]kotsv1beta1.ConfigItem, 0) |
| 58 | + for _, item := range group.Items { |
| 59 | + if !isItemEnabled(item.When) { |
| 60 | + continue |
| 61 | + } |
| 62 | + filteredItems = append(filteredItems, item) |
| 63 | + } |
| 64 | + if len(filteredItems) > 0 { |
| 65 | + group.Items = filteredItems |
| 66 | + filteredGroups = append(filteredGroups, group) |
| 67 | + } |
| 68 | + } |
| 69 | + updatedConfig.Spec.Groups = filteredGroups |
| 70 | + return updatedConfig, nil |
| 71 | +} |
| 72 | + |
| 73 | +// isItemEnabled checks if an item is enabled based on its 'when' condition |
| 74 | +func isItemEnabled(when multitype.QuotedBool) bool { |
| 75 | + return when != "false" |
13 | 76 | } |
0 commit comments