Commit 9989f5e6 authored by Brad Davidson's avatar Brad Davidson Committed by Brad Davidson

Increase log output while waiting for apiserver ready

Increases log verbosity but decreases polling frequency to avoid spamming the console. It usually takes a couple seconds for the apiserver to come up anyway. Signed-off-by: 's avatarBrad Davidson <brad.davidson@rancher.com> (cherry picked from commit 2c133692) Signed-off-by: 's avatarBrad Davidson <brad.davidson@rancher.com>
parent f13bf113
...@@ -3,14 +3,12 @@ package util ...@@ -3,14 +3,12 @@ package util
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"net" "net"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
"time" "time"
pkgerrors "github.com/pkg/errors"
"github.com/rancher/wrangler/v3/pkg/merr" "github.com/rancher/wrangler/v3/pkg/merr"
"github.com/rancher/wrangler/v3/pkg/schemes" "github.com/rancher/wrangler/v3/pkg/schemes"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
...@@ -53,16 +51,20 @@ func GetAddresses(endpoint *v1.Endpoints) []string { ...@@ -53,16 +51,20 @@ func GetAddresses(endpoint *v1.Endpoints) []string {
return serverAddresses return serverAddresses
} }
// WaitForAPIServerReady waits for the API Server's /readyz endpoint to report "ok" with timeout. // WaitForAPIServerReady waits for the API server's /readyz endpoint to report "ok" with timeout.
// This is modified from WaitForAPIServer from the Kubernetes controller-manager app, but checks the // This is modified from WaitForAPIServer from the Kubernetes controller-manager app, but checks the
// readyz endpoint instead of the deprecated healthz endpoint, and supports context. // readyz endpoint instead of the deprecated healthz endpoint, and supports context.
func WaitForAPIServerReady(ctx context.Context, kubeconfigPath string, timeout time.Duration) error { func WaitForAPIServerReady(ctx context.Context, kubeconfigPath string, timeout time.Duration) error {
var lastErr error lastErr := errors.New("API server not polled")
restConfig, err := GetRESTConfig(kubeconfigPath) restConfig, err := GetRESTConfig(kubeconfigPath)
if err != nil { if err != nil {
return err return err
} }
// Probe apiserver readiness with a 15 second timeout
// https://github.com/kubernetes/kubernetes/blob/v1.24.0/cmd/kubeadm/app/util/staticpod/utils.go#L252
restConfig.Timeout = time.Second * 15
// By default, idle connections to the apiserver are returned to a global pool // By default, idle connections to the apiserver are returned to a global pool
// between requests. Explicitly flag this client's request for closure so that // between requests. Explicitly flag this client's request for closure so that
// we re-dial through the loadbalancer in case the endpoints have changed. // we re-dial through the loadbalancer in case the endpoints have changed.
...@@ -80,17 +82,15 @@ func WaitForAPIServerReady(ctx context.Context, kubeconfigPath string, timeout t ...@@ -80,17 +82,15 @@ func WaitForAPIServerReady(ctx context.Context, kubeconfigPath string, timeout t
return err return err
} }
err = wait.PollUntilContextTimeout(ctx, time.Second, timeout, true, func(ctx context.Context) (bool, error) { err = wait.PollUntilContextTimeout(ctx, time.Second*2, timeout, true, func(ctx context.Context) (bool, error) {
healthStatus := 0 // DoRaw returns an error if the response code is < 200 OK or > 206 Partial Content
result := restClient.Get().AbsPath("/readyz").Do(ctx).StatusCode(&healthStatus) if _, err := restClient.Get().AbsPath("/readyz").Param("verbose", "").DoRaw(ctx); err != nil {
if rerr := result.Error(); rerr != nil { if err.Error() != lastErr.Error() {
lastErr = pkgerrors.WithMessage(rerr, "failed to get apiserver /readyz status") logrus.Infof("Polling for API server readiness: GET /readyz failed: %v", err)
return false, nil } else {
} logrus.Debug("Polling for API server readiness: GET /readyz failed: status unchanged")
if healthStatus != http.StatusOK { }
content, _ := result.Raw() lastErr = err
lastErr = fmt.Errorf("APIServer isn't ready: %v", string(content))
logrus.Warnf("APIServer isn't ready yet: %v. Waiting a little while.", string(content))
return false, nil return false, nil
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment