From 0cebc078ab0d426ad8c3b6d8898445bf98779501 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Wed, 24 Jan 2024 10:29:09 +0100 Subject: [PATCH] Improve `NewPodControllerForExistingPod` to set `podReady` to true (#2623) * Improve `NewPodControllerForExistingPod` to set `podReady` to true `NewPodControllerForExistingPod` function was supposed to return the podController for an existing pod. If this function is caleld, it would mean that the pod is already runnign and we are creating `podController` for that pod. But this function didn't set `podReady` field of podController to true that was beign checked in some other methods to make sure that the pod is running. And because of that those methods would see the pod to be not running and complain. This commit fixes that by making sure that the pod is running and sets `podReady` to true. * Improve comment to function --- pkg/kube/pod_controller.go | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/pkg/kube/pod_controller.go b/pkg/kube/pod_controller.go index 19f7af931d..cd9aa296a4 100644 --- a/pkg/kube/pod_controller.go +++ b/pkg/kube/pod_controller.go @@ -99,11 +99,20 @@ func NewPodController(cli kubernetes.Interface, options *PodOptions, opts ...Pod return r } -// NewPodControllerForExistingPod returns a new PodController given Kubernetes -// Client and existing pod details. -// Invocation of StartPod of returned PodController instance will fail, since the pod is already known. -func NewPodControllerForExistingPod(cli kubernetes.Interface, pod *corev1.Pod) PodController { - r := &podController{ +// NewPodControllerForExistingPod returns a new PodController for the given +// running pod. +// Invocation of StartPod of returned PodController instance will fail, since +// the pod is expected to be running already. +// Note: +// If the pod is not in the ready state, it will wait for up to +// KANISTER_POD_READY_WAIT_TIMEOUT (15 minutes by default) until the pod becomes ready. +func NewPodControllerForExistingPod(cli kubernetes.Interface, pod *corev1.Pod) (PodController, error) { + err := WaitForPodReady(context.Background(), cli, pod.Namespace, pod.Name) + if err != nil { + return nil, err + } + + pc := &podController{ cli: cli, pcp: &podControllerProcessor{ cli: cli, @@ -117,9 +126,10 @@ func NewPodControllerForExistingPod(cli kubernetes.Interface, pod *corev1.Pod) P Namespace: pod.Namespace, ContainerName: pod.Spec.Containers[0].Name, } - r.podOptions = options + pc.podOptions = options + pc.podReady = true - return r + return pc, nil } func (p *podController) PodName() string {