waitForPod should use different exitCondition depending on restart policy

Kubernetes-commit: 6f1484256db9579361d218aef7f7960446fd541d
This commit is contained in:
Maciej Szulik 2020-04-24 13:04:42 +02:00 committed by Kubernetes Publisher
parent 5b27ac0ca2
commit d727dabb7d
1 changed files with 25 additions and 7 deletions

View File

@ -381,17 +381,21 @@ func (o *RunOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
}
var pod *corev1.Pod
leaveStdinOpen := o.LeaveStdinOpen
waitForExitCode := !leaveStdinOpen && restartPolicy == corev1.RestartPolicyNever
waitForExitCode := !o.LeaveStdinOpen && (restartPolicy == corev1.RestartPolicyNever || restartPolicy == corev1.RestartPolicyOnFailure)
if waitForExitCode {
pod, err = waitForPod(clientset.CoreV1(), attachablePod.Namespace, attachablePod.Name, opts.GetPodTimeout, podCompleted)
// we need different exit condition depending on restart policy
// for Never it can either fail or succeed, for OnFailure only
// success matters
exitCondition := podCompleted
if restartPolicy == corev1.RestartPolicyOnFailure {
exitCondition = podSucceeded
}
pod, err = waitForPod(clientset.CoreV1(), attachablePod.Namespace, attachablePod.Name, opts.GetPodTimeout, exitCondition)
if err != nil {
return err
}
}
// after removal is done, return successfully if we are not interested in the exit code
if !waitForExitCode {
} else {
// after removal is done, return successfully if we are not interested in the exit code
return nil
}
@ -691,6 +695,20 @@ func podCompleted(event watch.Event) (bool, error) {
return false, nil
}
// podSucceeded returns true if the pod has run to completion, false if the pod has not yet
// reached running state, or an error in any other case.
func podSucceeded(event watch.Event) (bool, error) {
switch event.Type {
case watch.Deleted:
return false, errors.NewNotFound(schema.GroupResource{Resource: "pods"}, "")
}
switch t := event.Object.(type) {
case *corev1.Pod:
return t.Status.Phase == corev1.PodSucceeded, nil
}
return false, nil
}
// podRunningAndReady returns true if the pod is running and ready, false if the pod has not
// yet reached those states, returns ErrPodCompleted if the pod has run to completion, or
// an error in any other case.