Skip to content

Commit

Permalink
fix(helm): Allow custom resources in hooks (#4986)
Browse files Browse the repository at this point in the history
Currently the code that handles hooks uses a builder that creates the versioned types rather than unstructured. This results in an error whenever a custom resource is used in the hook as the type will not be registered in the scheme used in Helm. This changes this to use a builder that created unstructured resources and only converts to the versioned type when needed.

Signed-off-by: Morten Torkildsen <mortent@google.com>
  • Loading branch information
mortent authored and Matthew Fisher committed Nov 29, 2018
1 parent e2a0e7f commit 55a3385
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions pkg/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,11 @@ func (c *Client) Get(namespace string, reader io.Reader) (string, error) {
vk := gvk.Version + "/" + gvk.Kind
internalObj, err := asInternal(info)
if err != nil {
c.Log("Warning: conversion to internal type failed: %v", err)
// If the problem is just that the resource is not registered, don't print any
// error. This is normal for custom resources.
if !runtime.IsNotRegisteredError(err) {
c.Log("Warning: conversion to internal type failed: %v", err)
}
// Add the unstructured object in this situation. It will still get listed, just
// with less information.
objs[vk] = append(objs[vk], info.Object)
Expand Down Expand Up @@ -358,7 +362,7 @@ func (c *Client) watchTimeout(t time.Duration) ResourceActorFunc {
//
// Handling for other kinds will be added as necessary.
func (c *Client) WatchUntilReady(namespace string, reader io.Reader, timeout int64, shouldWait bool) error {
infos, err := c.Build(namespace, reader)
infos, err := c.BuildUnstructured(namespace, reader)
if err != nil {
return err
}
Expand Down Expand Up @@ -605,20 +609,21 @@ func (c *Client) watchUntilReady(timeout time.Duration, info *resource.Info) err
//
// 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)
if !ok {
return true, fmt.Errorf("Expected %s to be a *batch.Job, got %T", name, e.Object)
job := &batch.Job{}
err := legacyscheme.Scheme.Convert(e.Object, job, nil)
if err != nil {
return true, err
}

for _, c := range o.Status.Conditions {
for _, c := range job.Status.Conditions {
if c.Type == batch.JobComplete && c.Status == v1.ConditionTrue {
return true, nil
} else if c.Type == batch.JobFailed && c.Status == v1.ConditionTrue {
return true, fmt.Errorf("Job failed: %s", c.Reason)
}
}

c.Log("%s: Jobs active: %d, jobs failed: %d, jobs succeeded: %d", name, o.Status.Active, o.Status.Failed, o.Status.Succeeded)
c.Log("%s: Jobs active: %d, jobs failed: %d, jobs succeeded: %d", name, job.Status.Active, job.Status.Failed, job.Status.Succeeded)
return false, nil
}

Expand Down

0 comments on commit 55a3385

Please sign in to comment.