Skip to content

Commit

Permalink
Merge pull request #5 from CoreViewInc/wait-for-pod-running-state-bef…
Browse files Browse the repository at this point in the history
…ore-logs

feat: wait for pod to be running before streaming logs
  • Loading branch information
DeanHnter authored Apr 26, 2024
2 parents eb08e92 + 9d3f682 commit e921541
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 e921541

Please sign in to comment.