Skip to content

Commit

Permalink
Merge pull request #8 from run-ai/ignoreExtraneousOperation
Browse files Browse the repository at this point in the history
fix: adding check for newer config version if replicaset not exists f…
  • Loading branch information
yossig-runai authored Sep 30, 2024
2 parents 1a4fe03 + ed6d2af commit bc5e531
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
77 changes: 73 additions & 4 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
8 changes: 8 additions & 0 deletions internal/tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit bc5e531

Please sign in to comment.