option to wait for jobs

Signed-off-by: Austin Abro <AustinAbro321@gmail.com>
pull/13604/head
Austin Abro 9 months ago
parent a6e5466942
commit 7b896df4d1
No known key found for this signature in database
GPG Key ID: 92EB5159E403F9D6

@ -22,6 +22,7 @@ import (
"fmt" "fmt"
"time" "time"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/aggregator" "sigs.k8s.io/cli-utils/pkg/kstatus/polling/aggregator"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/collector" "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 { 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) ctx, cancel := context.WithTimeout(context.TODO(), timeout)
defer cancel() defer cancel()
cancelCtx, cancel := context.WithCancel(ctx) 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 // TODO maybe a simpler way to transfer the objects
runtimeObjs := []runtime.Object{} runtimeObjs := []runtime.Object{}
for _, resource := range resourceList { for _, resource := range resourceList {
switch AsVersioned(resource).(type) {
case *batchv1.Job:
if !waitWithJobs {
continue
}
}
runtimeObjs = append(runtimeObjs, resource.Object) runtimeObjs = append(runtimeObjs, resource.Object)
} }
resources := []object.ObjMetadata{} resources := []object.ObjMetadata{}
@ -65,7 +81,6 @@ func (w *kstatusWaiter) Wait(resourceList ResourceList, timeout time.Duration) e
if rs == nil { if rs == nil {
continue continue
} }
fmt.Println("this is the status of object", rs.Status)
rss = append(rss, rs) rss = append(rss, rs)
} }
desired := status.CurrentStatus desired := status.CurrentStatus
@ -96,8 +111,3 @@ func (w *kstatusWaiter) Wait(resourceList ResourceList, timeout time.Duration) e
} }
return nil return nil
} }
func (w *kstatusWaiter) WaitWithJobs(resources ResourceList, timeout time.Duration) error {
// Implementation
panic("not implemented")
}

@ -90,9 +90,10 @@ func getGVR(t *testing.T, mapper meta.RESTMapper, obj *unstructured.Unstructured
func TestKWaitJob(t *testing.T) { func TestKWaitJob(t *testing.T) {
t.Parallel() t.Parallel()
tests := []struct { tests := []struct {
name string name string
objYamls []string objYamls []string
expectErrs []error expectErrs []error
waitForJobs bool
}{ }{
{ {
name: "Job is complete", name: "Job is complete",
@ -100,9 +101,16 @@ func TestKWaitJob(t *testing.T) {
expectErrs: nil, expectErrs: nil,
}, },
{ {
name: "Job is not complete", name: "Job is not complete",
objYamls: []string{jobNoStatus}, objYamls: []string{jobNoStatus},
expectErrs: []error{errors.New("test: Job not ready, status: InProgress"), errors.New("context deadline exceeded")}, 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", name: "Pod is ready",
@ -141,7 +149,7 @@ func TestKWaitJob(t *testing.T) {
err = fakeClient.Tracker().Create(gvr, resource, resource.GetNamespace()) err = fakeClient.Tracker().Create(gvr, resource, resource.GetNamespace())
assert.NoError(t, err) assert.NoError(t, err)
} }
c.Waiter = &kstatusWaiter{ kwaiter := kstatusWaiter{
sw: statusWatcher, sw: statusWatcher,
log: c.Log, log: c.Log,
} }
@ -153,7 +161,7 @@ func TestKWaitJob(t *testing.T) {
resourceList = append(resourceList, list...) resourceList = append(resourceList, list...)
} }
err := c.Wait(resourceList, time.Second*3) err := kwaiter.wait(resourceList, time.Second*3, tt.waitForJobs)
if tt.expectErrs != nil { if tt.expectErrs != nil {
assert.EqualError(t, err, errors.Join(tt.expectErrs...).Error()) assert.EqualError(t, err, errors.Join(tt.expectErrs...).Error())
return return

Loading…
Cancel
Save