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 committed Mar 26, 2024
1 parent da3254a commit b562e7f
Show file tree
Hide file tree
Showing 36 changed files with 426 additions and 549 deletions.
31 changes: 13 additions & 18 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 @@ -34,27 +35,9 @@ const (
// PropagationPolicyUIDLabel is the uid of PropagationPolicy object.
PropagationPolicyUIDLabel = "propagationpolicy.karmada.io/uid"

// PropagationPolicyNamespaceAnnotation is added to objects to specify associated PropagationPolicy namespace.
PropagationPolicyNamespaceAnnotation = "propagationpolicy.karmada.io/namespace"

// PropagationPolicyNameAnnotation is added to objects to specify associated PropagationPolicy name.
PropagationPolicyNameAnnotation = "propagationpolicy.karmada.io/name"

// ClusterPropagationPolicyUIDLabel is the uid of ClusterPropagationPolicy object.
ClusterPropagationPolicyUIDLabel = "clusterpropagationpolicy.karmada.io/uid"

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

// PropagationPolicyNamespaceLabel is added to objects to specify associated PropagationPolicy namespace.
PropagationPolicyNamespaceLabel = "propagationpolicy.karmada.io/namespace"

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

// 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:
Expand All @@ -65,3 +48,15 @@ const (
// 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"

// PropagationPolicyNameAnnotation is added to objects to specify associated PropagationPolicy name.
PropagationPolicyNameAnnotation = "propagationpolicy.karmada.io/name"

// ClusterPropagationPolicyAnnotation is added to objects to specify associated ClusterPropagationPolicy name.
ClusterPropagationPolicyAnnotation = "clusterpropagationpolicy.karmada.io/name"
)
34 changes: 0 additions & 34 deletions pkg/apis/work/v1alpha1/well_known_constants.go

This file was deleted.

16 changes: 0 additions & 16 deletions pkg/apis/work/v1alpha2/well_known_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ const (
// WorkUIDLabel is the uid of Work object.
WorkUIDLabel = "work.karmada.io/uid"

// 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 @@ -77,12 +67,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
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{Requeue: true}, 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{Requeue: true}, 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
11 changes: 2 additions & 9 deletions pkg/controllers/binding/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1"
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
"github.com/karmada-io/karmada/pkg/resourceinterpreter"
"github.com/karmada-io/karmada/pkg/util"
Expand Down Expand Up @@ -109,7 +108,7 @@ func ensureWork(
klog.Errorf("Failed to apply overrides for %s/%s/%s, err is: %v", clonedWorkload.GetKind(), clonedWorkload.GetNamespace(), clonedWorkload.GetName(), err)
return err
}
workLabel := mergeLabel(clonedWorkload, workNamespace, binding, scope)
workLabel := mergeLabel(clonedWorkload, binding, scope)

annotations := mergeAnnotations(clonedWorkload, workNamespace, binding, scope)
annotations = mergeConflictResolution(clonedWorkload, conflictResolutionInBinding, annotations)
Expand Down Expand Up @@ -153,28 +152,22 @@ func mergeTargetClusters(targetClusters []workv1alpha2.TargetCluster, requiredBy
return targetClusters
}

func mergeLabel(workload *unstructured.Unstructured, workNamespace string, binding metav1.Object, scope apiextensionsv1.ResourceScope) map[string]string {
func mergeLabel(workload *unstructured.Unstructured, binding metav1.Object, scope apiextensionsv1.ResourceScope) map[string]string {
var workLabel = make(map[string]string)
util.MergeLabel(workload, workv1alpha1.WorkNamespaceLabel, workNamespace)
util.MergeLabel(workload, workv1alpha1.WorkNameLabel, names.GenerateWorkName(workload.GetKind(), workload.GetName(), workload.GetNamespace()))
util.MergeLabel(workload, util.ManagedByKarmadaLabel, util.ManagedByKarmadaLabelValue)
if scope == apiextensionsv1.NamespaceScoped {
util.RemoveLabels(workload, workv1alpha2.ResourceBindingUIDLabel)

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.ResourceBindingReferenceKey] = names.GenerateBindingReferenceKey(binding.GetNamespace(), binding.GetName())
workLabel[workv1alpha2.ResourceBindingPermanentIDLabel] = bindingID
} else {
util.RemoveLabels(workload, workv1alpha2.ClusterResourceBindingUIDLabel)

bindingID := util.GetLabelValue(binding.GetLabels(), workv1alpha2.ClusterResourceBindingPermanentIDLabel)
util.MergeLabel(workload, workv1alpha2.ClusterResourceBindingReferenceKey, names.GenerateBindingReferenceKey("", binding.GetName()))
util.MergeLabel(workload, workv1alpha2.ClusterResourceBindingPermanentIDLabel, bindingID)

workLabel[workv1alpha2.ClusterResourceBindingReferenceKey] = names.GenerateBindingReferenceKey("", binding.GetName())
workLabel[workv1alpha2.ClusterResourceBindingPermanentIDLabel] = bindingID
}
return workLabel
Expand Down
17 changes: 6 additions & 11 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 @@ -106,12 +105,11 @@ func Test_mergeLabel(t *testing.T) {
rbID := "93162d3c-ee8e-4995-9034-05f4d5d2c2b9"

tests := []struct {
name string
workload *unstructured.Unstructured
workNamespace string
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",
Expand All @@ -125,7 +123,6 @@ func Test_mergeLabel(t *testing.T) {
},
},
},
workNamespace: namespace,
binding: &workv1alpha2.ClusterResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: bindingName,
Expand All @@ -138,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 @@ -163,13 +159,12 @@ func Test_mergeLabel(t *testing.T) {
scope: v1.ClusterScoped,
want: map[string]string{
workv1alpha2.ClusterResourceBindingPermanentIDLabel: rbID,
workv1alpha2.ClusterResourceBindingReferenceKey: names.GenerateBindingReferenceKey("", bindingName),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := mergeLabel(tt.workload, tt.workNamespace, tt.binding, tt.scope); !reflect.DeepEqual(got, tt.want) {
if got := mergeLabel(tt.workload, tt.binding, tt.scope); !reflect.DeepEqual(got, tt.want) {
t.Errorf("mergeLabel() = %v, want %v", got, tt.want)
}
})
Expand Down
3 changes: 1 addition & 2 deletions pkg/controllers/execution/execution_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (c *Controller) tryDeleteWorkload(clusterName string, work *workv1alpha1.Wo
}

// Avoid deleting resources that not managed by karmada.
if util.GetLabelValue(clusterObj.GetLabels(), workv1alpha1.WorkNameLabel) != util.GetLabelValue(workload.GetLabels(), workv1alpha1.WorkNameLabel) {
if util.GetLabelValue(clusterObj.GetLabels(), workv1alpha2.WorkPermanentIDLabel) != util.GetLabelValue(workload.GetLabels(), workv1alpha2.WorkPermanentIDLabel) {
klog.Infof("Abort deleting the resource(kind=%s, %s/%s) exists in cluster %v but not managed by karmada", clusterObj.GetKind(), clusterObj.GetNamespace(), clusterObj.GetName(), clusterName)
return nil
}
Expand Down Expand Up @@ -211,7 +211,6 @@ func (c *Controller) syncToClusters(clusterName string, work *workv1alpha1.Work)
errs = append(errs, err)
continue
}
util.MergeLabel(workload, workv1alpha2.WorkPermanentIDLabel, util.GetLabelValue(work.Labels, workv1alpha2.WorkPermanentIDLabel))

if err = c.tryCreateOrUpdateWorkload(clusterName, workload); err != nil {
klog.Errorf("Failed to create or update resource(%v/%v) in the given member cluster %s, err is %v", workload.GetNamespace(), workload.GetName(), clusterName, err)
Expand Down
20 changes: 9 additions & 11 deletions pkg/controllers/federatedhpa/federatedhpa_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,25 +404,23 @@ func (c *FHPAController) reconcileAutoscaler(ctx context.Context, hpa *autoscali

func (c *FHPAController) getBindingByLabel(resourceLabel map[string]string, resourceRef autoscalingv2.CrossVersionObjectReference) (*workv1alpha2.ResourceBinding, error) {
if len(resourceLabel) == 0 {
return nil, fmt.Errorf("Target resource has no label. ")
return nil, errors.New("target resource has no policy permanent-id label")
}

var policyName, policyNameSpace string
var policyID string
var selector labels.Selector
if _, ok := resourceLabel[policyv1alpha1.PropagationPolicyNameLabel]; ok {
policyName = resourceLabel[policyv1alpha1.PropagationPolicyNameLabel]
policyNameSpace = resourceLabel[policyv1alpha1.PropagationPolicyNamespaceLabel]
if _, ok := resourceLabel[policyv1alpha1.PropagationPolicyPermanentIDLabel]; ok {
policyID = resourceLabel[policyv1alpha1.PropagationPolicyPermanentIDLabel]
selector = labels.SelectorFromSet(labels.Set{
policyv1alpha1.PropagationPolicyNameLabel: policyName,
policyv1alpha1.PropagationPolicyNamespaceLabel: policyNameSpace,
policyv1alpha1.PropagationPolicyPermanentIDLabel: policyID,
})
} else if _, ok = resourceLabel[policyv1alpha1.ClusterPropagationPolicyLabel]; ok {
policyName = resourceLabel[policyv1alpha1.ClusterPropagationPolicyLabel]
} else if _, ok = resourceLabel[policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel]; ok {
policyID = resourceLabel[policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel]
selector = labels.SelectorFromSet(labels.Set{
policyv1alpha1.ClusterPropagationPolicyLabel: policyName,
policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel: policyID,
})
} else {
return nil, fmt.Errorf("No label of policy found. ")
return nil, errors.New("no label of policy permanent-id found")
}

binding := &workv1alpha2.ResourceBinding{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@ func (c *SyncController) buildWorks(quota *policyv1alpha1.FederatedResourceQuota
resourceQuota.Namespace = quota.Namespace
resourceQuota.Name = quota.Name
resourceQuota.Labels = map[string]string{
workv1alpha1.WorkNamespaceLabel: workNamespace,
workv1alpha1.WorkNameLabel: workName,
util.ManagedByKarmadaLabel: util.ManagedByKarmadaLabelValue,
util.ManagedByKarmadaLabel: util.ManagedByKarmadaLabelValue,
}
resourceQuota.Spec.Hard = extractClusterHardResourceList(quota.Spec, cluster.Name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (r *HpaScaleTargetMarker) Generic(_ event.GenericEvent) bool {
}

func hasBeenPropagated(hpa *autoscalingv2.HorizontalPodAutoscaler) bool {
_, ppExist := hpa.GetLabels()[policyv1alpha1.PropagationPolicyNameLabel]
_, cppExist := hpa.GetLabels()[policyv1alpha1.ClusterPropagationPolicyLabel]
_, ppExist := hpa.GetLabels()[policyv1alpha1.PropagationPolicyPermanentIDLabel]
_, cppExist := hpa.GetLabels()[policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel]
return ppExist || cppExist
}
Loading

0 comments on commit b562e7f

Please sign in to comment.