From ecfec33ec3eddb22329b9ff14bde6249610b2387 Mon Sep 17 00:00:00 2001 From: jwcesign Date: Fri, 28 Oct 2022 17:07:57 +0800 Subject: [PATCH] Add label "namespace.karmada.io/skip-auto-propagation" to control whether to propagate ns to member clusters Signed-off-by: jwcesign --- pkg/apis/policy/v1alpha1/well_known_constants.go | 10 ++++++++++ .../namespace/namespace_sync_controller.go | 11 +++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/pkg/apis/policy/v1alpha1/well_known_constants.go b/pkg/apis/policy/v1alpha1/well_known_constants.go index d1c186b10..fd55be569 100644 --- a/pkg/apis/policy/v1alpha1/well_known_constants.go +++ b/pkg/apis/policy/v1alpha1/well_known_constants.go @@ -9,4 +9,14 @@ const ( // ClusterPropagationPolicyLabel is added to objects to specify associated ClusterPropagationPolicy. ClusterPropagationPolicyLabel = "clusterpropagationpolicy.karmada.io/name" + + // NamespaceSkipAutoPropagationLabel is added to namespace objects to indicate if + // the namespace should be skipped from propagating by the namespace controller. + // For example, a namespace with the following label will be skipped: + // labels: + // namespace.karmada.io/skip-auto-propagation: "true" + // + // NOTE: If create a ns without this label, then patch it with this label, the ns will not be + // synced to new member clusters, but old member clusters still have it. + NamespaceSkipAutoPropagationLabel = "namespace.karmada.io/skip-auto-propagation" ) diff --git a/pkg/controllers/namespace/namespace_sync_controller.go b/pkg/controllers/namespace/namespace_sync_controller.go index ec5a69211..af37dc39d 100644 --- a/pkg/controllers/namespace/namespace_sync_controller.go +++ b/pkg/controllers/namespace/namespace_sync_controller.go @@ -2,6 +2,7 @@ package namespace import ( "context" + "strings" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -52,7 +53,7 @@ func (c *Controller) Reconcile(ctx context.Context, req controllerruntime.Reques } namespace := &corev1.Namespace{} - if err := c.Client.Get(context.TODO(), req.NamespacedName, namespace); err != nil { + if err := c.Client.Get(ctx, req.NamespacedName, namespace); err != nil { // The resource may no longer exist, in which case we stop processing. if apierrors.IsNotFound(err) { return controllerruntime.Result{}, nil @@ -67,8 +68,14 @@ func (c *Controller) Reconcile(ctx context.Context, req controllerruntime.Reques return controllerruntime.Result{}, nil } + skipAutoPropagation := util.GetLabelValue(namespace.Labels, policyv1alpha1.NamespaceSkipAutoPropagationLabel) + if strings.ToLower(skipAutoPropagation) == "true" { + klog.Infof("Skip auto propagation namespace:%s", namespace.Name) + return controllerruntime.Result{}, nil + } + clusterList := &clusterv1alpha1.ClusterList{} - if err := c.Client.List(context.TODO(), clusterList); err != nil { + if err := c.Client.List(ctx, clusterList); err != nil { klog.Errorf("Failed to list clusters, error: %v", err) return controllerruntime.Result{Requeue: true}, err }