diff --git a/pkg/kube/kwait.go b/pkg/kube/kwait.go index 639794322..c1822d87c 100644 --- a/pkg/kube/kwait.go +++ b/pkg/kube/kwait.go @@ -22,6 +22,7 @@ import ( "fmt" "time" + batchv1 "k8s.io/api/batch/v1" "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/cli-utils/pkg/kstatus/polling/aggregator" "sigs.k8s.io/cli-utils/pkg/kstatus/polling/collector" @@ -39,6 +40,15 @@ type kstatusWaiter struct { } func (w *kstatusWaiter) Wait(resourceList ResourceList, timeout time.Duration) error { + return w.wait(resourceList, timeout, false) +} + +func (w *kstatusWaiter) WaitWithJobs(resourceList ResourceList, timeout time.Duration) error { + // Implementation + return w.wait(resourceList, timeout, true) +} + +func (w *kstatusWaiter) wait(resourceList ResourceList, timeout time.Duration, waitWithJobs bool) error { ctx, cancel := context.WithTimeout(context.TODO(), timeout) defer cancel() cancelCtx, cancel := context.WithCancel(ctx) @@ -46,6 +56,12 @@ func (w *kstatusWaiter) Wait(resourceList ResourceList, timeout time.Duration) e // TODO maybe a simpler way to transfer the objects runtimeObjs := []runtime.Object{} for _, resource := range resourceList { + switch AsVersioned(resource).(type) { + case *batchv1.Job: + if !waitWithJobs { + continue + } + } runtimeObjs = append(runtimeObjs, resource.Object) } resources := []object.ObjMetadata{} @@ -65,7 +81,6 @@ func (w *kstatusWaiter) Wait(resourceList ResourceList, timeout time.Duration) e if rs == nil { continue } - fmt.Println("this is the status of object", rs.Status) rss = append(rss, rs) } desired := status.CurrentStatus @@ -89,15 +104,10 @@ func (w *kstatusWaiter) Wait(resourceList ResourceList, timeout time.Duration) e if rs.Status == status.CurrentStatus { continue } - errs = append(errs, fmt.Errorf("%s: %s not ready, status: %s", rs.Identifier.Name, rs.Identifier.GroupKind.Kind, rs.Status)) + errs = append(errs, fmt.Errorf("%s: %s not ready, status: %s", rs.Identifier.Name, rs.Identifier.GroupKind.Kind, rs.Status)) } errs = append(errs, ctx.Err()) return errors.Join(errs...) } return nil } - -func (w *kstatusWaiter) WaitWithJobs(resources ResourceList, timeout time.Duration) error { - // Implementation - panic("not implemented") -} diff --git a/pkg/kube/kwait_test.go b/pkg/kube/kwait_test.go index fd5cd0b57..e595f9ed3 100644 --- a/pkg/kube/kwait_test.go +++ b/pkg/kube/kwait_test.go @@ -90,9 +90,10 @@ func getGVR(t *testing.T, mapper meta.RESTMapper, obj *unstructured.Unstructured func TestKWaitJob(t *testing.T) { t.Parallel() tests := []struct { - name string - objYamls []string - expectErrs []error + name string + objYamls []string + expectErrs []error + waitForJobs bool }{ { name: "Job is complete", @@ -100,9 +101,16 @@ func TestKWaitJob(t *testing.T) { expectErrs: nil, }, { - name: "Job is not complete", - objYamls: []string{jobNoStatus}, - expectErrs: []error{errors.New("test: Job not ready, status: InProgress"), errors.New("context deadline exceeded")}, + name: "Job is not complete", + objYamls: []string{jobNoStatus}, + expectErrs: []error{errors.New("test: Job not ready, status: InProgress"), errors.New("context deadline exceeded")}, + waitForJobs: true, + }, + { + name: "Job is not ready, but we pass wait anyway", + objYamls: []string{jobNoStatus}, + expectErrs: nil, + waitForJobs: false, }, { name: "Pod is ready", @@ -141,7 +149,7 @@ func TestKWaitJob(t *testing.T) { err = fakeClient.Tracker().Create(gvr, resource, resource.GetNamespace()) assert.NoError(t, err) } - c.Waiter = &kstatusWaiter{ + kwaiter := kstatusWaiter{ sw: statusWatcher, log: c.Log, } @@ -153,7 +161,7 @@ func TestKWaitJob(t *testing.T) { resourceList = append(resourceList, list...) } - err := c.Wait(resourceList, time.Second*3) + err := kwaiter.wait(resourceList, time.Second*3, tt.waitForJobs) if tt.expectErrs != nil { assert.EqualError(t, err, errors.Join(tt.expectErrs...).Error()) return