Skip to content

Commit

Permalink
chore(notifications): Refactor: invert function to match to applicati…
Browse files Browse the repository at this point in the history
…on controller's implementation

Signed-off-by: motoki317 <motoki317@gmail.com>
  • Loading branch information
motoki317 committed Oct 30, 2023
1 parent c2f1df0 commit 8bd9b51
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
11 changes: 6 additions & 5 deletions notification_controller/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func NewController(
if !ok {
return false, ""
}
if checkAppNotInAdditionalNamespaces(app, namespace, applicationNamespaces) {
if !isAppNamespaceAllowed(app, namespace, applicationNamespaces) {
return true, "app is not in one of the application-namespaces, nor the notification controller namespace"
}
return !isAppSyncStatusRefreshed(app, log.WithField("app", obj.GetName())), "sync status out of date"
Expand All @@ -99,9 +99,10 @@ func NewController(
return res
}

// Check if app is not in the namespace where the controller is in, and also app is not in one of the applicationNamespaces
func checkAppNotInAdditionalNamespaces(app *unstructured.Unstructured, namespace string, applicationNamespaces []string) bool {
return namespace != app.GetNamespace() && !glob.MatchStringInList(applicationNamespaces, app.GetNamespace(), false)
// isAppNamespaceAllowed returns whether app is in the namespace where the controller is in,
// or app is in one of the applicationNamespaces.
func isAppNamespaceAllowed(app *unstructured.Unstructured, namespace string, applicationNamespaces []string) bool {
return namespace == app.GetNamespace() || glob.MatchStringInList(applicationNamespaces, app.GetNamespace(), false)
}

func (c *notificationController) alterDestinations(obj v1.Object, destinations services.Destinations, cfg api.Config) services.Destinations {
Expand Down Expand Up @@ -130,7 +131,7 @@ func newInformer(resClient dynamic.ResourceInterface, controllerNamespace string
}
newItems := []unstructured.Unstructured{}
for _, res := range appList.Items {
if controllerNamespace == res.GetNamespace() || glob.MatchStringInList(applicationNamespaces, res.GetNamespace(), false) {
if isAppNamespaceAllowed(&res, controllerNamespace, applicationNamespaces) {
newItems = append(newItems, res)
}
}
Expand Down
8 changes: 4 additions & 4 deletions notification_controller/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func TestInitTimeout(t *testing.T) {
assert.Equal(t, "Timed out waiting for caches to sync", err.Error())
}

func TestCheckAppNotInAdditionalNamespaces(t *testing.T) {
func TestIsAppNamespaceAllowed(t *testing.T) {
app := &unstructured.Unstructured{
Object: map[string]interface{}{
"spec": map[string]interface{}{},
Expand All @@ -180,13 +180,13 @@ func TestCheckAppNotInAdditionalNamespaces(t *testing.T) {

// app is in same namespace as controller's namespace
app.SetNamespace(namespace)
assert.False(t, checkAppNotInAdditionalNamespaces(app, namespace, applicationNamespaces))
assert.True(t, isAppNamespaceAllowed(app, namespace, applicationNamespaces))

// app is not in the namespace as controller's namespace, but it is in one of the applicationNamespaces
app.SetNamespace("namespace2")
assert.False(t, checkAppNotInAdditionalNamespaces(app, "", applicationNamespaces))
assert.True(t, isAppNamespaceAllowed(app, "", applicationNamespaces))

// app is not in the namespace as controller's namespace, and it is not in any of the applicationNamespaces
app.SetNamespace("namespace3")
assert.True(t, checkAppNotInAdditionalNamespaces(app, "", applicationNamespaces))
assert.False(t, isAppNamespaceAllowed(app, "", applicationNamespaces))
}

0 comments on commit 8bd9b51

Please sign in to comment.