From 0c96138afa423b7d145c310bd1032c5348bd6dba Mon Sep 17 00:00:00 2001 From: Fabian Ruff Date: Sat, 2 Nov 2019 00:18:21 +0100 Subject: [PATCH] Consider namespace when comparing resources Fixes #6857 X-Cherry-Picked: 7f7e90b4078d412e2ff36d8220409ff8497c93a1 Signed-off-by: Mike Lundy --- pkg/kube/client.go | 17 +++++++++-------- pkg/kube/client_test.go | 2 +- pkg/kube/result.go | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/pkg/kube/client.go b/pkg/kube/client.go index 20edb3ba0..7e3991f73 100644 --- a/pkg/kube/client.go +++ b/pkg/kube/client.go @@ -339,7 +339,7 @@ func (c *Client) Get(namespace string, reader io.Reader) (string, error) { err = perform(tinfos, func(info *resource.Info) error { mux.Lock() defer mux.Unlock() - c.Log("Doing get for %s: %q", info.Mapping.GroupVersionKind.Kind, info.Name) + c.Log("Doing get for %s: %q in %q", info.Mapping.GroupVersionKind.Kind, info.Name, info.Namespace) if err := info.Get(); err != nil { c.Log("WARNING: Failed Get for resource %q: %s", info.Name, err) missing = append(missing, fmt.Sprintf("%v\t\t%s", info.Mapping.Resource, info.Name)) @@ -515,7 +515,7 @@ func (c *Client) UpdateWithOptions(namespace string, originalReader, targetReade newlyCreatedResources = append(newlyCreatedResources, info) kind := info.Mapping.GroupVersionKind.Kind - c.Log("Created a new %s called %q\n", kind, info.Name) + c.Log("Created a new %s called %q in %s\n", kind, info.Name, info.Namespace) return nil } @@ -528,9 +528,10 @@ func (c *Client) UpdateWithOptions(namespace string, originalReader, targetReade // See https://github.com/helm/helm/issues/1193 for more info. if originalInfo == nil { return fmt.Errorf( - "kind %s with the name %q already exists in the cluster and wasn't defined in the previous release. Before upgrading, please either delete the resource from the cluster or remove it from the chart", + "kind %s with the name %q in %q already exists in the cluster and wasn't defined in the previous release. Before upgrading, please either delete the resource from the cluster or remove it from the chart", info.Mapping.GroupVersionKind.Kind, info.Name, + info.Namespace, ) } @@ -846,7 +847,7 @@ func updateResource(c *Client, target *resource.Info, currentObj runtime.Object, return fmt.Errorf("failed to create patch: %s", err) } if patch == nil { - c.Log("Looks like there are no changes for %s %q", target.Mapping.GroupVersionKind.Kind, target.Name) + c.Log("Looks like there are no changes for %s %q in %q", target.Mapping.GroupVersionKind.Kind, target.Name, target.Namespace) // This needs to happen to make sure that tiller has the latest info from the API // Otherwise there will be no labels and other functions that use labels will panic if err := target.Get(); err != nil { @@ -859,20 +860,20 @@ func updateResource(c *Client, target *resource.Info, currentObj runtime.Object, obj, err := helper.Patch(target.Namespace, target.Name, patchType, patch, nil) if err != nil { kind := target.Mapping.GroupVersionKind.Kind - log.Printf("Cannot patch %s: %q (%v)", kind, target.Name, err) + log.Printf("Cannot patch %s: %q in %q (%v)", kind, target.Name, target.Namespace, err) if force { // Attempt to delete... if err := deleteResource(target); err != nil { return err } - log.Printf("Deleted %s: %q", kind, target.Name) + log.Printf("Deleted %s: %q in %q", kind, target.Name, target.Namespace) // ... and recreate if err := createResource(target); err != nil { return fmt.Errorf("Failed to recreate resource: %s", err) } - log.Printf("Created a new %s called %q\n", kind, target.Name) + log.Printf("Created a new %s called %q in %s\n", kind, target.Name, target.Namespace) // No need to refresh the target, as we recreated the resource based // on it. In addition, it might not exist yet and a call to `Refresh` @@ -973,7 +974,7 @@ func (c *Client) watchUntilReady(timeout time.Duration, info *resource.Info) err lw := cachetools.NewListWatchFromClient(info.Client, info.Mapping.Resource.Resource, info.Namespace, selector) kind := info.Mapping.GroupVersionKind.Kind - c.Log("Watching for changes to %s %s with timeout of %v", kind, info.Name, timeout) + c.Log("Watching for changes to %s %s in %s with timeout of %v", kind, info.Name, info.Namespace, timeout) // What we watch for depends on the Kind. // - For a Job, we watch for completion. diff --git a/pkg/kube/client_test.go b/pkg/kube/client_test.go index 8a039eccd..5add37283 100644 --- a/pkg/kube/client_test.go +++ b/pkg/kube/client_test.go @@ -302,7 +302,7 @@ func TestUpdateNonManagedResourceError(t *testing.T) { } if err := c.Update(v1.NamespaceDefault, objBody(¤t), objBody(&target), false, false, 0, false); err != nil { - if err.Error() != "kind Pod with the name \"starfish\" already exists in the cluster and wasn't defined in the previous release. Before upgrading, please either delete the resource from the cluster or remove it from the chart" { + if err.Error() != "kind Pod with the name \"starfish\" in \"default\" already exists in the cluster and wasn't defined in the previous release. Before upgrading, please either delete the resource from the cluster or remove it from the chart" { t.Fatal(err) } } else { diff --git a/pkg/kube/result.go b/pkg/kube/result.go index cf4a4195e..6cfcc4935 100644 --- a/pkg/kube/result.go +++ b/pkg/kube/result.go @@ -83,5 +83,5 @@ func (r Result) Intersect(rs Result) Result { // isMatchingInfo returns true if infos match on Name and GroupVersionKind. func isMatchingInfo(a, b *resource.Info) bool { - return a.Name == b.Name && a.Mapping.GroupVersionKind.Kind == b.Mapping.GroupVersionKind.Kind + return a.Name == b.Name && a.Namespace == b.Namespace && a.Mapping.GroupVersionKind.Kind == b.Mapping.GroupVersionKind.Kind }