From 8b701db901f5b7e5733a22396a01bd43108b75c9 Mon Sep 17 00:00:00 2001 From: chaunceyjiang Date: Fri, 7 Apr 2023 14:27:28 +0800 Subject: [PATCH] Allows setting wildcards for SkippedPropagatingNamespaces Signed-off-by: chaunceyjiang --- cmd/controller-manager/app/controllermanager.go | 13 ++----------- cmd/controller-manager/app/options/validation.go | 9 ++++++++- .../namespace/namespace_sync_controller.go | 9 ++++++--- pkg/detector/detector.go | 9 ++++++--- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/cmd/controller-manager/app/controllermanager.go b/cmd/controller-manager/app/controllermanager.go index 08b9df6cdc69..ed14fb5af8a4 100644 --- a/cmd/controller-manager/app/controllermanager.go +++ b/cmd/controller-manager/app/controllermanager.go @@ -411,14 +411,10 @@ func startWorkStatusController(ctx controllerscontext.Context) (enabled bool, er } func startNamespaceController(ctx controllerscontext.Context) (enabled bool, err error) { - skippedPropagatingNamespaces := map[string]struct{}{} - for _, ns := range ctx.Opts.SkippedPropagatingNamespaces { - skippedPropagatingNamespaces[ns] = struct{}{} - } namespaceSyncController := &namespace.Controller{ Client: ctx.Mgr.GetClient(), EventRecorder: ctx.Mgr.GetEventRecorderFor(namespace.ControllerName), - SkippedPropagatingNamespaces: skippedPropagatingNamespaces, + SkippedPropagatingNamespaces: ctx.Opts.SkippedPropagatingNamespaces, OverrideManager: ctx.OverrideManager, } if err := namespaceSyncController.SetupWithManager(ctx.Mgr); err != nil { @@ -543,11 +539,6 @@ func setupControllers(mgr controllerruntime.Manager, opts *options.Options, stop return } - skippedPropagatingNamespaces := map[string]struct{}{} - for _, ns := range opts.SkippedPropagatingNamespaces { - skippedPropagatingNamespaces[ns] = struct{}{} - } - controlPlaneInformerManager := genericmanager.NewSingleClusterInformerManager(dynamicClientSet, 0, stopChan) resourceInterpreter := resourceinterpreter.NewResourceInterpreter(controlPlaneInformerManager) @@ -564,7 +555,7 @@ func setupControllers(mgr controllerruntime.Manager, opts *options.Options, stop RESTMapper: mgr.GetRESTMapper(), DynamicClient: dynamicClientSet, SkippedResourceConfig: skippedResourceConfig, - SkippedPropagatingNamespaces: skippedPropagatingNamespaces, + SkippedPropagatingNamespaces: opts.SkippedPropagatingNamespaces, ResourceInterpreter: resourceInterpreter, EventRecorder: mgr.GetEventRecorderFor("resource-detector"), ConcurrentResourceTemplateSyncs: opts.ConcurrentResourceTemplateSyncs, diff --git a/cmd/controller-manager/app/options/validation.go b/cmd/controller-manager/app/options/validation.go index a9190b1ebaf4..4e58db83d9cc 100644 --- a/cmd/controller-manager/app/options/validation.go +++ b/cmd/controller-manager/app/options/validation.go @@ -1,6 +1,9 @@ package options import ( + "fmt" + "regexp" + "k8s.io/apimachinery/pkg/util/validation/field" "github.com/karmada-io/karmada/pkg/util" @@ -33,6 +36,10 @@ func (o *Options) Validate() field.ErrorList { if o.ClusterStartupGracePeriod.Duration <= 0 { errs = append(errs, field.Invalid(newPath.Child("ClusterStartupGracePeriod"), o.ClusterStartupGracePeriod, "must be greater than 0")) } - + for index, ns := range o.SkippedPropagatingNamespaces { + if _, err := regexp.Compile(fmt.Sprintf("^%s$", ns)); err != nil { + errs = append(errs, field.Invalid(newPath.Child("SkippedPropagatingNamespaces").Index(index), o.SkippedPropagatingNamespaces, "Invalid namespace string")) + } + } return errs } diff --git a/pkg/controllers/namespace/namespace_sync_controller.go b/pkg/controllers/namespace/namespace_sync_controller.go index 35b55d4cae02..1acbadd22cb6 100644 --- a/pkg/controllers/namespace/namespace_sync_controller.go +++ b/pkg/controllers/namespace/namespace_sync_controller.go @@ -3,6 +3,7 @@ package namespace import ( "context" "fmt" + "regexp" "strings" corev1 "k8s.io/api/core/v1" @@ -41,7 +42,7 @@ const ( type Controller struct { client.Client // used to operate Work resources. EventRecorder record.EventRecorder - SkippedPropagatingNamespaces map[string]struct{} + SkippedPropagatingNamespaces []string OverrideManager overridemanager.OverrideManager } @@ -96,8 +97,10 @@ func (c *Controller) namespaceShouldBeSynced(namespace string) bool { return false } - if _, ok := c.SkippedPropagatingNamespaces[namespace]; ok { - return false + for _, ns := range c.SkippedPropagatingNamespaces { + if match, _ := regexp.MatchString(fmt.Sprintf("^%s$", ns), namespace); match { + return false + } } return true } diff --git a/pkg/detector/detector.go b/pkg/detector/detector.go index cbd06c2f8fac..17645c767e10 100644 --- a/pkg/detector/detector.go +++ b/pkg/detector/detector.go @@ -3,6 +3,7 @@ package detector import ( "context" "fmt" + "regexp" "sync" "time" @@ -55,7 +56,7 @@ type ResourceDetector struct { EventHandler cache.ResourceEventHandler Processor util.AsyncWorker SkippedResourceConfig *util.SkippedResourceConfig - SkippedPropagatingNamespaces map[string]struct{} + SkippedPropagatingNamespaces []string // ResourceInterpreter knows the details of resource structure. ResourceInterpreter resourceinterpreter.ResourceInterpreter EventRecorder record.EventRecorder @@ -253,8 +254,10 @@ func (d *ResourceDetector) EventFilter(obj interface{}) bool { } // if SkippedPropagatingNamespaces is set, skip object events in these namespaces. - if _, ok := d.SkippedPropagatingNamespaces[clusterWideKey.Namespace]; ok { - return false + for _, ns := range d.SkippedPropagatingNamespaces { + if match, _ := regexp.MatchString(fmt.Sprintf("^%s$", ns), clusterWideKey.Namespace); match { + return false + } } if unstructObj, ok := obj.(*unstructured.Unstructured); ok {