Skip to content

Commit

Permalink
fix configmap update (#557)
Browse files Browse the repository at this point in the history
Signed-off-by: Mikhail Scherba <mikhail.scherba@flant.com>
  • Loading branch information
miklezzzz authored Jan 31, 2025
1 parent 1a691ae commit a14efc5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 71 deletions.
33 changes: 14 additions & 19 deletions pkg/kube_config_manager/backend/configmap/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,9 @@ func (b Backend) SaveConfigValues(ctx context.Context, key string, values utils.
}

func (b Backend) saveGlobalConfigValues(ctx context.Context, values utils.Values) ( /*checksum*/ string, error) {
globalKubeConfig, err := config.ParseGlobalKubeConfigFromValues(values)
if err != nil {
return "", err
}
if globalKubeConfig == nil {
return "", nil
globalKubeConfig := &config.GlobalKubeConfig{
Values: values,
Checksum: values.Checksum(),
}

if b.isDebugEnabled(ctx) {
Expand All @@ -90,7 +87,7 @@ func (b Backend) saveGlobalConfigValues(ctx context.Context, values utils.Values
b.logger.Info("Save global values to ConfigMap", slog.String("name", b.name))
}

err = b.mergeValues(ctx, globalKubeConfig.GetValuesWithGlobalName())
err := b.mergeValues(ctx, "global", globalKubeConfig.GetValues())

return globalKubeConfig.Checksum, err
}
Expand All @@ -107,10 +104,9 @@ func (b Backend) isDebugEnabled(ctx context.Context) bool {
// saveModuleConfigValues updates module section in ConfigMap.
// It uses knownChecksums to prevent KubeConfigChanged event on self-update.
func (b Backend) saveModuleConfigValues(ctx context.Context, moduleName string, values utils.Values) ( /*checksum*/ string, error) {
moduleKubeConfig := config.ParseModuleKubeConfigFromValues(moduleName, values)

if moduleKubeConfig == nil {
return "", nil
moduleKubeConfig := &config.ModuleKubeConfig{
ModuleConfig: *utils.NewModuleConfig(moduleName, values),
Checksum: values.Checksum(),
}

if b.isDebugEnabled(ctx) {
Expand All @@ -124,7 +120,7 @@ func (b Backend) saveModuleConfigValues(ctx context.Context, moduleName string,
slog.String("configMapName", b.name))
}

err := b.mergeValues(ctx, moduleKubeConfig.GetValuesWithModuleName()) //nolint: staticcheck,nolintlint
err := b.mergeValues(ctx, utils.ModuleNameToValuesKey(moduleName), moduleKubeConfig.GetValues()) //nolint: staticcheck,nolintlint

return moduleKubeConfig.Checksum, err
}
Expand Down Expand Up @@ -282,15 +278,14 @@ func fromConfigMapData(moduleName string, configData map[string]string) (*utils.
return mc.LoadFromValues(configValues)
}

func (b Backend) mergeValues(ctx context.Context, values utils.Values) error {
cmData, err := values.AsConfigMapData()
if err != nil {
return err
}
func (b Backend) mergeValues(ctx context.Context, moduleName string, values utils.Values) error {
return b.updateConfigMap(ctx, func(obj *v1.ConfigMap) error {
for k, v := range cmData {
obj.Data[k] = v
data, err := yaml.Marshal(values)
if err != nil {
return err
}

obj.Data[moduleName] = string(data)
return nil
})
}
Expand Down
38 changes: 0 additions & 38 deletions pkg/kube_config_manager/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ type GlobalKubeConfig struct {
Checksum string
}

// GetValuesWithGlobalName
// Deprecated: use GetValues instead
func (gkc GlobalKubeConfig) GetValuesWithGlobalName() utils.Values {
return gkc.Values
}

// GetValues returns global values, enrich them with top level key 'global'
func (gkc GlobalKubeConfig) GetValues() utils.Values {
if len(gkc.Values) == 0 {
Expand Down Expand Up @@ -64,35 +58,3 @@ const (
KubeConfigChanged KubeConfigType = "Changed"
KubeConfigInvalid KubeConfigType = "Invalid"
)

func ParseGlobalKubeConfigFromValues(values utils.Values) (*GlobalKubeConfig, error) {
if !values.HasGlobal() {
return nil, nil
}

globalValues := values.Global()

checksum := globalValues.Checksum()

return &GlobalKubeConfig{
Values: globalValues,
Checksum: checksum,
}, nil
}

func ParseModuleKubeConfigFromValues(moduleName string, values utils.Values) *ModuleKubeConfig {
valuesKey := utils.ModuleNameToValuesKey(moduleName)
if !values.HasKey(valuesKey) {
return nil
}

//nolint: staticcheck,nolintlint
moduleValues := values.SectionByKey(valuesKey)

checksum := moduleValues.Checksum()

return &ModuleKubeConfig{
ModuleConfig: *utils.NewModuleConfig(moduleName, moduleValues),
Checksum: checksum,
}
}
14 changes: 0 additions & 14 deletions pkg/utils/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,6 @@ func (v Values) AsString(format string) string {
return string(b)
}

// AsConfigMapData returns values as map that can be used as a 'data' field in the ConfigMap.
func (v Values) AsConfigMapData() (map[string]string, error) {
res := make(map[string]string)

for k, value := range v {
dump, err := yaml.Marshal(value)
if err != nil {
return nil, err
}
res[k] = string(dump)
}
return res, nil
}

func (v Values) JsonString() string {
return v.AsString("json")
}
Expand Down

0 comments on commit a14efc5

Please sign in to comment.