@@ -23,7 +23,7 @@ type PreInitCallback func(context.Context, JobExecutor) error
2323type Job interface {
2424 Spec () batchv1.JobSpec
2525 PreInit (TestJobContainer , PreInitCallback )
26- RunWithExecutionHandler (context.Context , func ([]JobExecutor ) error , func (JobExecutor ) error ) error
26+ RunWithExecutionHandler (context.Context , func (context. Context , []JobExecutor ) error , func (context. Context , JobExecutor ) error ) error
2727 Mount (func (ctx context.Context , exec JobExecutor , isInitContainer bool ) error )
2828}
2929
@@ -36,7 +36,7 @@ type JobExecutor interface {
3636 CopyTo (context.Context , string , string ) error
3737 Container () corev1.Container
3838 Pod () * corev1.Pod
39- PrepareCommand ([]string ) ([]byte , error )
39+ PrepareCommand (context. Context , []string ) ([]byte , error )
4040}
4141
4242type JobBuilder struct {
@@ -71,6 +71,13 @@ func (b *JobBuilder) BuildWithJob(jobSpec *batchv1.Job, containerNameToInstalled
7171 if err != nil {
7272 return nil , fmt .Errorf ("kubetest: failed to create agent config: %w" , err )
7373 }
74+ if sharedAgentSpec .Timeout != "" {
75+ timeout , err := time .ParseDuration (sharedAgentSpec .Timeout )
76+ if err != nil {
77+ return nil , err
78+ }
79+ cfg .SetTimeout (timeout )
80+ }
7481 if sharedAgentSpec .AllocationStartPort != nil {
7582 cfg .SetAllocationStartPort (* sharedAgentSpec .AllocationStartPort )
7683 }
@@ -94,11 +101,10 @@ func (b *JobBuilder) BuildWithJob(jobSpec *batchv1.Job, containerNameToInstalled
94101}
95102
96103type kubernetesJob struct {
97- preInitCallbackContext context.Context
98- job * kubejob.Job
99- finalizer * corev1.Container
100- agentConfig * kubejob.AgentConfig
101- mountCallback func (context.Context , JobExecutor , bool ) error
104+ job * kubejob.Job
105+ finalizer * corev1.Container
106+ agentConfig * kubejob.AgentConfig
107+ mountCallback func (context.Context , JobExecutor , bool ) error
102108}
103109
104110var defaultMountCallback = func (context.Context , JobExecutor , bool ) error { return nil }
@@ -117,37 +123,36 @@ func (j *kubernetesJob) Spec() batchv1.JobSpec {
117123}
118124
119125func (j * kubernetesJob ) PreInit (c TestJobContainer , cb PreInitCallback ) {
120- j .job .PreInit (c .Container , func (exec * kubejob.JobExecutor ) error {
121- return cb (j . preInitCallbackContext , & kubernetesJobExecutor {exec : exec })
126+ j .job .PreInit (c .Container , func (ctx context. Context , exec * kubejob.JobExecutor ) error {
127+ return cb (ctx , & kubernetesJobExecutor {exec : exec })
122128 })
123129}
124130
125131func (j * kubernetesJob ) Mount (cb func (context.Context , JobExecutor , bool ) error ) {
126132 j .mountCallback = cb
127133}
128134
129- func (j * kubernetesJob ) RunWithExecutionHandler (ctx context.Context , handler func ([]JobExecutor ) error , finalizerHandler func (JobExecutor ) error ) error {
130- j .preInitCallbackContext = ctx
135+ func (j * kubernetesJob ) RunWithExecutionHandler (ctx context.Context , handler func (context.Context , []JobExecutor ) error , finalizerHandler func (context.Context , JobExecutor ) error ) error {
131136 j .job .DisableInitContainerLog ()
132137 j .job .SetPendingPhaseTimeout (10 * time .Minute )
133- j .job .SetInitContainerExecutionHandler (func (exec * kubejob.JobExecutor ) error {
138+ j .job .SetInitContainerExecutionHandler (func (ctx context. Context , exec * kubejob.JobExecutor ) error {
134139 e := & kubernetesJobExecutor {exec : exec }
135140 if err := j .mountCallback (ctx , e , true ); err != nil {
136141 return err
137142 }
138- _ , err := exec .ExecOnly ()
143+ _ , err := exec .ExecOnly (ctx )
139144 return err
140145 })
141146 var finalizer * kubejob.JobFinalizer
142147 if j .finalizer != nil {
143148 finalizer = & kubejob.JobFinalizer {
144149 Container : * j .finalizer ,
145- Handler : func (exec * kubejob.JobExecutor ) error {
146- return finalizerHandler (& kubernetesJobExecutor {exec : exec })
150+ Handler : func (ctx context. Context , exec * kubejob.JobExecutor ) error {
151+ return finalizerHandler (ctx , & kubernetesJobExecutor {exec : exec })
147152 },
148153 }
149154 }
150- return j .job .RunWithExecutionHandler (ctx , func (execs []* kubejob.JobExecutor ) error {
155+ return j .job .RunWithExecutionHandler (ctx , func (ctx context. Context , execs []* kubejob.JobExecutor ) error {
151156 converted := make ([]JobExecutor , 0 , len (execs ))
152157 for _ , exec := range execs {
153158 e := & kubernetesJobExecutor {exec : exec }
@@ -156,27 +161,27 @@ func (j *kubernetesJob) RunWithExecutionHandler(ctx context.Context, handler fun
156161 }
157162 converted = append (converted , e )
158163 }
159- return handler (converted )
164+ return handler (ctx , converted )
160165 }, finalizer )
161166}
162167
163168type kubernetesJobExecutor struct {
164169 exec * kubejob.JobExecutor
165170}
166171
167- func (e * kubernetesJobExecutor ) PrepareCommand (cmd []string ) ([]byte , error ) {
168- return e .exec .ExecPrepareCommand ([]string {"sh" , "-c" , strings .Join (cmd , " " )})
172+ func (e * kubernetesJobExecutor ) PrepareCommand (ctx context. Context , cmd []string ) ([]byte , error ) {
173+ return e .exec .ExecPrepareCommand (ctx , []string {"sh" , "-c" , strings .Join (cmd , " " )})
169174}
170175
171- func (e * kubernetesJobExecutor ) Output (_ context.Context ) ([]byte , error ) {
172- return e .exec .ExecOnly ()
176+ func (e * kubernetesJobExecutor ) Output (ctx context.Context ) ([]byte , error ) {
177+ return e .exec .ExecOnly (ctx )
173178}
174179
175- func (e * kubernetesJobExecutor ) ExecAsync (_ context.Context ) {
176- e .exec .ExecAsync ()
180+ func (e * kubernetesJobExecutor ) ExecAsync (ctx context.Context ) {
181+ e .exec .ExecAsync (ctx )
177182}
178183
179- func (e * kubernetesJobExecutor ) TerminationLog (_ context.Context , log string ) error {
184+ func (e * kubernetesJobExecutor ) TerminationLog (ctx context.Context , log string ) error {
180185 return e .exec .TerminationLog (log )
181186}
182187
@@ -195,14 +200,14 @@ func (e *kubernetesJobExecutor) CopyFrom(ctx context.Context, src string, dst st
195200 containerName := e .exec .Container .Name
196201 addr := e .exec .Pod .Status .PodIP
197202 LoggerFromContext (ctx ).Debug ("copy from %s on container(%s) in %s pod to %s on local by %s" , src , containerName , addr , dst , e .execProtocol ())
198- return e .exec .CopyFromPod (src , dst )
203+ return e .exec .CopyFromPod (ctx , src , dst )
199204}
200205
201206func (e * kubernetesJobExecutor ) CopyTo (ctx context.Context , src string , dst string ) error {
202207 containerName := e .exec .Container .Name
203208 addr := e .exec .Pod .Status .PodIP
204209 LoggerFromContext (ctx ).Debug ("copy from %s on local to %s on container(%s) in %s pod by %s" , src , dst , containerName , addr , e .execProtocol ())
205- return e .exec .CopyToPod (src , dst )
210+ return e .exec .CopyToPod (ctx , src , dst )
206211}
207212
208213func (e * kubernetesJobExecutor ) Container () corev1.Container {
@@ -244,7 +249,7 @@ func (j *localJob) Mount(cb func(context.Context, JobExecutor, bool) error) {
244249 j .mountCallback = cb
245250}
246251
247- func (j * localJob ) RunWithExecutionHandler (ctx context.Context , handler func ([]JobExecutor ) error , finalizer func (JobExecutor ) error ) error {
252+ func (j * localJob ) RunWithExecutionHandler (ctx context.Context , handler func (context. Context , []JobExecutor ) error , finalizer func (context. Context , JobExecutor ) error ) error {
248253 preInitNameToPath := map [string ]string {}
249254 if j .preInitCallback != nil {
250255 j .preInitCallback (ctx , & localJobExecutor {
@@ -269,11 +274,11 @@ func (j *localJob) RunWithExecutionHandler(ctx context.Context, handler func([]J
269274 }
270275 execs = append (execs , e )
271276 }
272- if err := handler (execs ); err != nil {
277+ if err := handler (ctx , execs ); err != nil {
273278 return err
274279 }
275280 if j .finalizer != nil {
276- if err := finalizer (& localJobExecutor {
281+ if err := finalizer (ctx , & localJobExecutor {
277282 rootDir : j .rootDir ,
278283 container : * j .finalizer ,
279284 }); err != nil {
@@ -306,7 +311,7 @@ func (e *localJobExecutor) cmd(cmdarr []string) (*exec.Cmd, error) {
306311 return cmd , nil
307312}
308313
309- func (e * localJobExecutor ) PrepareCommand (cmdarr []string ) ([]byte , error ) {
314+ func (e * localJobExecutor ) PrepareCommand (ctx context. Context , cmdarr []string ) ([]byte , error ) {
310315 filteredCmd := []string {}
311316 for _ , c := range cmdarr {
312317 if strings .HasPrefix (c , "/" ) {
@@ -400,18 +405,18 @@ func (j *dryRunJob) Spec() batchv1.JobSpec {
400405func (j * dryRunJob ) PreInit (c TestJobContainer , cb PreInitCallback ) {}
401406func (j * dryRunJob ) Mount (_ func (context.Context , JobExecutor , bool ) error ) {}
402407
403- func (j * dryRunJob ) RunWithExecutionHandler (ctx context.Context , handler func ([]JobExecutor ) error , finalizer func (JobExecutor ) error ) error {
408+ func (j * dryRunJob ) RunWithExecutionHandler (ctx context.Context , handler func (context. Context , []JobExecutor ) error , finalizer func (context. Context , JobExecutor ) error ) error {
404409 execs := make ([]JobExecutor , 0 , len (j .job .Spec .Template .Spec .Containers ))
405410 for _ , container := range j .job .Spec .Template .Spec .Containers {
406411 execs = append (execs , & dryRunJobExecutor {
407412 container : container ,
408413 })
409414 }
410- if err := handler (execs ); err != nil {
415+ if err := handler (ctx , execs ); err != nil {
411416 return err
412417 }
413418 if j .finalizer != nil {
414- if err := finalizer (& dryRunJobExecutor {
419+ if err := finalizer (ctx , & dryRunJobExecutor {
415420 container : * j .finalizer ,
416421 }); err != nil {
417422 return err
@@ -424,7 +429,7 @@ type dryRunJobExecutor struct {
424429 container corev1.Container
425430}
426431
427- func (e * dryRunJobExecutor ) PrepareCommand (cmd []string ) ([]byte , error ) {
432+ func (e * dryRunJobExecutor ) PrepareCommand (ctx context. Context , cmd []string ) ([]byte , error ) {
428433 return nil , nil
429434}
430435
0 commit comments