Skip to content

Commit

Permalink
rename marks.go to claim.go
Browse files Browse the repository at this point in the history
Signed-off-by: zhzhuang-zju <[email protected]>
  • Loading branch information
zhzhuang-zju committed Oct 8, 2024
1 parent 3808a1a commit d20e47c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
32 changes: 16 additions & 16 deletions pkg/detector/marks.go → pkg/detector/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ import (
)

var (
propagationPolicyMarkedLabels = []string{
propagationPolicyClaimLabels = []string{
policyv1alpha1.PropagationPolicyPermanentIDLabel,
}
propagationPolicyMarkedAnnotations = []string{
propagationPolicyClaimAnnotations = []string{
policyv1alpha1.PropagationPolicyNamespaceAnnotation,
policyv1alpha1.PropagationPolicyNameAnnotation,
}
clusterPropagationPolicyMarkedLabels = []string{
clusterPropagationPolicyClaimLabels = []string{
policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel,
}
clusterPropagationPolicyMarkedAnnotations = []string{
clusterPropagationPolicyClaimAnnotations = []string{
policyv1alpha1.ClusterPropagationPolicyAnnotation,
}
)

// AddPolicyMarks adds PropagationPolicy marks, such as labels and annotations
func AddPolicyMarks(obj metav1.Object, policyID string, policyMeta metav1.ObjectMeta) {
// AddPPClaimMetadata adds PropagationPolicy claim metadata, such as labels and annotations
func AddPPClaimMetadata(obj metav1.Object, policyID string, policyMeta metav1.ObjectMeta) {
util.MergeLabel(obj, policyv1alpha1.PropagationPolicyPermanentIDLabel, policyID)

objectAnnotations := obj.GetAnnotations()
Expand All @@ -52,8 +52,8 @@ func AddPolicyMarks(obj metav1.Object, policyID string, policyMeta metav1.Object
obj.SetAnnotations(objectAnnotations)
}

// AddClusterPolicyMarks adds ClusterPropagationPolicy marks, such as labels and annotations
func AddClusterPolicyMarks(obj metav1.Object, policyID string, policyMeta metav1.ObjectMeta) {
// AddCPPClaimMetadata adds ClusterPropagationPolicy claim metadata, such as labels and annotations
func AddCPPClaimMetadata(obj metav1.Object, policyID string, policyMeta metav1.ObjectMeta) {
util.MergeLabel(obj, policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel, policyID)

objectAnnotations := obj.GetAnnotations()
Expand All @@ -64,14 +64,14 @@ func AddClusterPolicyMarks(obj metav1.Object, policyID string, policyMeta metav1
obj.SetAnnotations(objectAnnotations)
}

// CleanupPolicyMarks removes PropagationPolicy marks, such as labels and annotations
func CleanupPolicyMarks(obj metav1.Object) {
util.RemoveLabels(obj, propagationPolicyMarkedLabels...)
util.RemoveAnnotations(obj, propagationPolicyMarkedAnnotations...)
// CleanupPPClaimMetadata removes PropagationPolicy claim metadata, such as labels and annotations
func CleanupPPClaimMetadata(obj metav1.Object) {
util.RemoveLabels(obj, propagationPolicyClaimLabels...)
util.RemoveAnnotations(obj, propagationPolicyClaimAnnotations...)
}

// CleanupClusterPolicyMarks removes ClusterPropagationPolicy marks, such as labels and annotations
func CleanupClusterPolicyMarks(obj metav1.Object) {
util.RemoveLabels(obj, clusterPropagationPolicyMarkedLabels...)
util.RemoveAnnotations(obj, clusterPropagationPolicyMarkedAnnotations...)
// CleanupCPPClaimMetadata removes ClusterPropagationPolicy claim metadata, such as labels and annotations
func CleanupCPPClaimMetadata(obj metav1.Object) {
util.RemoveLabels(obj, clusterPropagationPolicyClaimLabels...)
util.RemoveAnnotations(obj, clusterPropagationPolicyClaimAnnotations...)
}
16 changes: 8 additions & 8 deletions pkg/detector/marks_test.go → pkg/detector/claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Test_AddPolicyMarks(t *testing.T) {
result metav1.Object
}{
{
name: "add policy marks",
name: "add policy claim metadata",
policyID: "f2507cgb-f3f3-4a4b-b289-5691a4fef979",
policyMeta: metav1.ObjectMeta{Name: "pp-example", Namespace: "test"},
obj: &unstructured.Unstructured{
Expand All @@ -57,7 +57,7 @@ func Test_AddPolicyMarks(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
AddPolicyMarks(tt.obj, tt.policyID, tt.policyMeta)
AddPPClaimMetadata(tt.obj, tt.policyID, tt.policyMeta)
assert.Equal(t, tt.obj, tt.result)
})
}
Expand All @@ -72,7 +72,7 @@ func Test_AddClusterPolicyMarks(t *testing.T) {
result metav1.Object
}{
{
name: "add cluster policy marks",
name: "add cluster policy claim metadata",
policyID: "f2507cgb-f3f3-4a4b-b289-5691a4fef979",
policyMeta: metav1.ObjectMeta{Name: "cpp-example"},
obj: &unstructured.Unstructured{
Expand All @@ -94,7 +94,7 @@ func Test_AddClusterPolicyMarks(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
AddClusterPolicyMarks(tt.obj, tt.policyID, tt.policyMeta)
AddCPPClaimMetadata(tt.obj, tt.policyID, tt.policyMeta)
assert.Equal(t, tt.obj, tt.result)
})
}
Expand All @@ -107,7 +107,7 @@ func Test_CleanupPolicyMarks(t *testing.T) {
result metav1.Object
}{
{
name: "clean up policy marks",
name: "clean up policy claim metadata",
obj: &unstructured.Unstructured{
Object: map[string]interface{}{
"metadata": map[string]interface{}{
Expand All @@ -128,7 +128,7 @@ func Test_CleanupPolicyMarks(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
CleanupPolicyMarks(tt.obj)
CleanupPPClaimMetadata(tt.obj)
assert.Equal(t, tt.obj, tt.result)
})
}
Expand All @@ -141,7 +141,7 @@ func Test_CleanupClusterPolicyMarks(t *testing.T) {
result metav1.Object
}{
{
name: "clean up policy marks",
name: "clean up policy claim metadata",
obj: &unstructured.Unstructured{
Object: map[string]interface{}{
"metadata": map[string]interface{}{
Expand All @@ -162,7 +162,7 @@ func Test_CleanupClusterPolicyMarks(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
CleanupClusterPolicyMarks(tt.obj)
CleanupCPPClaimMetadata(tt.obj)
assert.Equal(t, tt.obj, tt.result)
})
}
Expand Down
22 changes: 11 additions & 11 deletions pkg/detector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ func (d *ResourceDetector) ApplyPolicy(object *unstructured.Unstructured, object
return nil
}

binding, err := d.BuildResourceBinding(object, &policy.Spec, policyID, policy.ObjectMeta, AddPolicyMarks)
binding, err := d.BuildResourceBinding(object, &policy.Spec, policyID, policy.ObjectMeta, AddPPClaimMetadata)
if err != nil {
klog.Errorf("Failed to build resourceBinding for object: %s. error: %v", objectKey, err)
return err
Expand Down Expand Up @@ -536,7 +536,7 @@ func (d *ResourceDetector) ApplyClusterPolicy(object *unstructured.Unstructured,
// For namespace-scoped resources, which namespace is not empty, building `ResourceBinding`.
// For cluster-scoped resources, which namespace is empty, building `ClusterResourceBinding`.
if object.GetNamespace() != "" {
binding, err := d.BuildResourceBinding(object, &policy.Spec, policyID, policy.ObjectMeta, AddClusterPolicyMarks)
binding, err := d.BuildResourceBinding(object, &policy.Spec, policyID, policy.ObjectMeta, AddCPPClaimMetadata)
if err != nil {
klog.Errorf("Failed to build resourceBinding for object: %s. error: %v", objectKey, err)
return err
Expand Down Expand Up @@ -683,7 +683,7 @@ func (d *ResourceDetector) ClaimPolicyForObject(object *unstructured.Unstructure
}

objectCopy := object.DeepCopy()
AddPolicyMarks(objectCopy, policyID, policy.ObjectMeta)
AddPPClaimMetadata(objectCopy, policyID, policy.ObjectMeta)
return policyID, d.Client.Update(context.TODO(), objectCopy)
}

Expand All @@ -698,7 +698,7 @@ func (d *ResourceDetector) ClaimClusterPolicyForObject(object *unstructured.Unst
}

objectCopy := object.DeepCopy()
AddClusterPolicyMarks(objectCopy, policyID, policy.ObjectMeta)
AddCPPClaimMetadata(objectCopy, policyID, policy.ObjectMeta)

return policyID, d.Client.Update(context.TODO(), objectCopy)
}
Expand Down Expand Up @@ -776,7 +776,7 @@ func (d *ResourceDetector) BuildClusterResourceBinding(object *unstructured.Unst
},
}

AddClusterPolicyMarks(binding, policyID, policyMeta)
AddCPPClaimMetadata(binding, policyID, policyMeta)

if d.ResourceInterpreter.HookEnabled(object.GroupVersionKind(), configv1alpha1.InterpreterOperationInterpretReplica) {
replicas, replicaRequirements, err := d.ResourceInterpreter.GetReplicas(object)
Expand Down Expand Up @@ -1062,7 +1062,7 @@ func (d *ResourceDetector) HandlePropagationPolicyDeletion(policyID string) erro
// Must remove the marks, such as labels and annotations, from the resource template ahead of ResourceBinding,
// otherwise might lose the chance to do that in a retry loop (in particular, the marks was successfully removed
// from ResourceBinding, but resource template not), since the ResourceBinding will not be listed again.
if err := d.CleanupResourceTemplateMarks(binding.Spec.Resource, CleanupPolicyMarks); err != nil {
if err := d.CleanupResourceTemplateMarks(binding.Spec.Resource, CleanupPPClaimMetadata); err != nil {
klog.Errorf("Failed to clean up marks from resource(%s-%s/%s) when propagationPolicy removed, error: %v",
binding.Spec.Resource.Kind, binding.Spec.Resource.Namespace, binding.Spec.Resource.Name, err)
errs = append(errs, err)
Expand All @@ -1071,7 +1071,7 @@ func (d *ResourceDetector) HandlePropagationPolicyDeletion(policyID string) erro
}

// Clean up the marks from the reference binding so that the karmada scheduler won't reschedule the binding.
if err := d.CleanupResourceBindingMarks(&rbs.Items[index], CleanupPolicyMarks); err != nil {
if err := d.CleanupResourceBindingMarks(&rbs.Items[index], CleanupPPClaimMetadata); err != nil {
klog.Errorf("Failed to clean up marks from resource binding(%s/%s) when propagationPolicy removed, error: %v",
binding.Namespace, binding.Name, err)
errs = append(errs, err)
Expand Down Expand Up @@ -1102,15 +1102,15 @@ func (d *ResourceDetector) HandleClusterPropagationPolicyDeletion(policyID strin
// ClusterResourceBinding, otherwise might lose the chance to do that in a retry loop (in particular, the
// marks was successfully removed from ClusterResourceBinding, but resource template not), since the
// ClusterResourceBinding will not be listed again.
if err := d.CleanupResourceTemplateMarks(binding.Spec.Resource, CleanupClusterPolicyMarks); err != nil {
if err := d.CleanupResourceTemplateMarks(binding.Spec.Resource, CleanupCPPClaimMetadata); err != nil {
klog.Errorf("Failed to clean up marks from resource(%s-%s) when clusterPropagationPolicy removed, error: %v",
binding.Spec.Resource.Kind, binding.Spec.Resource.Name, err)
// Skip cleaning up policy labels and annotations from ClusterResourceBinding, give a chance to do that in a retry loop.
continue
}

// Clean up the marks from the reference binding so that the Karmada scheduler won't reschedule the binding.
if err := d.CleanupClusterResourceBindingMarks(&crbs.Items[index], CleanupClusterPolicyMarks); err != nil {
if err := d.CleanupClusterResourceBindingMarks(&crbs.Items[index], CleanupCPPClaimMetadata); err != nil {
klog.Errorf("Failed to clean up marks from clusterResourceBinding(%s) when clusterPropagationPolicy removed, error: %v",
binding.Name, err)
errs = append(errs, err)
Expand All @@ -1128,7 +1128,7 @@ func (d *ResourceDetector) HandleClusterPropagationPolicyDeletion(policyID strin
// Must remove the marks, such as labels and annotations, from the resource template ahead of ResourceBinding,
// otherwise might lose the chance to do that in a retry loop (in particular, the label was successfully
// removed from ResourceBinding, but resource template not), since the ResourceBinding will not be listed again.
if err := d.CleanupResourceTemplateMarks(binding.Spec.Resource, CleanupClusterPolicyMarks); err != nil {
if err := d.CleanupResourceTemplateMarks(binding.Spec.Resource, CleanupCPPClaimMetadata); err != nil {
klog.Errorf("Failed to clean up marks from resource(%s-%s/%s) when clusterPropagationPolicy removed, error: %v",
binding.Spec.Resource.Kind, binding.Spec.Resource.Namespace, binding.Spec.Resource.Name, err)
errs = append(errs, err)
Expand All @@ -1137,7 +1137,7 @@ func (d *ResourceDetector) HandleClusterPropagationPolicyDeletion(policyID strin
}

// Clean up the marks from the reference binding so that the Karmada scheduler won't reschedule the binding.
if err := d.CleanupResourceBindingMarks(&rbs.Items[index], CleanupClusterPolicyMarks); err != nil {
if err := d.CleanupResourceBindingMarks(&rbs.Items[index], CleanupCPPClaimMetadata); err != nil {
klog.Errorf("Failed to clean up marks from resourceBinding(%s/%s) when clusterPropagationPolicy removed, error: %v",
binding.Namespace, binding.Name, err)
errs = append(errs, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/detector/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,6 @@ func excludeClusterPolicy(obj metav1.Object) (hasClaimedClusterPolicy bool) {
if _, ok := obj.GetLabels()[policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel]; !ok {
return false
}
CleanupClusterPolicyMarks(obj)
CleanupCPPClaimMetadata(obj)
return true
}

0 comments on commit d20e47c

Please sign in to comment.