Skip to content

Commit

Permalink
Consider namespace when comparing resources
Browse files Browse the repository at this point in the history
Fixes #6857

X-Cherry-Picked: 7f7e90b
Signed-off-by: Mike Lundy <mike@fluffypenguin.org>
  • Loading branch information
databus23 authored and novas0x2a committed Jun 12, 2020
1 parent 7606f08 commit 0c96138
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
17 changes: 9 additions & 8 deletions pkg/kube/client.go
Expand Up @@ -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))
Expand Down Expand Up @@ -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
}

Expand All @@ -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,
)
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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`
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pkg/kube/client_test.go
Expand Up @@ -302,7 +302,7 @@ func TestUpdateNonManagedResourceError(t *testing.T) {
}

if err := c.Update(v1.NamespaceDefault, objBody(&current), 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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kube/result.go
Expand Up @@ -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
}

0 comments on commit 0c96138

Please sign in to comment.