feat(resource-policy): delete manifests when policy value is delete

Signed-off-by: 's avatarJacob LeGrone <git@jacob.work>
parent 33589472
......@@ -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
}
......
......@@ -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
}
......@@ -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
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment