My approach is:
func restartPod(meta metav1.ObjectMeta, kubeClient kubernetes.Interface) error {
err := kubeClient.CoreV1().Pods(meta.Namespace).Delete(meta.Name, deleteInForeground())
if err != nil {
return err
}
//time.Sleep(2 * time.Second)
return wait.PollImmediate(5*time.Second, 5*time.Minute, func() (done bool, err error) {
pod, err := kubeClient.CoreV1().Pods(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
if err != nil {
return false, nil
}
return pod.Status.Phase == v1.PodRunning && pod.Status.ContainerStatuses[0].Ready, nil
})
}
It doesn't work because the deletion of pod is non-blocking, means it doesn't wait for the pod to be deleted. So the Get
pod method returns the pod with running state. If I use sleep
for some seconds after pod deletion then it works fine. Is there any better way to do this without using sleep
?
In the metadata of every object there is UUID in a field called uid
. You can compare and wait until the pod is Ready and has a different UUID. See https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids for more details (though really that's about all there is to say).