Skip to content

Commit

Permalink
feat: wait for pod to be running before streaming logs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DeanHnter committed Apr 26, 2024
1 parent eb08e92 commit 9d3f682
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions Client/kubernetes/kuberentes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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)
}
}


Expand Down

0 comments on commit 9d3f682

Please sign in to comment.