From ed6d2af7f05fa4455a76f8afa8374a0e3d762185 Mon Sep 17 00:00:00 2001 From: yossig-runai <143929074+yossig-runai@users.noreply.github.com> Date: Mon, 30 Sep 2024 13:24:37 +0300 Subject: [PATCH] fix: adding check for newer config version if replicaset not exists for the ignoreExtraneousOperation --- internal/common/common.go | 77 +++++++++++++++++++++++++++++++++++++-- internal/tools/tools.go | 8 ++++ 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/internal/common/common.go b/internal/common/common.go index 2aac1cf..2411897 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -58,8 +58,8 @@ func (r *ArgoRolloutConfigKeeperCommon) ReconcileConfigMaps(ctx context.Context, return err } - if latestVersion, err := r.getLatestVersionOfReplicaSet(ctx, c.Namespace, strings.Split(finalizerFullName, "/")[1]); err != nil { - r.Logger.Error(err, "unable to get latest version of replicaset") + if latestVersion, err := r.getLatestVersionOfConfig(ctx, strings.Split(finalizerFullName, "/")[1], &c); err != nil { + r.Logger.Error(err, "unable to get latest version of configmap") return err } else { if err := r.ignoreExtraneousOperation(ctx, &c, latestVersion); err != nil { @@ -112,8 +112,8 @@ func (r *ArgoRolloutConfigKeeperCommon) ReconcileSecrets(ctx context.Context, na return err } - if latestVersion, err := r.getLatestVersionOfReplicaSet(ctx, s.Namespace, strings.Split(finalizerFullName, "/")[1]); err != nil { - r.Logger.Error(err, "unable to get latest version of replicaset") + if latestVersion, err := r.getLatestVersionOfConfig(ctx, strings.Split(finalizerFullName, "/")[1], &s); err != nil { + r.Logger.Error(err, "unable to get latest version of secret") return err } else { if err := r.ignoreExtraneousOperation(ctx, &s, latestVersion); err != nil { @@ -358,3 +358,72 @@ func (r *ArgoRolloutConfigKeeperCommon) listSecrets(ctx context.Context, namespa return secrets, nil } + +func (r *ArgoRolloutConfigKeeperCommon) getLatestVersionOfConfig(ctx context.Context, finalizerFullName string, T interface{}) (*goversion.Version, error) { + + switch t := T.(type) { + case *corev1.ConfigMap: + + labels := tools.CopyMap(t.Labels) + // need to remove the appVersion label from the labels to all configMaps + delete(labels, r.Labels.AppVersionLabel) + latestVersion, err := r.getLatestVersionOfReplicaSet(ctx, t.Namespace, finalizerFullName) + if err != nil { + r.Logger.Error(err, "unable to get latest version of replicaset") + return nil, err + } + + if latestVersion == nil { + if configMaps, err := r.listConfigMaps(ctx, t.Namespace, labels); err != nil { + return nil, err + } else { + for _, c := range configMaps.Items { + if val, ok := c.Labels[r.Labels.AppVersionLabel]; ok { + ver, err := goversion.NewVersion(val) + if err != nil { + r.Logger.Error(err, "unable to parse version") + return nil, err + } + if latestVersion == nil || ver.GreaterThan(latestVersion) { + latestVersion = ver + } + } + } + } + } + return latestVersion, nil + case *corev1.Secret: + labels := tools.CopyMap(t.Labels) + + // need to remove the appVersion label from the labels to all secrets + delete(labels, r.Labels.AppVersionLabel) + latestVersion, err := r.getLatestVersionOfReplicaSet(ctx, t.Namespace, finalizerFullName) + if err != nil { + r.Logger.Error(err, "unable to get latest version of replicaset") + return nil, err + } + + if latestVersion == nil { + r.Logger.Info("could not get latest version from replicaset, trying to get from secrets") + if secrets, err := r.listSecrets(ctx, t.Namespace, labels); err != nil { + return nil, err + } else { + for _, c := range secrets.Items { + if val, ok := c.Labels[r.Labels.AppVersionLabel]; ok { + ver, err := goversion.NewVersion(val) + if err != nil { + r.Logger.Error(err, "unable to parse version") + return nil, err + } + if latestVersion == nil || ver.GreaterThan(latestVersion) { + latestVersion = ver + } + } + } + } + } + return latestVersion, nil + default: + return nil, fmt.Errorf("unsupported type: %T", T) + } +} diff --git a/internal/tools/tools.go b/internal/tools/tools.go index ff94ee1..09d0df2 100644 --- a/internal/tools/tools.go +++ b/internal/tools/tools.go @@ -30,3 +30,11 @@ func CreateMapFromStringList(list []string) map[string]bool { } return result } + +func CopyMap(m map[string]string) map[string]string { + result := make(map[string]string) + for k, v := range m { + result[k] = v + } + return result +}