From 7aa3d0838543959937c12a54a5c0c731c7e3e549 Mon Sep 17 00:00:00 2001 From: larmog Date: Mon, 4 Nov 2019 12:08:47 +0100 Subject: [PATCH] Wait for api-server to report version after starting --- pkg/daemons/control/server.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/pkg/daemons/control/server.go b/pkg/daemons/control/server.go index 40999f6aad..7d9a4c06a6 100644 --- a/pkg/daemons/control/server.go +++ b/pkg/daemons/control/server.go @@ -30,6 +30,7 @@ import ( "github.com/sirupsen/logrus" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apiserver/pkg/authentication/authenticator" + "k8s.io/client-go/discovery" "k8s.io/client-go/tools/clientcmd" ccmapp "k8s.io/kubernetes/cmd/cloud-controller-manager/app" "k8s.io/kubernetes/cmd/kube-apiserver/app" @@ -90,6 +91,10 @@ func Server(ctx context.Context, cfg *config.Control) error { return err } + if err := waitForAPIServer(runtime); err != nil { + return err + } + runtime.Handler = handler runtime.Authenticator = auth @@ -825,3 +830,27 @@ func checkForCloudControllerPrivileges(runtime *config.ControlRuntime) error { } return nil } + +func waitForAPIServer(runtime *config.ControlRuntime) error { + restConfig, err := clientcmd.BuildConfigFromFlags("", runtime.KubeConfigAdmin) + if err != nil { + return err + } + + discoveryclient, err := discovery.NewDiscoveryClientForConfig(restConfig) + if err != nil { + return err + } + + for i := 0; i < 60; i++ { + info, err := discoveryclient.ServerVersion() + if err == nil { + logrus.Infof("apiserver %s is up and running", info) + return nil + } + logrus.Infof("waiting for apiserver to become available") + time.Sleep(1 * time.Second) + } + + return fmt.Errorf("timeout waiting for apiserver") +}