Skip to content

Commit d6f0f67

Browse files
authored
fix: (helm) - do not add owner references to resources that contain the Helm keep resource-policy annotation (#71)
1 parent 7cad81a commit d6f0f67

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

pkg/client/actionclient.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/json"
2222
"errors"
2323
"fmt"
24+
"strings"
2425

2526
sdkhandler "github.com/operator-framework/operator-lib/handler"
2627
"gomodules.xyz/jsonpatch/v2"
@@ -325,7 +326,7 @@ func (pr *ownerPostRenderer) Run(in *bytes.Buffer) (*bytes.Buffer, error) {
325326
if err != nil {
326327
return err
327328
}
328-
if useOwnerRef {
329+
if useOwnerRef && !containsResourcePolicyKeep(u.GetAnnotations()) {
329330
ownerRef := metav1.NewControllerRef(pr.owner, pr.owner.GetObjectKind().GroupVersionKind())
330331
ownerRefs := append(u.GetOwnerReferences(), *ownerRef)
331332
u.SetOwnerReferences(ownerRefs)
@@ -348,3 +349,15 @@ func (pr *ownerPostRenderer) Run(in *bytes.Buffer) (*bytes.Buffer, error) {
348349
}
349350
return &out, nil
350351
}
352+
353+
func containsResourcePolicyKeep(annotations map[string]string) bool {
354+
if annotations == nil {
355+
return false
356+
}
357+
resourcePolicyType, ok := annotations[kube.ResourcePolicyAnno]
358+
if !ok {
359+
return false
360+
}
361+
resourcePolicyType = strings.ToLower(strings.TrimSpace(resourcePolicyType))
362+
return resourcePolicyType == kube.KeepPolicy
363+
}

pkg/client/actionclient_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"context"
2222
"errors"
2323
"strconv"
24+
"strings"
2425

2526
. "github.com/onsi/ginkgo"
2627
. "github.com/onsi/gomega"
@@ -543,6 +544,33 @@ var _ = Describe("ActionClient", func() {
543544
Expect(err).NotTo(BeNil())
544545
})
545546
})
547+
548+
var _ = Describe("containsResourcePolicyKeep", func() {
549+
It("returns true on base case", func() {
550+
annotations := map[string]string{kube.ResourcePolicyAnno: kube.KeepPolicy}
551+
Expect(containsResourcePolicyKeep(annotations)).To(BeTrue())
552+
})
553+
It("returns false when annotation key is not found", func() {
554+
annotations := map[string]string{"not-" + kube.ResourcePolicyAnno: kube.KeepPolicy}
555+
Expect(containsResourcePolicyKeep(annotations)).To(BeFalse())
556+
})
557+
It("returns false when annotation value is not 'keep'", func() {
558+
annotations := map[string]string{"not-" + kube.ResourcePolicyAnno: "not-" + kube.KeepPolicy}
559+
Expect(containsResourcePolicyKeep(annotations)).To(BeFalse())
560+
})
561+
It("returns true when annotation is uppercase", func() {
562+
annotations := map[string]string{kube.ResourcePolicyAnno: strings.ToUpper(kube.KeepPolicy)}
563+
Expect(containsResourcePolicyKeep(annotations)).To(BeTrue())
564+
})
565+
It("returns true when annotation is has whitespace prefix and/or suffix", func() {
566+
annotations := map[string]string{kube.ResourcePolicyAnno: " " + kube.KeepPolicy + " "}
567+
Expect(containsResourcePolicyKeep(annotations)).To(BeTrue())
568+
})
569+
It("returns true when annotation is uppercase and has whitespace prefix and/or suffix", func() {
570+
annotations := map[string]string{kube.ResourcePolicyAnno: " " + strings.ToUpper(kube.KeepPolicy) + " "}
571+
Expect(containsResourcePolicyKeep(annotations)).To(BeTrue())
572+
})
573+
})
546574
})
547575

548576
func manifestToObjects(manifest string) []client.Object {

0 commit comments

Comments
 (0)