Merge pull request #2696 from jwcesign/add-ns-sync-labels

Add label "namespace.karmada.io/skip-auto-propagation" to control whether to propagate ns to member clusters
This commit is contained in:
karmada-bot 2022-10-29 14:59:22 +08:00 committed by GitHub
commit 026781f207
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View File

@ -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"
)

View File

@ -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
}