Skip to content

Commit bd75510

Browse files
authoredJan 17, 2025··
fix(appset): events not honouring configured namespaces (#21219) (#21241) (#21519)
* fix: 21219 Honour ARGOCD_APPLICATIONSET_CONTROLLER_NAMESPACES for all ApplicationSet events Namespace filtering is applied to Update, Delete and Generic events. Fixes #21219 * fix: 21219 Add tests for ignoreNotAllowedNamespaces * fix: 21219 Remove redundant package import --------- Signed-off-by: eadred <eadred77@googlemail.com>

File tree

2 files changed

+86
-5
lines changed

2 files changed

+86
-5
lines changed
 

‎applicationset/controllers/applicationset_controller.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -525,11 +525,9 @@ func (r *ApplicationSetReconciler) getMinRequeueAfter(applicationSetInfo *argov1
525525
}
526526

527527
func ignoreNotAllowedNamespaces(namespaces []string) predicate.Predicate {
528-
return predicate.Funcs{
529-
CreateFunc: func(e event.CreateEvent) bool {
530-
return utils.IsNamespaceAllowed(namespaces, e.Object.GetNamespace())
531-
},
532-
}
528+
return predicate.NewPredicateFuncs(func(object client.Object) bool {
529+
return utils.IsNamespaceAllowed(namespaces, object.GetNamespace())
530+
})
533531
}
534532

535533
func appControllerIndexer(rawObj client.Object) []string {

‎applicationset/controllers/applicationset_controller_test.go

+83
Original file line numberDiff line numberDiff line change
@@ -6657,3 +6657,86 @@ func TestMigrateStatus(t *testing.T) {
66576657
})
66586658
}
66596659
}
6660+
6661+
func TestIgnoreNotAllowedNamespaces(t *testing.T) {
6662+
tests := []struct {
6663+
name string
6664+
namespaces []string
6665+
objectNS string
6666+
expected bool
6667+
}{
6668+
{
6669+
name: "Namespace allowed",
6670+
namespaces: []string{"allowed-namespace"},
6671+
objectNS: "allowed-namespace",
6672+
expected: true,
6673+
},
6674+
{
6675+
name: "Namespace not allowed",
6676+
namespaces: []string{"allowed-namespace"},
6677+
objectNS: "not-allowed-namespace",
6678+
expected: false,
6679+
},
6680+
{
6681+
name: "Empty allowed namespaces",
6682+
namespaces: []string{},
6683+
objectNS: "any-namespace",
6684+
expected: false,
6685+
},
6686+
{
6687+
name: "Multiple allowed namespaces",
6688+
namespaces: []string{"allowed-namespace-1", "allowed-namespace-2"},
6689+
objectNS: "allowed-namespace-2",
6690+
expected: true,
6691+
},
6692+
{
6693+
name: "Namespace not in multiple allowed namespaces",
6694+
namespaces: []string{"allowed-namespace-1", "allowed-namespace-2"},
6695+
objectNS: "not-allowed-namespace",
6696+
expected: false,
6697+
},
6698+
{
6699+
name: "Namespace matched by glob pattern",
6700+
namespaces: []string{"allowed-namespace-*"},
6701+
objectNS: "allowed-namespace-1",
6702+
expected: true,
6703+
},
6704+
{
6705+
name: "Namespace matched by regex pattern",
6706+
namespaces: []string{"/^allowed-namespace-[^-]+$/"},
6707+
objectNS: "allowed-namespace-1",
6708+
expected: true,
6709+
},
6710+
}
6711+
6712+
for _, tt := range tests {
6713+
t.Run(tt.name, func(t *testing.T) {
6714+
predicate := ignoreNotAllowedNamespaces(tt.namespaces)
6715+
object := &v1alpha1.ApplicationSet{
6716+
ObjectMeta: metav1.ObjectMeta{
6717+
Namespace: tt.objectNS,
6718+
},
6719+
}
6720+
6721+
t.Run(tt.name+":Create", func(t *testing.T) {
6722+
result := predicate.Create(event.CreateEvent{Object: object})
6723+
assert.Equal(t, tt.expected, result)
6724+
})
6725+
6726+
t.Run(tt.name+":Update", func(t *testing.T) {
6727+
result := predicate.Update(event.UpdateEvent{ObjectNew: object})
6728+
assert.Equal(t, tt.expected, result)
6729+
})
6730+
6731+
t.Run(tt.name+":Delete", func(t *testing.T) {
6732+
result := predicate.Delete(event.DeleteEvent{Object: object})
6733+
assert.Equal(t, tt.expected, result)
6734+
})
6735+
6736+
t.Run(tt.name+":Generic", func(t *testing.T) {
6737+
result := predicate.Generic(event.GenericEvent{Object: object})
6738+
assert.Equal(t, tt.expected, result)
6739+
})
6740+
})
6741+
}
6742+
}

0 commit comments

Comments
 (0)
Please sign in to comment.