@@ -381,17 +381,21 @@ func (o *RunOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
381
381
}
382
382
383
383
var pod * corev1.Pod
384
- leaveStdinOpen := o .LeaveStdinOpen
385
- waitForExitCode := ! leaveStdinOpen && restartPolicy == corev1 .RestartPolicyNever
384
+ waitForExitCode := ! o .LeaveStdinOpen && (restartPolicy == corev1 .RestartPolicyNever || restartPolicy == corev1 .RestartPolicyOnFailure )
386
385
if waitForExitCode {
387
- pod , err = waitForPod (clientset .CoreV1 (), attachablePod .Namespace , attachablePod .Name , opts .GetPodTimeout , podCompleted )
386
+ // we need different exit condition depending on restart policy
387
+ // for Never it can either fail or succeed, for OnFailure only
388
+ // success matters
389
+ exitCondition := podCompleted
390
+ if restartPolicy == corev1 .RestartPolicyOnFailure {
391
+ exitCondition = podSucceeded
392
+ }
393
+ pod , err = waitForPod (clientset .CoreV1 (), attachablePod .Namespace , attachablePod .Name , opts .GetPodTimeout , exitCondition )
388
394
if err != nil {
389
395
return err
390
396
}
391
- }
392
-
393
- // after removal is done, return successfully if we are not interested in the exit code
394
- if ! waitForExitCode {
397
+ } else {
398
+ // after removal is done, return successfully if we are not interested in the exit code
395
399
return nil
396
400
}
397
401
@@ -691,6 +695,20 @@ func podCompleted(event watch.Event) (bool, error) {
691
695
return false , nil
692
696
}
693
697
698
+ // podSucceeded returns true if the pod has run to completion, false if the pod has not yet
699
+ // reached running state, or an error in any other case.
700
+ func podSucceeded (event watch.Event ) (bool , error ) {
701
+ switch event .Type {
702
+ case watch .Deleted :
703
+ return false , errors .NewNotFound (schema.GroupResource {Resource : "pods" }, "" )
704
+ }
705
+ switch t := event .Object .(type ) {
706
+ case * corev1.Pod :
707
+ return t .Status .Phase == corev1 .PodSucceeded , nil
708
+ }
709
+ return false , nil
710
+ }
711
+
694
712
// podRunningAndReady returns true if the pod is running and ready, false if the pod has not
695
713
// yet reached those states, returns ErrPodCompleted if the pod has run to completion, or
696
714
// an error in any other case.
0 commit comments