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 ( ...@@ -23,12 +23,13 @@ import (
goerrors "errors" goerrors "errors"
"fmt" "fmt"
"io" "io"
"k8s.io/apimachinery/pkg/api/meta"
"log" "log"
"sort" "sort"
"strings" "strings"
"time" "time"
"k8s.io/apimachinery/pkg/api/meta"
"github.com/evanphx/json-patch" "github.com/evanphx/json-patch"
appsv1 "k8s.io/api/apps/v1" appsv1 "k8s.io/api/apps/v1"
appsv1beta1 "k8s.io/api/apps/v1beta1" appsv1beta1 "k8s.io/api/apps/v1beta1"
...@@ -362,8 +363,9 @@ func (c *Client) Update(namespace string, originalReader, targetReader io.Reader ...@@ -362,8 +363,9 @@ func (c *Client) Update(namespace string, originalReader, targetReader io.Reader
if err != nil { if err != nil {
c.Log("Unable to get annotations on %q, err: %s", info.Name, err) c.Log("Unable to get annotations on %q, err: %s", info.Name, err)
} }
if annotations != nil && annotations[ResourcePolicyAnno] == KeepPolicy { if ResourcePolicyIsKeep(annotations) {
c.Log("Skipping delete of %q due to annotation [%s=%s]", info.Name, ResourcePolicyAnno, KeepPolicy) policy := annotations[ResourcePolicyAnno]
c.Log("Skipping delete of %q due to annotation [%s=%s]", info.Name, ResourcePolicyAnno, policy)
continue continue
} }
......
...@@ -19,8 +19,23 @@ package kube ...@@ -19,8 +19,23 @@ package kube
// ResourcePolicyAnno is the annotation name for a resource policy // ResourcePolicyAnno is the annotation name for a resource policy
const ResourcePolicyAnno = "helm.sh/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 // This resource policy type allows explicitly opting in to the default
// during an uninstallRelease action. // resource deletion behavior, for example when overriding a chart's
const KeepPolicy = "keep" // 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) { ...@@ -34,17 +34,11 @@ func filterManifestsToKeep(manifests []Manifest) ([]Manifest, []Manifest) {
continue continue
} }
resourcePolicyType, ok := m.Head.Metadata.Annotations[kube.ResourcePolicyAnno] if kube.ResourcePolicyIsKeep(m.Head.Metadata.Annotations) {
if !ok {
remaining = append(remaining, m)
continue
}
resourcePolicyType = strings.ToLower(strings.TrimSpace(resourcePolicyType))
if resourcePolicyType == kube.KeepPolicy {
keep = append(keep, m) keep = append(keep, m)
} else {
remaining = append(remaining, m)
} }
} }
return keep, remaining 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