From 68d0483890f0a63f784f0f014e032b3a5f40e149 Mon Sep 17 00:00:00 2001 From: Liu Ming Date: Wed, 2 Sep 2020 11:22:26 +0800 Subject: [PATCH] fix: rollback to release revision containing resource with keep-resource-policy annotation When rollback to a previous release revision that contains a resource having the annotaion `helm.sh/resource-policy=keep`, while the current release revision dosen't have this resource, rollback option will give `Error: no XXX with the name "XXX" found` error message. We should not care about weather the resource with annotaion `helm.sh/resource-policy=keep` exists in the current release revison or not. fix #8228 Signed-off-by: Liu Ming --- pkg/kube/client.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/pkg/kube/client.go b/pkg/kube/client.go index decfc2e6e..f4f68d42d 100644 --- a/pkg/kube/client.go +++ b/pkg/kube/client.go @@ -177,8 +177,11 @@ func (c *Client) Update(original, target ResourceList, force bool) (*Result, err return err } + kind := info.Mapping.GroupVersionKind.Kind + helper := resource.NewHelper(info.Client, info.Mapping) - if _, err := helper.Get(info.Namespace, info.Name, info.Export); err != nil { + currentObject, err := helper.Get(info.Namespace, info.Name, info.Export) + if err != nil { if !apierrors.IsNotFound(err) { return errors.Wrap(err, "could not get information about the resource") } @@ -191,14 +194,25 @@ func (c *Client) Update(original, target ResourceList, force bool) (*Result, err return errors.Wrap(err, "failed to create resource") } - kind := info.Mapping.GroupVersionKind.Kind c.Log("Created a new %s called %q in %s\n", kind, info.Name, info.Namespace) return nil } + // Figure out whether this resource in the target release revision has + // annotation "helm.sh/resource-policy=keep", if yes, Helm should not + // manage this resource anymore. + annotations, err := metadataAccessor.Annotations(currentObject) + if err != nil { + return errors.Errorf("failed to get annotations of kind %s with the name %q", kind, info.Name) + } + if annotations != nil { + if val, exist := annotations[ResourcePolicyAnno]; exist && val == KeepPolicy { + return nil + } + } + originalInfo := original.Get(info) if originalInfo == nil { - kind := info.Mapping.GroupVersionKind.Kind return errors.Errorf("no %s with the name %q found", kind, info.Name) }