@@ -169,17 +169,29 @@ func (k *NSKubernetesClient) CreateSecretFrom(ns, name string) {
169
169
170
170
// WaitForPodsReady waits for a list of pods to become ready.
171
171
func (k * NSKubernetesClient ) WaitForPodsReady (podNames ... string ) {
172
- k .WaitForPodsInPhase (v1 .PodRunning , podNames ... )
172
+ f := func (pod * v1.Pod ) bool {
173
+ for _ , cond := range pod .Status .Conditions {
174
+ if cond .Type == v1 .PodReady && cond .Status == v1 .ConditionTrue {
175
+ return true
176
+ }
177
+ }
178
+ return false
179
+ }
180
+ result := k .waitForPods (f , podNames ... )
181
+ logrus .Infof ("Pods marked as ready: %v" , result )
173
182
}
174
183
175
- // WaitForPodsInPhase waits for a list of pods to become ready .
184
+ // WaitForPodsInPhase waits for a list of pods to reach the given phase .
176
185
func (k * NSKubernetesClient ) WaitForPodsInPhase (expectedPhase v1.PodPhase , podNames ... string ) {
177
- if len ( podNames ) == 0 {
178
- return
186
+ f := func ( pod * v1. Pod ) bool {
187
+ return pod . Status . Phase == expectedPhase
179
188
}
189
+ result := k .waitForPods (f , podNames ... )
190
+ logrus .Infof ("Pods in phase %q: %v" , expectedPhase , result )
191
+ }
180
192
181
- logrus . Infoln ( "Waiting for pods" , podNames , " to be ready" )
182
-
193
+ // waitForPods waits for a list of pods to become ready.
194
+ func ( k * NSKubernetesClient ) waitForPods ( podReady func ( * v1. Pod ) bool , podNames ... string ) ( podsReady map [ string ] bool ) {
183
195
ctx , cancelTimeout := context .WithTimeout (context .Background (), 5 * time .Minute )
184
196
defer cancelTimeout ()
185
197
@@ -190,7 +202,14 @@ func (k *NSKubernetesClient) WaitForPodsInPhase(expectedPhase v1.PodPhase, podNa
190
202
}
191
203
defer w .Stop ()
192
204
193
- phases := map [string ]v1.PodPhase {}
205
+ waitForAllPods := len (podNames ) == 0
206
+ if waitForAllPods {
207
+ logrus .Infof ("Waiting for all pods in namespace %q to be ready" , k .ns )
208
+ } else {
209
+ logrus .Infoln ("Waiting for pods" , podNames , "to be ready" )
210
+ }
211
+
212
+ podsReady = map [string ]bool {}
194
213
195
214
for {
196
215
waitLoop:
@@ -200,30 +219,40 @@ func (k *NSKubernetesClient) WaitForPodsInPhase(expectedPhase v1.PodPhase, podNa
200
219
//k.debug("nodes")
201
220
k .debug ("pods" )
202
221
k .logs ("pod" , podNames )
203
- k .t .Fatalf ("Timed out waiting for pods %v ready in namespace %s " , podNames , k .ns )
222
+ k .t .Fatalf ("Timed out waiting for pods %v in namespace %q " , podNames , k .ns )
204
223
205
224
case event := <- w .ResultChan ():
206
225
if event .Object == nil {
207
226
return
208
227
}
209
228
pod := event .Object .(* v1.Pod )
210
- logrus .Infoln ("Pod" , pod .Name , "is" , pod .Status .Phase )
211
229
if pod .Status .Phase == v1 .PodFailed {
212
230
logs , err := pods .GetLogs (pod .Name , & v1.PodLogOptions {}).DoRaw (ctx )
213
231
if err != nil {
214
232
k .t .Fatalf ("failed to get logs for failed pod %s: %s" , pod .Name , err )
215
233
}
216
234
k .t .Fatalf ("pod %s failed. Logs:\n %s" , pod .Name , logs )
217
235
}
218
- phases [pod .Name ] = pod .Status .Phase
219
236
237
+ if _ , found := podsReady [pod .Name ]; ! found && waitForAllPods {
238
+ podNames = append (podNames , pod .Name )
239
+ }
240
+ podsReady [pod .Name ] = podReady (pod )
241
+
242
+ var waiting []string
220
243
for _ , podName := range podNames {
221
- if phases [podName ] != expectedPhase {
222
- break waitLoop
244
+ if ! podsReady [podName ] {
245
+ waiting = append ( waiting , podName )
223
246
}
224
247
}
225
-
226
- logrus .Infoln ("Pods" , podNames , "ready" )
248
+ if len (waiting ) > 0 {
249
+ logrus .Infof ("Still waiting for pods %v" , waiting )
250
+ break waitLoop
251
+ } else if l := len (w .ResultChan ()); l > 0 {
252
+ // carry on when there are pending messages in case a new pod has been created
253
+ logrus .Infof ("%d pending pod update messages" , l )
254
+ break waitLoop
255
+ }
227
256
return
228
257
}
229
258
}
0 commit comments