File tree Expand file tree Collapse file tree 3 files changed +34
-4
lines changed Expand file tree Collapse file tree 3 files changed +34
-4
lines changed Original file line number Diff line number Diff line change @@ -175,10 +175,8 @@ func mergeObjNamespaces(objs []*unstructured.Unstructured) sets.String {
175
175
// annotation exists within the annotation map; false otherwise.
176
176
func preventDeleteAnnotation (annotations map [string ]string ) bool {
177
177
for annotation , value := range annotations {
178
- if annotation == common .OnRemoveAnnotation {
179
- if value == common .OnRemoveKeep {
180
- return true
181
- }
178
+ if common .NoDeletion (annotation , value ) {
179
+ return true
182
180
}
183
181
}
184
182
return false
Original file line number Diff line number Diff line change @@ -268,6 +268,18 @@ func TestPreventDeleteAnnotation(t *testing.T) {
268
268
},
269
269
expected : true ,
270
270
},
271
+ "Annotation key client.lifecycle.config.k8s.io/deletion without value is false" : {
272
+ annotations : map [string ]string {
273
+ common .LifecycleDeleteAnnotation : "any" ,
274
+ },
275
+ expected : false ,
276
+ },
277
+ "Annotation key client.lifecycle.config.k8s.io/deletion and value is true" : {
278
+ annotations : map [string ]string {
279
+ common .LifecycleDeleteAnnotation : common .PreventDeletion ,
280
+ },
281
+ expected : true ,
282
+ },
271
283
}
272
284
for name , tc := range tests {
273
285
t .Run (name , func (t * testing.T ) {
Original file line number Diff line number Diff line change @@ -32,6 +32,13 @@ const (
32
32
// DefaultFieldManager is default owner of applied fields in
33
33
// server-side apply.
34
34
DefaultFieldManager = "kubectl"
35
+
36
+ // LifecycleDeletionAnnotation is the lifecycle annotation key for deletion operation.
37
+ LifecycleDeleteAnnotation = "client.lifecycle.config.k8s.io/deletion"
38
+
39
+ // PreventDeletion is the value used with LifecycleDeletionAnnotation
40
+ // to prevent deleting a resource.
41
+ PreventDeletion = "detach"
35
42
)
36
43
37
44
// RandomStr returns an eight-digit (with leading zeros) string of a
@@ -42,6 +49,19 @@ func RandomStr(seed int64) string {
42
49
return fmt .Sprintf ("%08d" , randomInt )
43
50
}
44
51
52
+ // NoDeletion checks the passed in annotation key and value and returns
53
+ // true if that matches with the prevent deletion annotation.
54
+ func NoDeletion (key , value string ) bool {
55
+ m := map [string ]string {
56
+ LifecycleDeleteAnnotation : PreventDeletion ,
57
+ OnRemoveAnnotation : OnRemoveKeep ,
58
+ }
59
+ if val , found := m [key ]; found {
60
+ return val == value
61
+ }
62
+ return false
63
+ }
64
+
45
65
var Strategies = []DryRunStrategy {DryRunClient , DryRunServer }
46
66
47
67
type DryRunStrategy int
You can’t perform that action at this time.
0 commit comments