Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/ansible/proxy/inject_owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ func (i *injectOwnerReferenceHandler) ServeHTTP(w http.ResponseWriter, req *http
http.Error(w, m, http.StatusBadRequest)
return
}
if addOwnerRef {
// If the resource contains the Helm resource-policy keep annotation, then do not add
// the owner reference. So when the CR is deleted, Kubernetes won't GCs the resource.
if addOwnerRef && !k8sutil.ContainsResourcePolicyKeep(data.GetAnnotations()) {
data.SetOwnerReferences(append(data.GetOwnerReferences(), owner.OwnerReference))
} else {
err := handler.SetOwnerAnnotations(data, ownerObject)
Expand Down
15 changes: 1 addition & 14 deletions internal/helm/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package client
import (
"errors"
"io"
"strings"

"github.com/operator-framework/operator-lib/handler"
"github.com/operator-framework/operator-sdk/internal/util/k8sutil"
Expand Down Expand Up @@ -147,7 +146,7 @@ func (c *ownerRefInjectingClient) Build(reader io.Reader, validate bool) (kube.R

// If the resource contains the Helm resource-policy keep annotation, then do not add
// the owner reference. So when the CR is deleted, Kubernetes won't GCs the resource.
if useOwnerRef && !containsResourcePolicyKeep(u.GetAnnotations()) {
if useOwnerRef && !k8sutil.ContainsResourcePolicyKeep(u.GetAnnotations()) {
ownerRef := metav1.NewControllerRef(c.owner, c.owner.GroupVersionKind())
u.SetOwnerReferences([]metav1.OwnerReference{*ownerRef})
} else {
Expand All @@ -163,15 +162,3 @@ func (c *ownerRefInjectingClient) Build(reader io.Reader, validate bool) (kube.R
}
return resourceList, 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
}
79 changes: 0 additions & 79 deletions internal/helm/client/client_test.go

This file was deleted.

13 changes: 13 additions & 0 deletions internal/util/k8sutil/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"
"unicode"

"helm.sh/helm/v3/pkg/kube"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -156,3 +157,15 @@ func SupportsOwnerReference(restMapper meta.RESTMapper, owner, dependent runtime
// Both owner and dependent are namespace-scoped and in the same namespace.
return true, 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
}
58 changes: 58 additions & 0 deletions internal/util/k8sutil/k8sutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
package k8sutil

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"helm.sh/helm/v3/pkg/kube"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -332,3 +334,59 @@ func TestFormatOperatorNameDNS1123(t *testing.T) {
})
}
}

func TestContainsResourcePolicyKeep(t *testing.T) {
tests := []struct {
input map[string]string
expectedVal bool
expectedOut string
name string
}{
{
input: map[string]string{
kube.ResourcePolicyAnno: kube.KeepPolicy,
},
expectedVal: true,
name: "base case true",
},
{
input: map[string]string{
"not-" + kube.ResourcePolicyAnno: kube.KeepPolicy,
},
expectedVal: false,
name: "base case annotation false",
},
{
input: map[string]string{
kube.ResourcePolicyAnno: "not-" + kube.KeepPolicy,
},
expectedVal: false,
name: "base case value false",
},
{
input: map[string]string{
kube.ResourcePolicyAnno: strings.ToUpper(kube.KeepPolicy),
},
expectedVal: true,
name: "true with upper case",
},
{
input: map[string]string{
kube.ResourcePolicyAnno: " " + kube.KeepPolicy + " ",
},
expectedVal: true,
name: "true with spaces",
},
{
input: map[string]string{
kube.ResourcePolicyAnno: " " + strings.ToUpper(kube.KeepPolicy) + " ",
},
expectedVal: true,
name: "true with upper case and spaces",
},
}

for _, test := range tests {
assert.Equal(t, test.expectedVal, ContainsResourcePolicyKeep(test.input), test.name)
}
}