From 189b5a12293e1712d63065b58884af6cca622eb3 Mon Sep 17 00:00:00 2001 From: Taylor Thomas Date: Fri, 26 Jul 2019 17:01:27 -0600 Subject: [PATCH] Fixed object typing for watching Jobs/hooks Signed-off-by: Taylor Thomas --- pkg/kube/client.go | 11 +++++++---- pkg/kube/converter.go | 17 ++++++++++++----- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/pkg/kube/client.go b/pkg/kube/client.go index 9ddc42645..25a141dab 100644 --- a/pkg/kube/client.go +++ b/pkg/kube/client.go @@ -387,6 +387,9 @@ func (c *Client) watchUntilReady(timeout time.Duration, info *resource.Info) err ctx, cancel := watchtools.ContextWithOptionalTimeout(context.Background(), timeout) defer cancel() _, err = watchtools.UntilWithoutRetry(ctx, w, func(e watch.Event) (bool, error) { + // Make sure the incoming object is versioned as we use unstructured + // objects when we build manifests + obj := convertWithMapper(e.Object, info.Mapping) switch e.Type { case watch.Added, watch.Modified: // For things like a secret or a config map, this is the best indicator @@ -395,7 +398,7 @@ func (c *Client) watchUntilReady(timeout time.Duration, info *resource.Info) err // we don't really do anything to support these as hooks. c.Log("Add/Modify event for %s: %v", info.Name, e.Type) if kind == "Job" { - return c.waitForJob(e, info.Name) + return c.waitForJob(obj, info.Name) } return true, nil case watch.Deleted: @@ -415,10 +418,10 @@ func (c *Client) watchUntilReady(timeout time.Duration, info *resource.Info) err // waitForJob is a helper that waits for a job to complete. // // This operates on an event returned from a watcher. -func (c *Client) waitForJob(e watch.Event, name string) (bool, error) { - o, ok := e.Object.(*batch.Job) +func (c *Client) waitForJob(obj runtime.Object, name string) (bool, error) { + o, ok := obj.(*batch.Job) if !ok { - return true, errors.Errorf("expected %s to be a *batch.Job, got %T", name, e.Object) + return true, errors.Errorf("expected %s to be a *batch.Job, got %T", name, obj) } for _, c := range o.Status.Conditions { diff --git a/pkg/kube/converter.go b/pkg/kube/converter.go index 92b56ab6c..d298ad623 100644 --- a/pkg/kube/converter.go +++ b/pkg/kube/converter.go @@ -17,6 +17,7 @@ limitations under the License. package kube // import "helm.sh/helm/pkg/kube" import ( + "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/cli-runtime/pkg/resource" @@ -26,12 +27,18 @@ import ( // AsVersioned converts the given info into a runtime.Object with the correct // group and version set func AsVersioned(info *resource.Info) runtime.Object { - gv := runtime.GroupVersioner(schema.GroupVersions(scheme.Scheme.PrioritizedVersionsAllGroups())) - if info.Mapping != nil { - gv = info.Mapping.GroupVersionKind.GroupVersion() + return convertWithMapper(info.Object, info.Mapping) +} + +// convertWithMapper converts the given object with the optional provided +// RESTMapping. If no mapping is provided, the default schema versioner is used +func convertWithMapper(obj runtime.Object, mapping *meta.RESTMapping) runtime.Object { + var gv = runtime.GroupVersioner(schema.GroupVersions(scheme.Scheme.PrioritizedVersionsAllGroups())) + if mapping != nil { + gv = mapping.GroupVersionKind.GroupVersion() } - if obj, err := runtime.ObjectConvertor(scheme.Scheme).ConvertToVersion(info.Object, gv); err == nil { + if obj, err := runtime.ObjectConvertor(scheme.Scheme).ConvertToVersion(obj, gv); err == nil { return obj } - return info.Object + return obj }