package app import ( "fmt" "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/klog" "net/http" "time" ) // WaitForAPIServer waits for the API Server's /healthz endpoint to report "ok" before timeout. func WaitForAPIServer(client clientset.Interface, timeout time.Duration) error { var lastErr error err := wait.PollImmediate(time.Second, timeout, func() (bool, error) { healthStatus := 0 result := client.Discovery().RESTClient().Get().AbsPath("/healthz").Do().StatusCode(&healthStatus) if result.Error() != nil { lastErr = fmt.Errorf("failed to get apiserver /healthz status: %v", result.Error()) return false, nil } if healthStatus != http.StatusOK { content, _ := result.Raw() lastErr = fmt.Errorf("APIServer isn't healthy: %v", string(content)) klog.Warningf("APIServer isn't healthy yet: %v. Waiting a little while.", string(content)) return false, nil } return true, nil }) if err != nil { return fmt.Errorf("%v: %v", err, lastErr) } return nil }