Skip to content

Commit

Permalink
Deprecate name/namespace/key labels
Browse files Browse the repository at this point in the history
Co-authored-by: changzhen <[email protected]>
Signed-off-by: whitewindmills <[email protected]>
  • Loading branch information
whitewindmills and XiShanYongYe-Chang committed Apr 7, 2024
1 parent b0da31a commit 1469cd6
Show file tree
Hide file tree
Showing 45 changed files with 615 additions and 731 deletions.
30 changes: 20 additions & 10 deletions pkg/apis/policy/v1alpha1/well_known_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.

package v1alpha1

// The well-known label key constant.
const (
// PropagationPolicyPermanentIDLabel is the identifier of a PropagationPolicy object.
// Karmada generates a unique identifier, such as metadata.UUID, for each PropagationPolicy object.
Expand All @@ -31,6 +32,19 @@ const (
// In backup scenarios, when applying the backup resource manifest in a new cluster, the UUID may change.
ClusterPropagationPolicyPermanentIDLabel = "clusterpropagationpolicy.karmada.io/permanent-id"

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

// The well-known annotation key constant.
const (
// PropagationPolicyNamespaceAnnotation is added to objects to specify associated PropagationPolicy namespace.
PropagationPolicyNamespaceAnnotation = "propagationpolicy.karmada.io/namespace"

Expand All @@ -39,23 +53,19 @@ const (

// ClusterPropagationPolicyAnnotation is added to objects to specify associated ClusterPropagationPolicy name.
ClusterPropagationPolicyAnnotation = "clusterpropagationpolicy.karmada.io/name"
)

// TODO(whitewindmills): These deprecated labels will be removed in a future version.
const (
// PropagationPolicyNamespaceLabel is added to objects to specify associated PropagationPolicy namespace.
// Deprecated
PropagationPolicyNamespaceLabel = "propagationpolicy.karmada.io/namespace"

// PropagationPolicyNameLabel is added to objects to specify associated PropagationPolicy's name.
// Deprecated
PropagationPolicyNameLabel = "propagationpolicy.karmada.io/name"

// ClusterPropagationPolicyLabel is added to objects to specify associated ClusterPropagationPolicy.
// Deprecated
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"
)
6 changes: 6 additions & 0 deletions pkg/apis/work/v1alpha1/well_known_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@ limitations under the License.

package v1alpha1

// TODO(whitewindmills): These deprecated labels will be removed in a future version.
const (
// ResourceBindingNamespaceLabel is added to objects to specify associated ResourceBinding's namespace.
// Deprecated
ResourceBindingNamespaceLabel = "resourcebinding.karmada.io/namespace"

// ResourceBindingNameLabel is added to objects to specify associated ResourceBinding's name.
// Deprecated
ResourceBindingNameLabel = "resourcebinding.karmada.io/name"

// ClusterResourceBindingLabel is added to objects to specify associated ClusterResourceBinding.
// Deprecated
ClusterResourceBindingLabel = "clusterresourcebinding.karmada.io/name"

// WorkNamespaceLabel is added to objects to specify associated Work's namespace.
// Deprecated
WorkNamespaceLabel = "work.karmada.io/namespace"

// WorkNameLabel is added to objects to specify associated Work's name.
// Deprecated
WorkNameLabel = "work.karmada.io/name"
)
39 changes: 23 additions & 16 deletions pkg/apis/work/v1alpha2/well_known_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,6 @@ const (
// WorkNameAnnotation is added to objects to specify associated Work's name.
WorkNameAnnotation = "work.karmada.io/name"

// ResourceBindingReferenceKey is the key of ResourceBinding object.
// It is usually a unique hash value of ResourceBinding object's namespace and name, intended to be added to the Work object.
// It will be used to retrieve all Works objects that derived from a specific ResourceBinding object.
ResourceBindingReferenceKey = "resourcebinding.karmada.io/key"

// ClusterResourceBindingReferenceKey is the key of ClusterResourceBinding object.
// It is usually a unique hash value of ClusterResourceBinding object's namespace and name, intended to be added to the Work object.
// It will be used to retrieve all Works objects that derived by a specific ClusterResourceBinding object.
ClusterResourceBindingReferenceKey = "clusterresourcebinding.karmada.io/key"

// ResourceBindingNamespaceAnnotationKey is added to object to describe the associated ResourceBinding's namespace.
// It is added to:
// - Work object: describes the namespace of ResourceBinding which the Work derived from.
Expand All @@ -68,12 +58,6 @@ const (
// - Manifest in Work object: describes the name of ClusterResourceBinding which the manifest derived from.
ClusterResourceBindingAnnotationKey = "clusterresourcebinding.karmada.io/name"

// WorkNamespaceLabel is added to objects to specify associated Work's namespace.
WorkNamespaceLabel = "work.karmada.io/namespace"

// WorkNameLabel is added to objects to specify associated Work's name.
WorkNameLabel = "work.karmada.io/name"

// BindingManagedByLabel is added to ResourceBinding to represent what kind of resource manages this Binding.
BindingManagedByLabel = "binding.karmada.io/managed-by"
)
Expand Down Expand Up @@ -147,3 +131,26 @@ const (
// EvictionProducerTaintManager represents the name of taint manager.
EvictionProducerTaintManager = "TaintManager"
)

// TODO(whitewindmills): These deprecated labels will be removed in a future version.
const (
// ResourceBindingReferenceKey is the key of ResourceBinding object.
// It is usually a unique hash value of ResourceBinding object's namespace and name, intended to be added to the Work object.
// It will be used to retrieve all Works objects that derived from a specific ResourceBinding object.
// Deprecated
ResourceBindingReferenceKey = "resourcebinding.karmada.io/key"

// ClusterResourceBindingReferenceKey is the key of ClusterResourceBinding object.
// It is usually a unique hash value of ClusterResourceBinding object's namespace and name, intended to be added to the Work object.
// It will be used to retrieve all Works objects that derived by a specific ClusterResourceBinding object.
// Deprecated
ClusterResourceBindingReferenceKey = "clusterresourcebinding.karmada.io/key"

// WorkNamespaceLabel is added to objects to specify associated Work's namespace.
// Deprecated
WorkNamespaceLabel = "work.karmada.io/namespace"

// WorkNameLabel is added to objects to specify associated Work's name.
// Deprecated
WorkNameLabel = "work.karmada.io/name"
)
5 changes: 3 additions & 2 deletions pkg/controllers/binding/binding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (c *ResourceBindingController) Reconcile(ctx context.Context, req controlle

if !binding.DeletionTimestamp.IsZero() {
klog.V(4).Infof("Begin to delete works owned by binding(%s).", req.NamespacedName.String())
if err := helper.DeleteWorkByRBNamespaceAndName(c.Client, req.Namespace, req.Name); err != nil {
if err := helper.DeleteWorks(c.Client, req.Namespace, req.Name, binding.Labels[workv1alpha2.ResourceBindingPermanentIDLabel]); err != nil {
klog.Errorf("Failed to delete works related to %s/%s: %v", binding.GetNamespace(), binding.GetName(), err)
return controllerruntime.Result{}, err
}
Expand Down Expand Up @@ -143,7 +143,8 @@ func (c *ResourceBindingController) syncBinding(binding *workv1alpha2.ResourceBi
}

func (c *ResourceBindingController) removeOrphanWorks(binding *workv1alpha2.ResourceBinding) error {
works, err := helper.FindOrphanWorks(c.Client, binding.Namespace, binding.Name, helper.ObtainBindingSpecExistingClusters(binding.Spec))
works, err := helper.FindOrphanWorks(c.Client, binding.Namespace, binding.Name,
binding.Labels[workv1alpha2.ResourceBindingPermanentIDLabel], helper.ObtainBindingSpecExistingClusters(binding.Spec))
if err != nil {
klog.Errorf("Failed to find orphan works by resourceBinding(%s/%s). Error: %v.",
binding.GetNamespace(), binding.GetName(), err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (c *ClusterResourceBindingController) Reconcile(ctx context.Context, req co

if !clusterResourceBinding.DeletionTimestamp.IsZero() {
klog.V(4).Infof("Begin to delete works owned by binding(%s).", req.NamespacedName.String())
if err := helper.DeleteWorkByCRBName(c.Client, req.Name); err != nil {
if err := helper.DeleteWorks(c.Client, "", req.Name, clusterResourceBinding.Labels[workv1alpha2.ClusterResourceBindingPermanentIDLabel]); err != nil {
klog.Errorf("Failed to delete works related to %s: %v", clusterResourceBinding.GetName(), err)
return controllerruntime.Result{}, err
}
Expand Down Expand Up @@ -142,7 +142,7 @@ func (c *ClusterResourceBindingController) syncBinding(binding *workv1alpha2.Clu
}

func (c *ClusterResourceBindingController) removeOrphanWorks(binding *workv1alpha2.ClusterResourceBinding) error {
works, err := helper.FindOrphanWorks(c.Client, "", binding.Name, helper.ObtainBindingSpecExistingClusters(binding.Spec))
works, err := helper.FindOrphanWorks(c.Client, "", binding.Name, binding.Labels[workv1alpha2.ClusterResourceBindingPermanentIDLabel], helper.ObtainBindingSpecExistingClusters(binding.Spec))
if err != nil {
klog.Errorf("Failed to find orphan works by ClusterResourceBinding(%s). Error: %v.", binding.GetName(), err)
c.EventRecorder.Event(binding, corev1.EventTypeWarning, events.EventReasonCleanupWorkFailed, err.Error())
Expand Down
24 changes: 10 additions & 14 deletions pkg/controllers/binding/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ func ensureWork(
targetCluster := targetClusters[i]
clonedWorkload := workload.DeepCopy()

workNamespace := names.GenerateExecutionSpaceName(targetCluster.Name)

// If and only if the resource template has replicas, and the replica scheduling policy is divided,
// we need to revise replicas.
if needReviseReplicas(replicas, placement) {
Expand Down Expand Up @@ -110,7 +108,7 @@ func ensureWork(
}
workLabel := mergeLabel(clonedWorkload, binding, scope)

annotations := mergeAnnotations(clonedWorkload, workNamespace, binding, scope)
annotations := mergeAnnotations(clonedWorkload, binding, scope)
annotations = mergeConflictResolution(clonedWorkload, conflictResolutionInBinding, annotations)
annotations, err = RecordAppliedOverrides(cops, ops, annotations)
if err != nil {
Expand All @@ -120,7 +118,7 @@ func ensureWork(

workMeta := metav1.ObjectMeta{
Name: names.GenerateWorkName(clonedWorkload.GetKind(), clonedWorkload.GetName(), clonedWorkload.GetNamespace()),
Namespace: workNamespace,
Namespace: names.GenerateExecutionSpaceName(targetCluster.Name),
Finalizers: []string{util.ExecutionControllerFinalizer},
Labels: workLabel,
Annotations: annotations,
Expand Down Expand Up @@ -154,30 +152,28 @@ func mergeTargetClusters(targetClusters []workv1alpha2.TargetCluster, requiredBy

func mergeLabel(workload *unstructured.Unstructured, binding metav1.Object, scope apiextensionsv1.ResourceScope) map[string]string {
var workLabel = make(map[string]string)
util.MergeLabel(workload, util.ManagedByKarmadaLabel, util.ManagedByKarmadaLabelValue)
if scope == apiextensionsv1.NamespaceScoped {
bindingID := util.GetLabelValue(binding.GetLabels(), workv1alpha2.ResourceBindingPermanentIDLabel)
util.MergeLabel(workload, workv1alpha2.ResourceBindingReferenceKey, names.GenerateBindingReferenceKey(binding.GetNamespace(), binding.GetName()))
util.MergeLabel(workload, workv1alpha2.ResourceBindingPermanentIDLabel, bindingID)
workLabel[workv1alpha2.ResourceBindingPermanentIDLabel] = bindingID

// TODO(whitewindmills): Delete the following two lines in a future version.
workLabel[workv1alpha2.ResourceBindingReferenceKey] = names.GenerateBindingReferenceKey(binding.GetNamespace(), binding.GetName())
workLabel[workv1alpha2.ResourceBindingPermanentIDLabel] = bindingID
util.MergeLabel(workload, workv1alpha2.ResourceBindingReferenceKey, names.GenerateBindingReferenceKey(binding.GetNamespace(), binding.GetName()))
} else {
bindingID := util.GetLabelValue(binding.GetLabels(), workv1alpha2.ClusterResourceBindingPermanentIDLabel)
util.MergeLabel(workload, workv1alpha2.ClusterResourceBindingReferenceKey, names.GenerateBindingReferenceKey("", binding.GetName()))
util.MergeLabel(workload, workv1alpha2.ClusterResourceBindingPermanentIDLabel, bindingID)
workLabel[workv1alpha2.ClusterResourceBindingPermanentIDLabel] = bindingID

// TODO(whitewindmills): Delete the following two lines in a future version.
workLabel[workv1alpha2.ClusterResourceBindingReferenceKey] = names.GenerateBindingReferenceKey("", binding.GetName())
workLabel[workv1alpha2.ClusterResourceBindingPermanentIDLabel] = bindingID
util.MergeLabel(workload, workv1alpha2.ClusterResourceBindingReferenceKey, names.GenerateBindingReferenceKey("", binding.GetName()))
}
return workLabel
}

func mergeAnnotations(workload *unstructured.Unstructured, workNamespace string, binding metav1.Object, scope apiextensionsv1.ResourceScope) map[string]string {
func mergeAnnotations(workload *unstructured.Unstructured, binding metav1.Object, scope apiextensionsv1.ResourceScope) map[string]string {
annotations := make(map[string]string)
util.MergeAnnotation(workload, workv1alpha2.WorkNameAnnotation, names.GenerateWorkName(workload.GetKind(), workload.GetName(), workload.GetNamespace()))
util.MergeAnnotation(workload, workv1alpha2.WorkNamespaceAnnotation, workNamespace)

if scope == apiextensionsv1.NamespaceScoped {
util.MergeAnnotation(workload, workv1alpha2.ResourceBindingNamespaceAnnotationKey, binding.GetNamespace())
util.MergeAnnotation(workload, workv1alpha2.ResourceBindingNameAnnotationKey, binding.GetName())
Expand Down Expand Up @@ -234,7 +230,7 @@ func mergeConflictResolution(workload *unstructured.Unstructured, conflictResolu
return annotations
} else if conflictResolutionInRT != "" {
// ignore its value and add logs if conflictResolutionInRT is neither abort nor overwrite.
klog.Warningf("ignore the invalid conflict-resolution annotation in ResourceTemplate %s/%s/%s: %s",
klog.Warningf("Ignore the invalid conflict-resolution annotation in ResourceTemplate %s/%s/%s: %s",
workload.GetKind(), workload.GetNamespace(), workload.GetName(), conflictResolutionInRT)
}

Expand Down
33 changes: 18 additions & 15 deletions pkg/controllers/binding/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
"github.com/karmada-io/karmada/pkg/util/names"
)

func Test_mergeTargetClusters(t *testing.T) {
Expand Down Expand Up @@ -136,7 +135,6 @@ func Test_mergeLabel(t *testing.T) {
scope: v1.NamespaceScoped,
want: map[string]string{
workv1alpha2.ResourceBindingPermanentIDLabel: rbID,
workv1alpha2.ResourceBindingReferenceKey: names.GenerateBindingReferenceKey(namespace, bindingName),
},
},
{
Expand All @@ -161,13 +159,21 @@ func Test_mergeLabel(t *testing.T) {
scope: v1.ClusterScoped,
want: map[string]string{
workv1alpha2.ClusterResourceBindingPermanentIDLabel: rbID,
workv1alpha2.ClusterResourceBindingReferenceKey: names.GenerateBindingReferenceKey("", bindingName),
},
},
}

checker := func(got, want map[string]string) bool {
for key, val := range want {
if got[key] != val {
return false
}
}
return true
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := mergeLabel(tt.workload, tt.binding, tt.scope); !reflect.DeepEqual(got, tt.want) {
if got := mergeLabel(tt.workload, tt.binding, tt.scope); !checker(got, tt.want) {
t.Errorf("mergeLabel() = %v, want %v", got, tt.want)
}
})
Expand All @@ -179,16 +185,14 @@ func Test_mergeAnnotations(t *testing.T) {
bindingName := "fake-bindingName"

tests := []struct {
name string
namespace string
workload *unstructured.Unstructured
binding metav1.Object
scope v1.ResourceScope
want map[string]string
name string
workload *unstructured.Unstructured
binding metav1.Object
scope v1.ResourceScope
want map[string]string
}{
{
name: "NamespaceScoped",
namespace: "test",
name: "NamespaceScoped",
workload: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
Expand All @@ -212,8 +216,7 @@ func Test_mergeAnnotations(t *testing.T) {
},
},
{
name: "ClusterScoped",
namespace: "",
name: "ClusterScoped",
workload: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
Expand All @@ -236,7 +239,7 @@ func Test_mergeAnnotations(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := mergeAnnotations(tt.workload, tt.namespace, tt.binding, tt.scope); !reflect.DeepEqual(got, tt.want) {
if got := mergeAnnotations(tt.workload, tt.binding, tt.scope); !reflect.DeepEqual(got, tt.want) {
t.Errorf("mergeAnnotations() = %v, want %v", got, tt.want)
}
})
Expand Down
3 changes: 0 additions & 3 deletions pkg/controllers/certificate/cert_rotation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,6 @@ func (c *CertRotationController) createCSRInControlPlane(clusterName string, pri
certificateSigningRequest := &certificatesv1.CertificateSigningRequest{
ObjectMeta: metav1.ObjectMeta{
Name: csrName,
Labels: map[string]string{
util.ManagedByKarmadaLabel: util.ManagedByKarmadaLabelValue,
},
},
Spec: certificatesv1.CertificateSigningRequestSpec{
Request: csrData,
Expand Down
3 changes: 0 additions & 3 deletions pkg/controllers/cluster/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,6 @@ func (c *Controller) createExecutionSpace(cluster *clusterv1alpha1.Cluster) erro
executionSpace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: executionSpaceName,
Labels: map[string]string{
util.ManagedByKarmadaLabel: util.ManagedByKarmadaLabelValue,
},
},
}
err = c.Client.Create(context.TODO(), executionSpace)
Expand Down
Loading

0 comments on commit 1469cd6

Please sign in to comment.