From 71cf6e6beb3046f0065df414ce01901d9c3bb8d7 Mon Sep 17 00:00:00 2001 From: Joe Lanford Date: Wed, 13 Jan 2021 14:41:47 -0500 Subject: [PATCH] pkg/client: skip owner reference injection when helm keep annotation is present --- pkg/client/actionclient.go | 15 ++++++++++++++- pkg/client/actionclient_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/pkg/client/actionclient.go b/pkg/client/actionclient.go index 9589c467..b9824bdd 100644 --- a/pkg/client/actionclient.go +++ b/pkg/client/actionclient.go @@ -21,6 +21,7 @@ import ( "encoding/json" "errors" "fmt" + "strings" sdkhandler "github.com/operator-framework/operator-lib/handler" "gomodules.xyz/jsonpatch/v2" @@ -325,7 +326,7 @@ func (pr *ownerPostRenderer) Run(in *bytes.Buffer) (*bytes.Buffer, error) { if err != nil { return err } - if useOwnerRef { + if useOwnerRef && !containsResourcePolicyKeep(u.GetAnnotations()) { ownerRef := metav1.NewControllerRef(pr.owner, pr.owner.GetObjectKind().GroupVersionKind()) ownerRefs := append(u.GetOwnerReferences(), *ownerRef) u.SetOwnerReferences(ownerRefs) @@ -348,3 +349,15 @@ func (pr *ownerPostRenderer) Run(in *bytes.Buffer) (*bytes.Buffer, error) { } return &out, nil } + +func containsResourcePolicyKeep(annotations map[string]string) bool { + if annotations == nil { + return false + } + resourcePolicyType, ok := annotations[kube.ResourcePolicyAnno] + if !ok { + return false + } + resourcePolicyType = strings.ToLower(strings.TrimSpace(resourcePolicyType)) + return resourcePolicyType == kube.KeepPolicy +} diff --git a/pkg/client/actionclient_test.go b/pkg/client/actionclient_test.go index 4be1d0f0..25ab437c 100644 --- a/pkg/client/actionclient_test.go +++ b/pkg/client/actionclient_test.go @@ -21,6 +21,7 @@ import ( "context" "errors" "strconv" + "strings" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -543,6 +544,33 @@ var _ = Describe("ActionClient", func() { Expect(err).NotTo(BeNil()) }) }) + + var _ = Describe("containsResourcePolicyKeep", func() { + It("returns true on base case", func() { + annotations := map[string]string{kube.ResourcePolicyAnno: kube.KeepPolicy} + Expect(containsResourcePolicyKeep(annotations)).To(BeTrue()) + }) + It("returns false when annotation key is not found", func() { + annotations := map[string]string{"not-" + kube.ResourcePolicyAnno: kube.KeepPolicy} + Expect(containsResourcePolicyKeep(annotations)).To(BeFalse()) + }) + It("returns false when annotation value is not 'keep'", func() { + annotations := map[string]string{"not-" + kube.ResourcePolicyAnno: "not-" + kube.KeepPolicy} + Expect(containsResourcePolicyKeep(annotations)).To(BeFalse()) + }) + It("returns true when annotation is uppercase", func() { + annotations := map[string]string{kube.ResourcePolicyAnno: strings.ToUpper(kube.KeepPolicy)} + Expect(containsResourcePolicyKeep(annotations)).To(BeTrue()) + }) + It("returns true when annotation is has whitespace prefix and/or suffix", func() { + annotations := map[string]string{kube.ResourcePolicyAnno: " " + kube.KeepPolicy + " "} + Expect(containsResourcePolicyKeep(annotations)).To(BeTrue()) + }) + It("returns true when annotation is uppercase and has whitespace prefix and/or suffix", func() { + annotations := map[string]string{kube.ResourcePolicyAnno: " " + strings.ToUpper(kube.KeepPolicy) + " "} + Expect(containsResourcePolicyKeep(annotations)).To(BeTrue()) + }) + }) }) func manifestToObjects(manifest string) []client.Object {