Skip to content

Commit 6f14842

Browse files
committed
waitForPod should use different exitCondition depending on restart policy
1 parent 98058a1 commit 6f14842

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/run/run.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -381,17 +381,21 @@ func (o *RunOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
381381
}
382382

383383
var pod *corev1.Pod
384-
leaveStdinOpen := o.LeaveStdinOpen
385-
waitForExitCode := !leaveStdinOpen && restartPolicy == corev1.RestartPolicyNever
384+
waitForExitCode := !o.LeaveStdinOpen && (restartPolicy == corev1.RestartPolicyNever || restartPolicy == corev1.RestartPolicyOnFailure)
386385
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)
388394
if err != nil {
389395
return err
390396
}
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
395399
return nil
396400
}
397401

@@ -691,6 +695,20 @@ func podCompleted(event watch.Event) (bool, error) {
691695
return false, nil
692696
}
693697

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+
694712
// podRunningAndReady returns true if the pod is running and ready, false if the pod has not
695713
// yet reached those states, returns ErrPodCompleted if the pod has run to completion, or
696714
// an error in any other case.

test/e2e/kubectl/kubectl.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,13 +522,21 @@ var _ = SIGDescribe("Kubectl client", func() {
522522
_, err = framework.NewKubectlCommand(ns, nsFlag, "run", "-i", "--image="+busyboxImage, "--restart=OnFailure", "failure-2", "--", "/bin/sh", "-c", "cat && exit 42").
523523
WithStdinData("abcd1234").
524524
Exec()
525-
framework.ExpectNoError(err)
525+
ee, ok = err.(uexec.ExitError)
526+
framework.ExpectEqual(ok, true)
527+
if !strings.Contains(ee.String(), "timed out") {
528+
framework.Failf("Missing expected 'timed out' error, got: %#v", ee)
529+
}
526530

527531
ginkgo.By("running a failing command without --restart=Never, but with --rm")
528532
_, err = framework.NewKubectlCommand(ns, nsFlag, "run", "-i", "--image="+busyboxImage, "--restart=OnFailure", "--rm", "failure-3", "--", "/bin/sh", "-c", "cat && exit 42").
529533
WithStdinData("abcd1234").
530534
Exec()
531-
framework.ExpectNoError(err)
535+
ee, ok = err.(uexec.ExitError)
536+
framework.ExpectEqual(ok, true)
537+
if !strings.Contains(ee.String(), "timed out") {
538+
framework.Failf("Missing expected 'timed out' error, got: %#v", ee)
539+
}
532540
e2epod.WaitForPodToDisappear(f.ClientSet, ns, "failure-3", labels.Everything(), 2*time.Second, wait.ForeverTestTimeout)
533541

534542
ginkgo.By("running a failing command with --leave-stdin-open")

0 commit comments

Comments
 (0)