Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion controllers/argocd/argocd_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func (r *ReconcileArgoCD) internalReconcile(ctx context.Context, request ctrl.Re
// SetupWithManager sets up the controller with the Manager.
func (r *ReconcileArgoCD) SetupWithManager(mgr ctrl.Manager) error {
bldr := ctrl.NewControllerManagedBy(mgr)
r.setResourceWatches(bldr, r.clusterResourceMapper, r.tlsSecretMapper, r.namespaceResourceMapper, r.clusterSecretResourceMapper, r.applicationSetSCMTLSConfigMapMapper, r.nmMapper, r.systemCATrustMapper, r.imagePullSecretMapper)
r.setResourceWatches(bldr, r.clusterResourceMapper, r.tlsSecretMapper, r.namespaceResourceMapper, r.clusterSecretResourceMapper, r.applicationSetSCMTLSConfigMapMapper, r.nmMapper, r.systemCATrustMapper, r.imagePullSecretMapper, r.imageUpdaterWatchNSMapper)
return bldr.Complete(r)
}

Expand Down
47 changes: 47 additions & 0 deletions controllers/argocd/custommapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

argoproj "github.com/argoproj-labs/argocd-operator/api/v1beta1"
"github.com/argoproj-labs/argocd-operator/common"
"github.com/argoproj-labs/argocd-operator/controllers/argoutil"

corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -282,6 +283,52 @@ func (r *ReconcileArgoCD) applicationSetSCMTLSConfigMapMapper(ctx context.Contex
return result
}

// imageUpdaterWatchNSMapper maps a Namespace create/delete event to every ArgoCD instance
// whose IMAGE_UPDATER_WATCH_NAMESPACES list of exact namespace names includes that namespace.
// This ensures RBAC and the deployment env var are refreshed immediately when a matching
// namespace appears or disappears, rather than waiting for the next unrelated reconcile
// on the ArgoCD CR itself.
func (r *ReconcileArgoCD) imageUpdaterWatchNSMapper(ctx context.Context, o client.Object) []reconcile.Request {
var result []reconcile.Request

namespaceName := o.GetName()

argocds := &argoproj.ArgoCDList{}
if err := r.List(ctx, argocds, &client.ListOptions{}); err != nil {
return result
}

for i := range argocds.Items {
argocd := &argocds.Items[i]
if !argocd.Spec.ImageUpdater.Enabled {
continue
}
env := argoutil.EnvGet(argocd.Spec.ImageUpdater.Env, "IMAGE_UPDATER_WATCH_NAMESPACES")
if env == nil {
continue
}
watchNS := strings.TrimSpace(env.Value)
// normalizeWatchNamespaces canonicalizes "*," → "*", rejects "*,team-a", etc.
// On error (invalid config) or when the value resolves to a sentinel, skip:
// the main reconcile loop will surface the error.
normalized, err := normalizeWatchNamespaces(watchNS, argocd.Namespace)
if err != nil || normalized == "" || normalized == "*" {
continue
}
patterns := splitAndFilterPatterns(normalized)
if len(patterns) == 0 {
continue
}
if glob.MatchStringInList(patterns, namespaceName, glob.EXACT) {
result = append(result, reconcile.Request{
NamespacedName: client.ObjectKey{Name: argocd.Name, Namespace: argocd.Namespace},
})
}
}

return result
}

// namespaceResourceMapper maps a watch event on a namespaceManagement, back to the
// ArgoCD object that we want to reconcile.
func (r *ReconcileArgoCD) nmMapper(ctx context.Context, o client.Object) []reconcile.Request {
Expand Down
190 changes: 190 additions & 0 deletions controllers/argocd/custommapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1103,3 +1103,193 @@ func TestReconcileArgoCD_nmMapper(t *testing.T) {
})
}
}

func TestReconcileArgoCD_imageUpdaterWatchNSMapper(t *testing.T) {
argocd1 := makeTestArgoCD()
argocd1.Name = "argocd1"
argocd1.Namespace = "argo-ns-1"
argocd1.Spec.ImageUpdater.Enabled = true
argocd1.Spec.ImageUpdater.Env = []corev1.EnvVar{
{Name: "IMAGE_UPDATER_WATCH_NAMESPACES", Value: "app-frontend,app-backend"},
}

argocd2 := makeTestArgoCD()
argocd2.Name = "argocd2"
argocd2.Namespace = "argo-ns-2"
argocd2.Spec.ImageUpdater.Enabled = true
argocd2.Spec.ImageUpdater.Env = []corev1.EnvVar{
{Name: "IMAGE_UPDATER_WATCH_NAMESPACES", Value: "team-alpha"},
}

argocd3 := makeTestArgoCD()
argocd3.Name = "argocd3"
argocd3.Namespace = "argo-ns-3"
argocd3.Spec.ImageUpdater.Enabled = false
argocd3.Spec.ImageUpdater.Env = []corev1.EnvVar{
{Name: "IMAGE_UPDATER_WATCH_NAMESPACES", Value: "app-frontend"},
}

argocd4 := makeTestArgoCD()
argocd4.Name = "argocd4"
argocd4.Namespace = "argo-ns-4"
argocd4.Spec.ImageUpdater.Enabled = true
// No IMAGE_UPDATER_WATCH_NAMESPACES → skip

argocd5 := makeTestArgoCD()
argocd5.Name = "argocd5"
argocd5.Namespace = "argo-ns-5"
argocd5.Spec.ImageUpdater.Enabled = true
argocd5.Spec.ImageUpdater.Env = []corev1.EnvVar{
{Name: "IMAGE_UPDATER_WATCH_NAMESPACES", Value: "*"},
}

for _, a := range []*argoproj.ArgoCD{argocd1, argocd2, argocd3, argocd4, argocd5} {
a.ResourceVersion = ""
}

resObjs := []client.Object{argocd1, argocd2, argocd3, argocd4, argocd5}
subresObjs := []client.Object{argocd1, argocd2, argocd3, argocd4, argocd5}
runtimeObjs := []runtime.Object{}
sch := makeTestReconcilerScheme(argoproj.AddToScheme)
cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs)
r := makeTestReconciler(cl, sch, testclient.NewSimpleClientset())

