-
Notifications
You must be signed in to change notification settings - Fork 885
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5563 from zhzhuang-zju/preemption
Clean up the residual annotations when resources are preempted by pp from cpp
- Loading branch information
Showing
5 changed files
with
354 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright 2024 The Karmada Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package detector | ||
|
||
import ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1" | ||
"github.com/karmada-io/karmada/pkg/util" | ||
) | ||
|
||
var ( | ||
propagationPolicyClaimLabels = []string{ | ||
policyv1alpha1.PropagationPolicyPermanentIDLabel, | ||
} | ||
propagationPolicyClaimAnnotations = []string{ | ||
policyv1alpha1.PropagationPolicyNamespaceAnnotation, | ||
policyv1alpha1.PropagationPolicyNameAnnotation, | ||
} | ||
clusterPropagationPolicyClaimLabels = []string{ | ||
policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel, | ||
} | ||
clusterPropagationPolicyClaimAnnotations = []string{ | ||
policyv1alpha1.ClusterPropagationPolicyAnnotation, | ||
} | ||
) | ||
|
||
// 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() | ||
if objectAnnotations == nil { | ||
objectAnnotations = make(map[string]string) | ||
} | ||
objectAnnotations[policyv1alpha1.PropagationPolicyNamespaceAnnotation] = policyMeta.GetNamespace() | ||
objectAnnotations[policyv1alpha1.PropagationPolicyNameAnnotation] = policyMeta.GetName() | ||
obj.SetAnnotations(objectAnnotations) | ||
} | ||
|
||
// 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() | ||
if objectAnnotations == nil { | ||
objectAnnotations = make(map[string]string) | ||
} | ||
objectAnnotations[policyv1alpha1.ClusterPropagationPolicyAnnotation] = policyMeta.GetName() | ||
obj.SetAnnotations(objectAnnotations) | ||
} | ||
|
||
// CleanupPPClaimMetadata removes PropagationPolicy claim metadata, such as labels and annotations | ||
func CleanupPPClaimMetadata(obj metav1.Object) { | ||
util.RemoveLabels(obj, propagationPolicyClaimLabels...) | ||
util.RemoveAnnotations(obj, propagationPolicyClaimAnnotations...) | ||
} | ||
|
||
// CleanupCPPClaimMetadata removes ClusterPropagationPolicy claim metadata, such as labels and annotations | ||
func CleanupCPPClaimMetadata(obj metav1.Object) { | ||
util.RemoveLabels(obj, clusterPropagationPolicyClaimLabels...) | ||
util.RemoveAnnotations(obj, clusterPropagationPolicyClaimAnnotations...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
Copyright 2024 The Karmada Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package detector | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
|
||
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1" | ||
) | ||
|
||
func TestAddPPClaimMetadata(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
policyID string | ||
policyMeta metav1.ObjectMeta | ||
obj metav1.Object | ||
result metav1.Object | ||
}{ | ||
{ | ||
name: "add policy claim metadata", | ||
policyID: "f2507cgb-f3f3-4a4b-b289-5691a4fef979", | ||
policyMeta: metav1.ObjectMeta{Name: "pp-example", Namespace: "test"}, | ||
obj: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"metadata": map[string]interface{}{ | ||
"labels": map[string]interface{}{}, | ||
}, | ||
}, | ||
}, | ||
result: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"metadata": map[string]interface{}{ | ||
"labels": map[string]interface{}{policyv1alpha1.PropagationPolicyPermanentIDLabel: "f2507cgb-f3f3-4a4b-b289-5691a4fef979"}, | ||
"annotations": map[string]interface{}{policyv1alpha1.PropagationPolicyNamespaceAnnotation: "test", policyv1alpha1.PropagationPolicyNameAnnotation: "pp-example"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
AddPPClaimMetadata(tt.obj, tt.policyID, tt.policyMeta) | ||
assert.Equal(t, tt.obj, tt.result) | ||
}) | ||
} | ||
} | ||
|
||
func TestAddCPPClaimMetadata(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
policyID string | ||
policyMeta metav1.ObjectMeta | ||
obj metav1.Object | ||
result metav1.Object | ||
}{ | ||
{ | ||
name: "add cluster policy claim metadata", | ||
policyID: "f2507cgb-f3f3-4a4b-b289-5691a4fef979", | ||
policyMeta: metav1.ObjectMeta{Name: "cpp-example"}, | ||
obj: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"metadata": map[string]interface{}{ | ||
"labels": map[string]interface{}{}, | ||
}, | ||
}, | ||
}, | ||
result: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"metadata": map[string]interface{}{ | ||
"labels": map[string]interface{}{policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel: "f2507cgb-f3f3-4a4b-b289-5691a4fef979"}, | ||
"annotations": map[string]interface{}{policyv1alpha1.ClusterPropagationPolicyAnnotation: "cpp-example"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
AddCPPClaimMetadata(tt.obj, tt.policyID, tt.policyMeta) | ||
assert.Equal(t, tt.obj, tt.result) | ||
}) | ||
} | ||
} | ||
|
||
func TestCleanupPPClaimMetadata(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
obj metav1.Object | ||
result metav1.Object | ||
}{ | ||
{ | ||
name: "clean up policy claim metadata", | ||
obj: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"metadata": map[string]interface{}{ | ||
"labels": map[string]interface{}{policyv1alpha1.PropagationPolicyPermanentIDLabel: "f2507cgb-f3f3-4a4b-b289-5691a4fef979"}, | ||
"annotations": map[string]interface{}{policyv1alpha1.PropagationPolicyNamespaceAnnotation: "default", policyv1alpha1.PropagationPolicyNameAnnotation: "pp-example"}, | ||
}, | ||
}, | ||
}, | ||
result: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"metadata": map[string]interface{}{ | ||
"labels": map[string]interface{}{}, | ||
"annotations": map[string]interface{}{}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
CleanupPPClaimMetadata(tt.obj) | ||
assert.Equal(t, tt.obj, tt.result) | ||
}) | ||
} | ||
} | ||
|
||
func TestCleanupCPPClaimMetadata(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
obj metav1.Object | ||
result metav1.Object | ||
}{ | ||
{ | ||
name: "clean up policy claim metadata", | ||
obj: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"metadata": map[string]interface{}{ | ||
"labels": map[string]interface{}{policyv1alpha1.ClusterPropagationPolicyPermanentIDLabel: "f2507cgb-f3f3-4a4b-b289-5691a4fef979"}, | ||
"annotations": map[string]interface{}{policyv1alpha1.ClusterPropagationPolicyAnnotation: "cpp-example"}, | ||
}, | ||
}, | ||
}, | ||
result: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"metadata": map[string]interface{}{ | ||
"labels": map[string]interface{}{}, | ||
"annotations": map[string]interface{}{}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
CleanupCPPClaimMetadata(tt.obj) | ||
assert.Equal(t, tt.obj, tt.result) | ||
}) | ||
} | ||
} |
Oops, something went wrong.