Skip to content

Commit

Permalink
Clean up the residual annotations when resources are preempted by pp …
Browse files Browse the repository at this point in the history
…from cpp

Signed-off-by: zhzhuang-zju <[email protected]>
  • Loading branch information
zhzhuang-zju committed Sep 18, 2024
1 parent 96622c6 commit 457a70f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 39 deletions.
10 changes: 8 additions & 2 deletions pkg/detector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package detector
import (
"context"
"fmt"
"reflect"
"regexp"
"sync"
"time"
Expand Down Expand Up @@ -501,7 +502,9 @@ func (d *ResourceDetector) ApplyPolicy(object *unstructured.Unstructured, object
bindingCopy.Spec.Failover = binding.Spec.Failover
bindingCopy.Spec.ConflictResolution = binding.Spec.ConflictResolution
bindingCopy.Spec.Suspension = binding.Spec.Suspension
excludeClusterPolicy(bindingCopy.Labels)
// If propagation policy was claimed, cluster propagation policy should not exist.
util.RemoveLabels(bindingCopy, clusterPropagationPolicyMarkedLabels...)
util.RemoveAnnotations(bindingCopy, clusterPropagationPolicyMarkedAnnotations...)
return nil
})
if err != nil {
Expand Down Expand Up @@ -708,8 +711,11 @@ func (d *ResourceDetector) ClaimPolicyForObject(object *unstructured.Unstructure
if objLabels == nil {
objLabels = make(map[string]string)
} else if len(objLabels) > 0 {
objCopy := object.DeepCopy()
util.RemoveLabels(object, clusterPropagationPolicyMarkedLabels...)
util.RemoveAnnotations(object, clusterPropagationPolicyMarkedAnnotations...)
// object has been claimed, don't need to claim again
if !excludeClusterPolicy(objLabels) &&
if reflect.DeepEqual(object, objCopy) &&
objLabels[policyv1alpha1.PropagationPolicyPermanentIDLabel] == policyID {
return policyID, nil
}
Expand Down
10 changes: 0 additions & 10 deletions pkg/detector/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,3 @@ func (d *ResourceDetector) listCPPDerivedCRBs(policyID, policyName string) (*wor

return bindings, nil
}

// excludeClusterPolicy excludes cluster propagation policy.
// If propagation policy was claimed, cluster propagation policy should not exist.
func excludeClusterPolicy(objLabels map[string]string) bool {
if _, ok := objLabels[policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel]; !ok {
return false
}
delete(objLabels, policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel)
return true
}
27 changes: 0 additions & 27 deletions pkg/detector/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -1360,29 +1359,3 @@ func Test_listCPPDerivedCRBs(t *testing.T) {
})
}
}

func Test_excludeClusterPolicy(t *testing.T) {
tests := []struct {
name string
objLabels map[string]string
want bool
}{
{
name: "propagation policy was claimed",
objLabels: map[string]string{},
want: false,
}, {
name: "propagation policy was not claimed",
objLabels: map[string]string{
policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel: "f2507cgb-f3f3-4a4b-b289-5691a4fef979",
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := excludeClusterPolicy(tt.objLabels)
assert.Equal(t, tt.want, got)
})
}
}

0 comments on commit 457a70f

Please sign in to comment.