req1 := reconcile.Request{NamespacedName: types.NamespacedName{Name: "argocd1", Namespace: "argo-ns-1"}}
req2 := reconcile.Request{NamespacedName: types.NamespacedName{Name: "argocd2", Namespace: "argo-ns-2"}}

tests := []struct {
name string
namespace string
want []reconcile.Request
}{
{
name: "exact match triggers reconcile for argocd1",
namespace: "app-frontend",
want: []reconcile.Request{req1},
},
{
name: "non-match triggers no reconcile",
namespace: "other-ns",
want: []reconcile.Request{},
},
{
name: "exact match triggers reconcile for argocd2",
namespace: "team-alpha",
want: []reconcile.Request{req2},
},
{
name: "similarly-named namespace that is not an exact match triggers no reconcile",
namespace: "team-alpha-2",
want: []reconcile.Request{},
},
{
name: "disabled image updater is ignored",
namespace: "app-frontend",
// argocd1 has app-frontend and is enabled; argocd3 also has app-frontend but is disabled
want: []reconcile.Request{req1},
},
{
name: "argocd with no IMAGE_UPDATER_WATCH_NAMESPACES is ignored",
namespace: "any-namespace",
// argocd4 is enabled but has no env var
want: []reconcile.Request{},
},
{
name: "wildcard '*' is skipped (cluster-wide mode, no per-namespace matching)",
namespace: "app-backend",
// argocd1 lists app-backend, argocd5 has '*' which is skipped
want: []reconcile.Request{req1},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: tt.namespace}}
got := r.imageUpdaterWatchNSMapper(context.TODO(), ns)
assert.ElementsMatch(t, tt.want, got)
})
}
}

// TestReconcileArgoCD_imageUpdaterWatchNSMapper_EmptyPatterns verifies that a trailing
// or leading comma in IMAGE_UPDATER_WATCH_NAMESPACES does not produce a match-all empty
// entry, which would otherwise enqueue a reconcile for every namespace in the cluster.
func TestReconcileArgoCD_imageUpdaterWatchNSMapper_EmptyPatterns(t *testing.T) {
tests := []struct {
name string
watchNamespace string
triggerNS string // namespace event that fires the mapper
wantReconcile bool // whether a reconcile request is expected
}{
{
name: "trailing comma: only the listed name matches, not everything",
watchNamespace: "app-frontend,",
triggerNS: "app-frontend",
wantReconcile: true,
},
{
name: "trailing comma: non-matching namespace is still rejected",
watchNamespace: "app-frontend,",
triggerNS: "other-ns",
wantReconcile: false,
},
{
name: "only commas: no valid names → no reconcile for any namespace",
watchNamespace: ",,,",
triggerNS: "app-frontend",
wantReconcile: false,
},
{
name: "leading and trailing commas with valid name",
watchNamespace: ",app-frontend,",
triggerNS: "app-frontend",
wantReconcile: true,
},
{
name: "leading and trailing commas: non-matching namespace is still rejected",
watchNamespace: ",app-frontend,",
triggerNS: "other-ns",
wantReconcile: false,
},
{
name: "sole * with trailing comma is treated as cluster-scoped (skipped by mapper)",
watchNamespace: "*,",
triggerNS: "any-namespace",
wantReconcile: false,
},
{
name: "* mixed with a name is skipped by mapper (invalid config handled by reconciler)",
watchNamespace: "*,team-a",
triggerNS: "team-a",
wantReconcile: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
argocd := makeTestArgoCD()
argocd.Name = "test-argocd"
argocd.Namespace = "test-ns"
argocd.Spec.ImageUpdater.Enabled = true
argocd.Spec.ImageUpdater.Env = []corev1.EnvVar{
{Name: "IMAGE_UPDATER_WATCH_NAMESPACES", Value: tt.watchNamespace},
}
argocd.ResourceVersion = ""

resObjs := []client.Object{argocd}
sch := makeTestReconcilerScheme(argoproj.AddToScheme)
cl := makeTestReconcilerClient(sch, resObjs, resObjs, []runtime.Object{})
r := makeTestReconciler(cl, sch, testclient.NewSimpleClientset())

ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: tt.triggerNS}}
got := r.imageUpdaterWatchNSMapper(context.TODO(), ns)

if tt.wantReconcile {
assert.Len(t, got, 1)
assert.Equal(t, types.NamespacedName{Name: "test-argocd", Namespace: "test-ns"}, got[0].NamespacedName)
} else {
assert.Empty(t, got)
}
})
}
}
Loading
Loading