Skip to content

Commit

Permalink
feat(sourceNamespace): Glob -> Regex2 Library
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur <[email protected]>
  • Loading branch information
ArthurVardevanyan committed Jul 10, 2024
1 parent de76937 commit 201d849
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 8 deletions.
3 changes: 2 additions & 1 deletion controller/appcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import (
argodiff "github.com/argoproj/argo-cd/v2/util/argo/diff"
"github.com/argoproj/argo-cd/v2/util/argo/normalizers"
"github.com/argoproj/argo-cd/v2/util/env"
"github.com/argoproj/argo-cd/v2/util/regex"
"github.com/argoproj/argo-cd/v2/util/stats"

kubeerrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -2087,7 +2088,7 @@ func (ctrl *ApplicationController) shouldSelfHeal(app *appv1.Application) (bool,
// isAppNamespaceAllowed returns whether the application is allowed in the
// namespace it's residing in.
func (ctrl *ApplicationController) isAppNamespaceAllowed(app *appv1.Application) bool {
return app.Namespace == ctrl.namespace || glob.MatchStringInList(ctrl.applicationNamespaces, app.Namespace, false)
return app.Namespace == ctrl.namespace || regex.MatchStringInList(ctrl.applicationNamespaces, app.Namespace, false)
}

func (ctrl *ApplicationController) canProcessApp(obj interface{}) bool {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dlclark/regexp2 v1.11.1
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/evanphx/json-patch/v5 v5.8.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,8 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/distribution/reference v0.5.0 h1:/FUIFXtfc/x2gpa5/VGfiGLuOIdYa1t65IKK2OFGvA0=
github.com/distribution/reference v0.5.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
github.com/dlclark/regexp2 v1.11.1 h1:CJs78ewKXO9PuNf6Xwlw6eibMadBkXTRpOeUdv+IcWM=
github.com/dlclark/regexp2 v1.11.1/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dnaeon/go-vcr v1.1.0 h1:ReYa/UBrRyQdant9B4fNHGoCNKw6qh6P0fsdGmZpR7c=
github.com/dnaeon/go-vcr v1.1.0/go.mod h1:M7tiix8f0r6mKKJ3Yq/kqU1OYf3MnfmBWVbPx/yU9ko=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
Expand Down
7 changes: 3 additions & 4 deletions notification_controller/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"fmt"
"time"

"github.com/argoproj/argo-cd/v2/util/glob"

"github.com/argoproj/argo-cd/v2/util/notification/k8s"

service "github.com/argoproj/argo-cd/v2/util/notification/argocd"
Expand All @@ -18,6 +16,7 @@ import (

"github.com/argoproj/argo-cd/v2/util/notification/settings"

"github.com/argoproj/argo-cd/v2/util/regex"
"github.com/argoproj/notifications-engine/pkg/api"
"github.com/argoproj/notifications-engine/pkg/controller"
"github.com/argoproj/notifications-engine/pkg/services"
Expand Down Expand Up @@ -122,7 +121,7 @@ func NewController(

// 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)
return namespace != app.GetNamespace() && !regex.MatchStringInList(applicationNamespaces, app.GetNamespace(), false)
}

func (c *notificationController) alterDestinations(obj v1.Object, destinations services.Destinations, cfg api.Config) services.Destinations {
Expand Down Expand Up @@ -151,7 +150,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 controllerNamespace == res.GetNamespace() || regex.MatchStringInList(applicationNamespaces, res.GetNamespace(), false) {
newItems = append(newItems, res)
}
}
Expand Down
4 changes: 3 additions & 1 deletion util/glob/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package glob
// exactMatch is set to false, list may contain globs to be matched.
func MatchStringInList(list []string, item string, exactMatch bool) bool {
for _, ll := range list {
if item == ll || (!exactMatch && Match(ll, item)) {
if item == ll {
return true
} else if !exactMatch && Match(ll, item) {
return true
}
}
Expand Down
14 changes: 14 additions & 0 deletions util/regex/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package regex

// MatchStringInList will return true if item is contained in list. If
// exactMatch is set to false, list may contain globs to be matched.
func MatchStringInList(list []string, item string, exactMatch bool) bool {
for _, ll := range list {
if item == ll || (!exactMatch && Match(ll, item)) {
return true
} else {

}
}
return false
}
20 changes: 20 additions & 0 deletions util/regex/regex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package regex

import (
"github.com/dlclark/regexp2"
log "github.com/sirupsen/logrus"
)

func Match(pattern, text string) bool {
compiledRegex, err := regexp2.Compile(pattern, 0)
if err != nil {
log.Warnf("failed to compile pattern %s due to error %v", pattern, err)
return false
}
regexMatch, err := compiledRegex.MatchString(text)
if err != nil {
log.Warnf("failed to match pattern %s due to error %v", pattern, err)
return false
}
return regexMatch
}
4 changes: 2 additions & 2 deletions util/security/application_namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package security
import (
"fmt"

"github.com/argoproj/argo-cd/v2/util/glob"
"github.com/argoproj/argo-cd/v2/util/regex"
)

func IsNamespaceEnabled(namespace string, serverNamespace string, enabledNamespaces []string) bool {
return namespace == serverNamespace || glob.MatchStringInList(enabledNamespaces, namespace, false)
return namespace == serverNamespace || regex.MatchStringInList(enabledNamespaces, namespace, false)
}

func NamespaceNotPermittedError(namespace string) error {
Expand Down

0 comments on commit 201d849

Please sign in to comment.