From 9d3f682838de640bca716602d1b730738820fbb6 Mon Sep 17 00:00:00 2001 From: Dean Hunter Date: Fri, 26 Apr 2024 10:44:10 +0200 Subject: [PATCH] feat: wait for pod to be running before streaming logs Modify the StreamJobLogs function to wait for the pod associated with the job to be in the Running state before streaming logs. Add a loop that polls the pod status until it is running or a timeout is reached. This ensures that the logs are streamed only when the pod is ready, avoiding potential issues with accessing logs of a pod that is not yet running. --- Client/kubernetes/kuberentes.go | 51 ++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/Client/kubernetes/kuberentes.go b/Client/kubernetes/kuberentes.go index 28d66ca..aeeee71 100644 --- a/Client/kubernetes/kuberentes.go +++ b/Client/kubernetes/kuberentes.go @@ -79,7 +79,26 @@ func StreamJobLogs(namespace, jobName string) error { } retryTicker.Stop() // Stop the ticker - podName := pods.Items[0].Name //there should be only 1 pod in the job + podName := pods.Items[0].Name + + fmt.Println("Waiting for pod to be in Running state...") + podReady := false + for !podReady { + select { + case <-retryTimeout: + return fmt.Errorf("timeout reached while waiting for pod to be ready for job %s", jobName) + case <-retryTicker.C: + pod, err := clientset.CoreV1().Pods(namespace).Get(context.Background(), podName, metav1.GetOptions{}) + if err != nil { + return fmt.Errorf("failed to get pod %s: %w", podName, err) + } + if pod.Status.Phase == corev1.PodRunning { + podReady = true + break // Exit the loop once the pod is ready + } + fmt.Println("Pod is not ready yet, retrying...") + } + } fmt.Printf("Streaming logs from pod %s...\n", podName) logOptions := &corev1.PodLogOptions{Follow: true} @@ -104,24 +123,24 @@ func StreamJobLogs(namespace, jobName string) error { } } - for { - job, err := clientset.BatchV1().Jobs(namespace).Get(context.Background(), jobName, metav1.GetOptions{}) - if err != nil { - return fmt.Errorf("failed to get job %s: %w", jobName, err) - } + for { + job, err := clientset.BatchV1().Jobs(namespace).Get(context.Background(), jobName, metav1.GetOptions{}) + if err != nil { + return fmt.Errorf("failed to get job %s: %w", jobName, err) + } - if job.Status.Succeeded > 0 { - fmt.Printf("Job %s completed successfully.\n", jobName) - return nil - } + if job.Status.Succeeded > 0 { + fmt.Printf("Job %s completed successfully.\n", jobName) + return nil + } - if job.Status.Failed > 0 { - return fmt.Errorf("job %s failed", jobName) - } + if job.Status.Failed > 0 { + return fmt.Errorf("job %s failed", jobName) + } - fmt.Printf("Waiting on build %s to complete.\n", jobName) - time.Sleep(2 * time.Second) - } + fmt.Printf("Waiting for job %s to complete.\n", jobName) + time.Sleep(1 * time.Second) + } }