Wait for api-server to report version after starting

pull/1007/head
larmog 2019-11-04 12:08:47 +01:00 committed by Erik Wilson
parent 9cb7416913
commit 7aa3d08385
1 changed files with 29 additions and 0 deletions

View File

@ -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")
}