@@ -117,8 +117,9 @@ func NewKubeWaiter(client clientset.Interface, timeout time.Duration, writer io.
117
117
// controlPlaneComponent holds a component name and an URL
118
118
// on which to perform health checks.
119
119
type controlPlaneComponent struct {
120
- name string
121
- url string
120
+ name string
121
+ addressPort string
122
+ endpoint string
122
123
}
123
124
124
125
// getControlPlaneComponentAddressAndPort parses the command in a static Pod
@@ -181,7 +182,6 @@ func getControlPlaneComponents(podMap map[string]*v1.Pod, addressAPIServer strin
181
182
182
183
type componentConfig struct {
183
184
name string
184
- podKey string
185
185
args []string
186
186
defaultAddr string
187
187
defaultPort string
@@ -190,24 +190,21 @@ func getControlPlaneComponents(podMap map[string]*v1.Pod, addressAPIServer strin
190
190
191
191
components := []componentConfig {
192
192
{
193
- name : "kube-apiserver" ,
194
- podKey : constants .KubeAPIServer ,
193
+ name : constants .KubeAPIServer ,
195
194
args : []string {argAdvertiseAddress , argPort },
196
195
defaultAddr : addressAPIServer ,
197
196
defaultPort : portAPIServer ,
198
197
endpoint : endpointLivez ,
199
198
},
200
199
{
201
- name : "kube-controller-manager" ,
202
- podKey : constants .KubeControllerManager ,
200
+ name : constants .KubeControllerManager ,
203
201
args : []string {argBindAddress , argPort },
204
202
defaultAddr : addressKCM ,
205
203
defaultPort : portKCM ,
206
204
endpoint : endpointHealthz ,
207
205
},
208
206
{
209
- name : "kube-scheduler" ,
210
- podKey : constants .KubeScheduler ,
207
+ name : constants .KubeScheduler ,
211
208
args : []string {argBindAddress , argPort },
212
209
defaultAddr : addressScheduler ,
213
210
defaultPort : portScheduler ,
@@ -219,8 +216,8 @@ func getControlPlaneComponents(podMap map[string]*v1.Pod, addressAPIServer strin
219
216
address , port := component .defaultAddr , component .defaultPort
220
217
221
218
values , err := getControlPlaneComponentAddressAndPort (
222
- podMap [component .podKey ],
223
- component .podKey ,
219
+ podMap [component .name ],
220
+ component .name ,
224
221
component .args ,
225
222
)
226
223
if err != nil {
@@ -235,8 +232,9 @@ func getControlPlaneComponents(podMap map[string]*v1.Pod, addressAPIServer strin
235
232
}
236
233
237
234
result = append (result , controlPlaneComponent {
238
- name : component .name ,
239
- url : fmt .Sprintf ("https://%s/%s" , net .JoinHostPort (address , port ), component .endpoint ),
235
+ name : component .name ,
236
+ addressPort : net .JoinHostPort (address , port ),
237
+ endpoint : component .endpoint ,
240
238
})
241
239
}
242
240
@@ -248,7 +246,7 @@ func getControlPlaneComponents(podMap map[string]*v1.Pod, addressAPIServer strin
248
246
249
247
// WaitForControlPlaneComponents waits for all control plane components to report "ok".
250
248
func (w * KubeWaiter ) WaitForControlPlaneComponents (podMap map [string ]* v1.Pod , apiSeverAddress string ) error {
251
- fmt .Printf ( "[control-plane-check] Waiting for healthy control plane components." +
249
+ _ , _ = fmt .Fprintf ( w . writer , "[control-plane-check] Waiting for healthy control plane components." +
252
250
" This can take up to %v\n " , w .timeout )
253
251
254
252
components , err := getControlPlaneComponents (podMap , apiSeverAddress )
@@ -260,44 +258,58 @@ func (w *KubeWaiter) WaitForControlPlaneComponents(podMap map[string]*v1.Pod, ap
260
258
errChan := make (chan error , len (components ))
261
259
262
260
for _ , comp := range components {
263
- fmt .Printf ("[control-plane-check] Checking %s at %s\n " , comp .name , comp .url )
261
+ url := fmt .Sprintf ("https://%s/%s" , comp .addressPort , comp .endpoint )
262
+ _ , _ = fmt .Fprintf (w .writer , "[control-plane-check] Checking %s at %s\n " , comp .name , url )
264
263
265
264
go func (comp controlPlaneComponent ) {
266
265
tr := & http.Transport {
267
266
TLSClientConfig : & tls.Config {InsecureSkipVerify : true },
268
267
}
269
268
client := & http.Client {Transport : tr }
270
269
start := time .Now ()
270
+ statusCode := 0
271
271
var lastError error
272
272
273
273
err := wait .PollUntilContextTimeout (
274
274
context .Background (),
275
275
constants .KubernetesAPICallRetryInterval ,
276
276
w .timeout ,
277
277
true , func (ctx context.Context ) (bool , error ) {
278
- resp , err := client .Get (comp .url )
279
- if err != nil {
280
- lastError = errors .WithMessagef (err , "%s check failed at %s" , comp .name , comp .url )
281
- return false , nil
278
+ // The kube-apiserver check should use the client defined in the waiter
279
+ // or otherwise the regular http client can fail when anonymous auth is enabled.
280
+ if comp .name == constants .KubeAPIServer {
281
+ result := w .client .Discovery ().RESTClient ().
282
+ Get ().AbsPath (comp .endpoint ).Do (ctx ).StatusCode (& statusCode )
283
+ if err := result .Error (); err != nil {
284
+ lastError = errors .WithMessagef (err , "%s check failed at %s" , comp .name , url )
285
+ return false , nil
286
+ }
287
+ } else {
288
+ resp , err := client .Get (url )
289
+ if err != nil {
290
+ lastError = errors .WithMessagef (err , "%s check failed at %s" , comp .name , url )
291
+ return false , nil
292
+ }
293
+ defer func () {
294
+ _ = resp .Body .Close ()
295
+ }()
296
+ statusCode = resp .StatusCode
282
297
}
283
298
284
- defer func () {
285
- _ = resp .Body .Close ()
286
- }()
287
- if resp .StatusCode != http .StatusOK {
299
+ if statusCode != http .StatusOK {
288
300
lastError = errors .Errorf ("%s check failed at %s with status: %d" ,
289
- comp .name , comp . url , resp . StatusCode )
301
+ comp .name , url , statusCode )
290
302
return false , nil
291
303
}
292
304
293
305
return true , nil
294
306
})
295
307
if err != nil {
296
- fmt .Printf ( "[control-plane-check] %s is not healthy after %v\n " , comp .name , time .Since (start ))
308
+ _ , _ = fmt .Fprintf ( w . writer , "[control-plane-check] %s is not healthy after %v\n " , comp .name , time .Since (start ))
297
309
errChan <- lastError
298
310
return
299
311
}
300
- fmt .Printf ( "[control-plane-check] %s is healthy after %v\n " , comp .name , time .Since (start ))
312
+ _ , _ = fmt .Fprintf ( w . writer , "[control-plane-check] %s is healthy after %v\n " , comp .name , time .Since (start ))
301
313
errChan <- nil
302
314
}(comp )
303
315
}
@@ -312,7 +324,7 @@ func (w *KubeWaiter) WaitForControlPlaneComponents(podMap map[string]*v1.Pod, ap
312
324
313
325
// WaitForAPI waits for the API Server's /healthz endpoint to report "ok"
314
326
func (w * KubeWaiter ) WaitForAPI () error {
315
- fmt .Printf ( "[api-check] Waiting for a healthy API server. This can take up to %v\n " , w .timeout )
327
+ _ , _ = fmt .Fprintf ( w . writer , "[api-check] Waiting for a healthy API server. This can take up to %v\n " , w .timeout )
316
328
317
329
start := time .Now ()
318
330
err := wait .PollUntilContextTimeout (
@@ -328,11 +340,11 @@ func (w *KubeWaiter) WaitForAPI() error {
328
340
return true , nil
329
341
})
330
342
if err != nil {
331
- fmt .Printf ( "[api-check] The API server is not healthy after %v\n " , time .Since (start ))
343
+ _ , _ = fmt .Fprintf ( w . writer , "[api-check] The API server is not healthy after %v\n " , time .Since (start ))
332
344
return err
333
345
}
334
346
335
- fmt .Printf ( "[api-check] The API server is healthy after %v\n " , time .Since (start ))
347
+ _ , _ = fmt .Fprintf ( w . writer , "[api-check] The API server is healthy after %v\n " , time .Since (start ))
336
348
return nil
337
349
}
338
350
@@ -347,12 +359,12 @@ func (w *KubeWaiter) WaitForPodsWithLabel(kvLabel string) error {
347
359
listOpts := metav1.ListOptions {LabelSelector : kvLabel }
348
360
pods , err := w .client .CoreV1 ().Pods (metav1 .NamespaceSystem ).List (context .TODO (), listOpts )
349
361
if err != nil {
350
- fmt .Fprintf (w .writer , "[apiclient] Error getting Pods with label selector %q [%v]\n " , kvLabel , err )
362
+ _ , _ = fmt .Fprintf (w .writer , "[apiclient] Error getting Pods with label selector %q [%v]\n " , kvLabel , err )
351
363
return false , nil
352
364
}
353
365
354
366
if lastKnownPodNumber != len (pods .Items ) {
355
- fmt .Fprintf (w .writer , "[apiclient] Found %d Pods for label selector %s\n " , len (pods .Items ), kvLabel )
367
+ _ , _ = fmt .Fprintf (w .writer , "[apiclient] Found %d Pods for label selector %s\n " , len (pods .Items ), kvLabel )
356
368
lastKnownPodNumber = len (pods .Items )
357
369
}
358
370
@@ -379,10 +391,10 @@ func (w *KubeWaiter) WaitForKubelet(healthzAddress string, healthzPort int32) er
379
391
)
380
392
381
393
if healthzPort == 0 {
382
- fmt .Println ( "[kubelet-check] Skipping the kubelet health check because the healthz port is set to 0" )
394
+ _ , _ = fmt .Fprintln ( w . writer , "[kubelet-check] Skipping the kubelet health check because the healthz port is set to 0" )
383
395
return nil
384
396
}
385
- fmt .Printf ( "[kubelet-check] Waiting for a healthy kubelet at %s. This can take up to %v\n " ,
397
+ _ , _ = fmt .Fprintf ( w . writer , "[kubelet-check] Waiting for a healthy kubelet at %s. This can take up to %v\n " ,
386
398
healthzEndpoint , w .timeout )
387
399
388
400
formatError := func (cause string ) error {
@@ -417,11 +429,11 @@ func (w *KubeWaiter) WaitForKubelet(healthzAddress string, healthzPort int32) er
417
429
return true , nil
418
430
})
419
431
if err != nil {
420
- fmt .Printf ( "[kubelet-check] The kubelet is not healthy after %v\n " , time .Since (start ))
432
+ _ , _ = fmt .Fprintf ( w . writer , "[kubelet-check] The kubelet is not healthy after %v\n " , time .Since (start ))
421
433
return lastError
422
434
}
423
435
424
- fmt .Printf ( "[kubelet-check] The kubelet is healthy after %v\n " , time .Since (start ))
436
+ _ , _ = fmt .Fprintf ( w . writer , "[kubelet-check] The kubelet is healthy after %v\n " , time .Since (start ))
425
437
return nil
426
438
}
427
439
@@ -528,10 +540,10 @@ func PrintControlPlaneErrorHelpScreen(outputWriter io.Writer, criSocket string)
528
540
Socket : criSocket ,
529
541
}
530
542
_ = controlPlaneFailTempl .Execute (outputWriter , context )
531
- fmt .Println ( "" )
543
+ _ , _ = fmt .Fprintln ( outputWriter , "" )
532
544
}
533
545
534
546
// PrintKubeletErrorHelpScreen prints help text on kubelet errors.
535
547
func PrintKubeletErrorHelpScreen (outputWriter io.Writer ) {
536
- fmt .Fprintln (outputWriter , kubeletFailMsg )
548
+ _ , _ = fmt .Fprintln (outputWriter , kubeletFailMsg )
537
549
}
0 commit comments