diff --git a/argocd-operator/controllers/argocd/namespaceManagement_events_test.go b/argocd-operator/controllers/argocd/namespaceManagement_events_test.go new file mode 100644 index 00000000000..53645bae17a --- /dev/null +++ b/argocd-operator/controllers/argocd/namespaceManagement_events_test.go @@ -0,0 +1,148 @@ +package argocd + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + corev1 "k8s.io/api/core/v1" + rbacv1 "k8s.io/api/rbac/v1" + "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/types" + testclient "k8s.io/client-go/kubernetes/fake" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/event" + + argoproj "github.com/redhat-developer/gitops-operator/argocd-operator/api/v1beta1" + "github.com/redhat-developer/gitops-operator/argocd-operator/common" +) + +// Withdrawing consent (deleting or retargeting a NamespaceManagement CR) must enqueue the +// instance that held it, even when the predicate skips or fails its own cleanup, so the +// reconcile can prune what that instance was granted. +func TestNamespaceManagementPredicateEnqueuesOnConsentWithdrawal(t *testing.T) { + const ( + manager = "manager" + target = "target" + ) + nm := func(managedBy string) *argoproj.NamespaceManagement { + return &argoproj.NamespaceManagement{ + ObjectMeta: metav1.ObjectMeta{Name: "nm", Namespace: target}, + Spec: argoproj.NamespaceManagementSpec{ManagedBy: managedBy}, + } + } + selfLabelled := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{ + Name: manager, + Labels: map[string]string{common.ArgoCDManagedByLabel: manager}, + }} + + tests := []struct { + name string + // old is nil for a delete event. + old, cur *argoproj.NamespaceManagement + namespaces []client.Object + wantEnqueue bool + }{ + { + name: "delete enqueues when the managing namespace carries a matching managed-by label", + cur: nm(manager), + namespaces: []client.Object{selfLabelled}, + wantEnqueue: true, + }, + { + name: "delete enqueues when the managing namespace cannot be read", + cur: nm(manager), + wantEnqueue: true, + }, + { + name: "delete without .spec.managedBy has no instance to enqueue", + cur: nm(""), + wantEnqueue: false, + }, + { + name: "retarget enqueues when the managing namespace carries a matching managed-by label", + old: nm(manager), + cur: nm("other"), + namespaces: []client.Object{selfLabelled}, + wantEnqueue: true, + }, + { + name: "retarget enqueues when the managing namespace cannot be read", + old: nm(manager), + cur: nm("other"), + wantEnqueue: true, + }, + { + // Our own reconcile writes the CR status; enqueuing it would loop. + name: "status-only update is not enqueued", + old: nm(manager), + cur: nm(manager), + namespaces: []client.Object{&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: manager}}}, + wantEnqueue: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + sch := makeTestReconcilerScheme(argoproj.AddToScheme) + cl := makeTestReconcilerClient(sch, tt.namespaces, nil, []runtime.Object{}) + r := makeTestReconciler(cl, sch, testclient.NewSimpleClientset()) + + pred := r.namespaceManagementFilterPredicate() + var got bool + if tt.old == nil { + got = pred.Delete(event.DeleteEvent{Object: tt.cur}) + } else { + got = pred.Update(event.UpdateEvent{ObjectOld: tt.old, ObjectNew: tt.cur}) + } + assert.Equal(t, tt.wantEnqueue, got) + }) + } +} + +// Once the instance is enqueued after a NamespaceManagement CR is deleted, its reconcile must +// remove the Image Updater manager RBAC that the CR's consent allowed. +func TestImageUpdaterRBACRemovedAfterNamespaceManagementConsentWithdrawn(t *testing.T) { + t.Setenv(common.EnableManagedNamespace, "true") + const target = "team-a" + + a := makeTestArgoCD(func(a *argoproj.ArgoCD) { + a.Spec.ImageUpdater.Enabled = true + a.Spec.ImageUpdater.Env = []corev1.EnvVar{{Name: "IMAGE_UPDATER_WATCH_NAMESPACES", Value: target}} + a.Spec.NamespaceManagement = []argoproj.ManagedNamespaces{{Name: target, AllowManagedBy: true}} + }) + nm := &argoproj.NamespaceManagement{ + ObjectMeta: metav1.ObjectMeta{Name: "nm", Namespace: target}, + Spec: argoproj.NamespaceManagementSpec{ManagedBy: a.Namespace}, + } + ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: target}} + + sch := makeTestReconcilerScheme(argoproj.AddToScheme) + cl := makeTestReconcilerClient(sch, []client.Object{a, nm, ns}, []client.Object{a, nm}, []runtime.Object{}) + r := makeTestReconciler(cl, sch, testclient.NewSimpleClientset()) + sa := &corev1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Name: "sa", Namespace: a.Namespace}} + + // The consent steps of a reconcile, followed by the Image Updater RBAC. + reconcileRBAC := func() { + t.Helper() + require.NoError(t, r.setManagedNamespaces(a)) + require.NoError(t, r.reconcileNamespaceManagement(a)) + ws, expanded := testResolveWatchNamespaces(t, r, a) + _, err := r.reconcileImageUpdaterRBAC(a, sa, ws, expanded) + require.NoError(t, err) + } + roleKey := types.NamespacedName{Name: getRoleNameForApplicationSourceNamespaces(target, a), Namespace: target} + bindingKey := types.NamespacedName{Name: getRoleBindingNameForSourceNamespaces(a.Name, target), Namespace: target} + + reconcileRBAC() + require.NoError(t, r.Get(context.TODO(), roleKey, &rbacv1.Role{}), "consent should grant the manager Role") + require.NoError(t, r.Get(context.TODO(), bindingKey, &rbacv1.RoleBinding{}), "consent should grant the manager RoleBinding") + + require.NoError(t, r.Delete(context.TODO(), nm)) + reconcileRBAC() + assert.True(t, errors.IsNotFound(r.Get(context.TODO(), roleKey, &rbacv1.Role{})), "manager Role must be removed once consent is withdrawn") + assert.True(t, errors.IsNotFound(r.Get(context.TODO(), bindingKey, &rbacv1.RoleBinding{})), "manager RoleBinding must be removed once consent is withdrawn") +} diff --git a/argocd-operator/controllers/argocd/util.go b/argocd-operator/controllers/argocd/util.go index 327ad4641f3..d9feaa907b1 100644 --- a/argocd-operator/controllers/argocd/util.go +++ b/argocd-operator/controllers/argocd/util.go @@ -2067,40 +2067,36 @@ func (r *ReconcileArgoCD) handleArgoCDNamespaceManagementUpdate(valNew, valOld * return true } +// handleNamespaceManagementUpdate cleans up after a NamespaceManagement CR is pointed at another +// instance and reports whether the event must be enqueued. It is enqueued whenever .spec.managedBy +// changes, even if the cleanup here is skipped or fails: the reconcile rebuilds consent and prunes +// the grants the previous instance no longer has (e.g. Image Updater RBAC). func (r *ReconcileArgoCD) handleNamespaceManagementUpdate(oldNSMgmt, newNSMgmt *argoproj.NamespaceManagement, k8sClient kubernetes.Interface) bool { + // Status-only updates come from our own reconcile; enqueuing them would loop. + if oldNSMgmt.Spec.ManagedBy == newNSMgmt.Spec.ManagedBy { + return false + } + ns := &corev1.Namespace{} if err := r.Get(context.TODO(), types.NamespacedName{Name: oldNSMgmt.Spec.ManagedBy}, ns); err != nil { - return false + return true } // Skip Update if managed-by label exists and matches .spec.managedBy if isNamespaceLabelledManagedBy(ns, oldNSMgmt.Spec.ManagedBy) { log.Info(fmt.Sprintf("Namespace %s still managed by same ArgoCD instance via label, skipping update cleanup", oldNSMgmt.Namespace)) - return false - } - - // If `.spec.managedBy` changes, trigger reconciliation - if oldNSMgmt.Spec.ManagedBy != newNSMgmt.Spec.ManagedBy { - if err := cleanupRBACsForNamespaceManagement(oldNSMgmt.Spec.ManagedBy, oldNSMgmt.Namespace, k8sClient); err != nil { - return false - } return true } - return false + + _ = cleanupRBACsForNamespaceManagement(oldNSMgmt.Spec.ManagedBy, oldNSMgmt.Namespace, k8sClient) // logged inside + return true } +// handleNamespaceManagementDelete cleans up after a NamespaceManagement CR is deleted and reports +// whether the event must be enqueued. It is enqueued whenever the CR named a managing instance, +// even if the cleanup here is skipped or fails, so that instance's reconcile prunes the grants +// that lost their consent. func (r *ReconcileArgoCD) handleNamespaceManagementDelete(nsMgmt *argoproj.NamespaceManagement, k8sClient kubernetes.Interface) bool { - ns := &corev1.Namespace{} - if err := r.Get(context.TODO(), types.NamespacedName{Name: nsMgmt.Spec.ManagedBy}, ns); err != nil { - return false - } - - // Skip cleanup if managed-by label exists and matches .spec.managedBy - if isNamespaceLabelledManagedBy(ns, nsMgmt.Spec.ManagedBy) { - log.Info(fmt.Sprintf("Namespace %s still managed by same ArgoCD instance via label, skipping delete cleanup", nsMgmt.Namespace)) - return false - } - // Retrieve the ArgoCD instance that was managing this namespace argocdNamespace := nsMgmt.Spec.ManagedBy if argocdNamespace == "" { @@ -2109,10 +2105,19 @@ func (r *ReconcileArgoCD) handleNamespaceManagementDelete(nsMgmt *argoproj.Names return false } - if err := cleanupRBACsForNamespaceManagement(argocdNamespace, nsMgmt.Namespace, k8sClient); err != nil { - return false + ns := &corev1.Namespace{} + if err := r.Get(context.TODO(), types.NamespacedName{Name: nsMgmt.Spec.ManagedBy}, ns); err != nil { + return true } - return false + + // Skip cleanup if managed-by label exists and matches .spec.managedBy + if isNamespaceLabelledManagedBy(ns, nsMgmt.Spec.ManagedBy) { + log.Info(fmt.Sprintf("Namespace %s still managed by same ArgoCD instance via label, skipping delete cleanup", nsMgmt.Namespace)) + return true + } + + _ = cleanupRBACsForNamespaceManagement(argocdNamespace, nsMgmt.Namespace, k8sClient) // logged inside + return true } func cleanupRBACsForNamespaceManagement(argocdNamespace, nms string, k8sClient kubernetes.Interface) error { diff --git a/argocd-operator/controllers/argocd/util_test.go b/argocd-operator/controllers/argocd/util_test.go index d5d5c974ed2..a4e7c14e328 100644 --- a/argocd-operator/controllers/argocd/util_test.go +++ b/argocd-operator/controllers/argocd/util_test.go @@ -1783,7 +1783,7 @@ func TestNamespaceManagementHandlers(t *testing.T) { secretName := setupRBACAndSecret(t, client, argoCD, testNamespace) changed := r.handleNamespaceManagementDelete(nsMgmt, client) - assert.False(t, changed) + assert.True(t, changed, "deleting the CR must enqueue the instance it named") _, err := client.RbacV1().Roles(testNamespace).Get(context.TODO(), "test-role", metav1.GetOptions{}) assert.ErrorContains(t, err, "not found")