Skip to content

Commit 39c5605

Browse files
authored
Merge pull request #273 from Liujingfang1/master
add the new lifecycle annotation for deletion
2 parents 4fb20bf + 4cda17f commit 39c5605

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

pkg/apply/prune/prune.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,8 @@ func mergeObjNamespaces(objs []*unstructured.Unstructured) sets.String {
175175
// annotation exists within the annotation map; false otherwise.
176176
func preventDeleteAnnotation(annotations map[string]string) bool {
177177
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
182180
}
183181
}
184182
return false

pkg/apply/prune/prune_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,18 @@ func TestPreventDeleteAnnotation(t *testing.T) {
268268
},
269269
expected: true,
270270
},
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+
},
271283
}
272284
for name, tc := range tests {
273285
t.Run(name, func(t *testing.T) {

pkg/common/common.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ const (
3232
// DefaultFieldManager is default owner of applied fields in
3333
// server-side apply.
3434
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"
3542
)
3643

3744
// RandomStr returns an eight-digit (with leading zeros) string of a
@@ -42,6 +49,19 @@ func RandomStr(seed int64) string {
4249
return fmt.Sprintf("%08d", randomInt)
4350
}
4451

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+
4565
var Strategies = []DryRunStrategy{DryRunClient, DryRunServer}
4666

4767
type DryRunStrategy int

0 commit comments

Comments
 (0)