@@ -2,8 +2,8 @@ package checker
22
33import (
44 "context"
5+ "errors"
56 "fmt"
6- "math/rand"
77 "os"
88 "path/filepath"
99 "strings"
@@ -76,46 +76,47 @@ func (k *K8sPodChecker) Check(ctx context.Context) error {
7676 return err
7777 }
7878
79- k .Common .Logger .Info ().Strs ("labels" , k .LabelSelector ).Str ("namespace" , k .Namespace ).Msg ("listing pods" )
80-
81- pods , err := clientset .CoreV1 ().Pods (k .Namespace ).List (ctx , metav1.ListOptions {
82- LabelSelector : strings .Join (k .LabelSelector , "," ),
83- })
84- if err != nil {
85- return err
86- }
79+ var latestPod * corev1.Pod
80+ var podIP string
8781
88- k .Common .Logger .Info ().Msgf ("found %d pods with the selected filters" , len (pods .Items ))
82+ retriable := func () error {
83+ k .Common .Logger .Info ().Strs ("labels" , k .LabelSelector ).Str ("namespace" , k .Namespace ).Msg ("listing pods" )
8984
90- var latestPod * corev1. Pod
91- for _ , pod := range pods . Items {
92- if k . Image != "" && k . Image != pod . Spec . Containers [ 0 ]. Image {
93- k . Common . Logger . Info (). Msgf ( "found pod %s/%s but did not match on image" , pod . Namespace , pod . Name )
94- continue
85+ pods , err := clientset . CoreV1 (). Pods ( k . Namespace ). List ( ctx , metav1. ListOptions {
86+ LabelSelector : strings . Join ( k . LabelSelector , "," ),
87+ })
88+ if err != nil {
89+ return err
9590 }
9691
97- if latestPod == nil || latestPod .CreationTimestamp .After (pod .CreationTimestamp .Time ) {
98- latestPod = & pod
99- }
100- }
92+ k .Common .Logger .Info ().Msgf ("found %d pods with the selected filters" , len (pods .Items ))
10193
102- if latestPod == nil {
103- return fmt .Errorf ("no matching pod found in %s namespace with labels: %s" , k .Namespace , k .LabelSelector )
104- }
94+ for _ , pod := range pods .Items {
95+ if k .Image != "" && k .Image != pod .Spec .Containers [0 ].Image {
96+ k .Common .Logger .Info ().Msgf ("found pod %s/%s but did not match on image" , pod .Namespace , pod .Name )
97+ continue
98+ }
10599
106- var podIP string
107- for {
100+ if latestPod == nil || latestPod .CreationTimestamp .After (pod .CreationTimestamp .Time ) {
101+ latestPod = & pod
102+ }
103+ }
104+
105+ if latestPod == nil {
106+ return fmt .Errorf ("no matching pod found in %s namespace with labels: %s" , k .Namespace , k .LabelSelector )
107+ }
108108 podIP = latestPod .Status .PodIP
109109 if podIP != "" {
110110 k .Common .Logger .Info ().Msgf ("pod %s has an IP assigned: %s" , latestPod .Name , latestPod .Status .PodIP )
111- break
111+ return nil
112112 }
113113
114- jitterSeconds := rand .Intn (6 ) + 5
115- err := waitWithJitter (ctx , jitterSeconds )
116- if err != nil {
117- return err
118- }
114+ return errors .New ("no IP assigned to the pod yet" )
115+ }
116+
117+ err = k .Common .runWithJitterBackoff (ctx , retriable )
118+ if err != nil {
119+ return err
119120 }
120121
121122 port := k .Port
0 commit comments