Skip to content

Commit

Permalink
fix: after upgrade from <=0.7 to newer version ownerReferences in con…
Browse files Browse the repository at this point in the history
…figmaps is not updated (#1542)

Signed-off-by: Jayendra Parsai <jparsai@jparsai-thinkpadp1gen4i.remote.csb>
Co-authored-by: Jayendra Parsai <jparsai@jparsai-thinkpadp1gen4i.remote.csb>
  • Loading branch information
jparsai and Jayendra Parsai authored Sep 27, 2024
1 parent 603adda commit 6ece288
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
62 changes: 62 additions & 0 deletions controllers/argocd/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

argoproj "github.com/argoproj-labs/argocd-operator/api/v1beta1"
Expand Down Expand Up @@ -464,8 +466,24 @@ func (r *ReconcileArgoCD) reconcileArgoConfigMap(cr *argoproj.ArgoCD) error {
cm.Data[common.ArgoCDKeyOIDCConfig] = existingCM.Data[common.ArgoCDKeyOIDCConfig]
}

changed := false
if !reflect.DeepEqual(cm.Data, existingCM.Data) {
existingCM.Data = cm.Data
changed = true
}

// Compare OwnerReferences
var refChanged bool
var err error
if refChanged, err = validateOwnerReferences(cr, existingCM, r.Scheme); err != nil {
return err
}

if refChanged {
changed = true
}

if changed {
return r.Client.Update(context.TODO(), existingCM)
}
return nil // Do nothing as there is no change in the configmap.
Expand Down Expand Up @@ -541,6 +559,50 @@ func (r *ReconcileArgoCD) reconcileRBACConfigMap(cm *corev1.ConfigMap, cr *argop
return nil // ConfigMap exists and nothing to do, move along...
}

// validateOwnerReferences checks if OwnerReferences is changed
func validateOwnerReferences(cr *argoproj.ArgoCD, cm *corev1.ConfigMap, scheme *runtime.Scheme) (bool, error) {
changed := false

if cm.OwnerReferences != nil {
ref := cm.OwnerReferences[0]

gvk, err := apiutil.GVKForObject(cr, scheme)
if err != nil {
return false, err
}

if ref.APIVersion != gvk.GroupVersion().String() {
cm.OwnerReferences[0].APIVersion = gvk.GroupVersion().String()
changed = true
}

if ref.Kind != gvk.Kind {
cm.OwnerReferences[0].Kind = gvk.Kind
changed = true
}

if ref.UID != cr.GetUID() {
cm.OwnerReferences[0].UID = cr.GetUID()
changed = true
}

if ref.Name != cr.GetName() {
cm.OwnerReferences[0].Name = cr.GetName()
changed = true
}
return changed, nil

}

if cm.OwnerReferences == nil {
if err := controllerutil.SetControllerReference(cr, cm, scheme); err != nil {
return false, err
}
}

return true, nil
}

// reconcileRedisConfiguration will ensure that all of the Redis ConfigMaps are present for the given ArgoCD.
func (r *ReconcileArgoCD) reconcileRedisConfiguration(cr *argoproj.ArgoCD, useTLSForRedis bool) error {
if err := r.reconcileRedisHAConfigMap(cr, useTLSForRedis); err != nil {
Expand Down
67 changes: 67 additions & 0 deletions controllers/argocd/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/uuid"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand Down Expand Up @@ -1040,3 +1041,69 @@ func Test_reconcileRBAC(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, cm.Data["policy.matchMode"], matcherMode)
}

func Test_validateOwnerReferences(t *testing.T) {
a := makeTestArgoCD()
uid := uuid.NewUUID()
a.UID = uid
resObjs := []client.Object{a}
subresObjs := []client.Object{a}
runtimeObjs := []runtime.Object{}
sch := makeTestReconcilerScheme(argoproj.AddToScheme)
cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs)
r := makeTestReconciler(cl, sch)
cm := newConfigMapWithName(common.ArgoCDConfigMapName, a)

// verify when OwnerReferences is not set
_, err := validateOwnerReferences(a, cm, r.Scheme)
assert.NoError(t, err)

assert.Equal(t, cm.OwnerReferences[0].APIVersion, "argoproj.io/v1beta1")
assert.Equal(t, cm.OwnerReferences[0].Kind, "ArgoCD")
assert.Equal(t, cm.OwnerReferences[0].Name, "argocd")
assert.Equal(t, cm.OwnerReferences[0].UID, uid)

// verify when APIVersion is changed
cm.OwnerReferences[0].APIVersion = "test"

changed, err := validateOwnerReferences(a, cm, r.Scheme)
assert.NoError(t, err)
assert.True(t, changed)
assert.Equal(t, cm.OwnerReferences[0].APIVersion, "argoproj.io/v1beta1")
assert.Equal(t, cm.OwnerReferences[0].Kind, "ArgoCD")
assert.Equal(t, cm.OwnerReferences[0].Name, "argocd")
assert.Equal(t, cm.OwnerReferences[0].UID, uid)

// verify when Kind is changed
cm.OwnerReferences[0].Kind = "test"

changed, err = validateOwnerReferences(a, cm, r.Scheme)
assert.NoError(t, err)
assert.True(t, changed)
assert.Equal(t, cm.OwnerReferences[0].APIVersion, "argoproj.io/v1beta1")
assert.Equal(t, cm.OwnerReferences[0].Kind, "ArgoCD")
assert.Equal(t, cm.OwnerReferences[0].Name, "argocd")
assert.Equal(t, cm.OwnerReferences[0].UID, uid)

// verify when Kind is changed
cm.OwnerReferences[0].Name = "test"

changed, err = validateOwnerReferences(a, cm, r.Scheme)
assert.NoError(t, err)
assert.True(t, changed)
assert.Equal(t, cm.OwnerReferences[0].APIVersion, "argoproj.io/v1beta1")
assert.Equal(t, cm.OwnerReferences[0].Kind, "ArgoCD")
assert.Equal(t, cm.OwnerReferences[0].Name, "argocd")
assert.Equal(t, cm.OwnerReferences[0].UID, uid)

// verify when UID is changed
cm.OwnerReferences[0].UID = "test"

changed, err = validateOwnerReferences(a, cm, r.Scheme)
assert.NoError(t, err)
assert.True(t, changed)
assert.Equal(t, cm.OwnerReferences[0].APIVersion, "argoproj.io/v1beta1")
assert.Equal(t, cm.OwnerReferences[0].Kind, "ArgoCD")
assert.Equal(t, cm.OwnerReferences[0].Name, "argocd")
assert.Equal(t, cm.OwnerReferences[0].UID, uid)
}

0 comments on commit 6ece288

Please sign in to comment.