diff --git a/pkg/kube/client.go b/pkg/kube/client.go index eccf888e8..955c75ab1 100644 --- a/pkg/kube/client.go +++ b/pkg/kube/client.go @@ -23,12 +23,13 @@ import ( goerrors "errors" "fmt" "io" - "k8s.io/apimachinery/pkg/api/meta" "log" "sort" "strings" "time" + "k8s.io/apimachinery/pkg/api/meta" + "github.com/evanphx/json-patch" appsv1 "k8s.io/api/apps/v1" appsv1beta1 "k8s.io/api/apps/v1beta1" @@ -362,8 +363,9 @@ func (c *Client) Update(namespace string, originalReader, targetReader io.Reader if err != nil { c.Log("Unable to get annotations on %q, err: %s", info.Name, err) } - if annotations != nil && annotations[ResourcePolicyAnno] == KeepPolicy { - c.Log("Skipping delete of %q due to annotation [%s=%s]", info.Name, ResourcePolicyAnno, KeepPolicy) + if ResourcePolicyIsKeep(annotations) { + policy := annotations[ResourcePolicyAnno] + c.Log("Skipping delete of %q due to annotation [%s=%s]", info.Name, ResourcePolicyAnno, policy) continue } diff --git a/pkg/kube/resource_policy.go b/pkg/kube/resource_policy.go index 45cebcba8..9bce63a7c 100644 --- a/pkg/kube/resource_policy.go +++ b/pkg/kube/resource_policy.go @@ -19,8 +19,23 @@ package kube // ResourcePolicyAnno is the annotation name for a resource policy const ResourcePolicyAnno = "helm.sh/resource-policy" -// KeepPolicy is the resource policy type for keep +// deletePolicy is the resource policy type for delete // -// This resource policy type allows resources to skip being deleted -// during an uninstallRelease action. -const KeepPolicy = "keep" +// This resource policy type allows explicitly opting in to the default +// resource deletion behavior, for example when overriding a chart's +// default annotations. Any other value allows resources to skip being +// deleted during an uninstallRelease action. +const deletePolicy = "delete" + +// ResourcePolicyIsKeep accepts a map of Kubernetes resource annotations and +// returns true if the resource should be kept, otherwise false if it is safe +// for Helm to delete. +func ResourcePolicyIsKeep(annotations map[string]string) bool { + if annotations != nil { + resourcePolicyType, ok := annotations[ResourcePolicyAnno] + if ok && resourcePolicyType != deletePolicy { + return true + } + } + return false +} diff --git a/pkg/tiller/resource_policy.go b/pkg/tiller/resource_policy.go index aa9c5d2bd..c97621fcd 100644 --- a/pkg/tiller/resource_policy.go +++ b/pkg/tiller/resource_policy.go @@ -34,17 +34,11 @@ func filterManifestsToKeep(manifests []Manifest) ([]Manifest, []Manifest) { continue } - resourcePolicyType, ok := m.Head.Metadata.Annotations[kube.ResourcePolicyAnno] - if !ok { - remaining = append(remaining, m) - continue - } - - resourcePolicyType = strings.ToLower(strings.TrimSpace(resourcePolicyType)) - if resourcePolicyType == kube.KeepPolicy { + if kube.ResourcePolicyIsKeep(m.Head.Metadata.Annotations) { keep = append(keep, m) + } else { + remaining = append(remaining, m) } - } return keep, remaining }