mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 14:07:14 +00:00
cloud-controller-manager get /healthz instead of calling restclient.ServerAPIVersions to wait for apiserver being healthy
This commit is contained in:
parent
6a7656b693
commit
97be082fb1
@ -260,15 +260,9 @@ func startControllers(c *cloudcontrollerconfig.CompletedConfig, kubeconfig *rest
|
|||||||
|
|
||||||
// If apiserver is not running we should wait for some time and fail only then. This is particularly
|
// If apiserver is not running we should wait for some time and fail only then. This is particularly
|
||||||
// important when we start apiserver and controller manager at the same time.
|
// important when we start apiserver and controller manager at the same time.
|
||||||
err = wait.PollImmediate(time.Second, 10*time.Second, func() (bool, error) {
|
err = genericcontrollermanager.WaitForAPIServer(versionedClient, 10*time.Second)
|
||||||
if _, err = restclient.ServerAPIVersions(kubeconfig); err == nil {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
glog.Errorf("Failed to get api versions from server: %v", err)
|
|
||||||
return false, nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Fatalf("Failed to get api versions from server: %v", err)
|
glog.Fatalf("Failed to wait for apiserver being healthy: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sharedInformers.Start(stop)
|
sharedInformers.Start(stop)
|
||||||
|
55
cmd/controller-manager/app/helper.go
Normal file
55
cmd/controller-manager/app/helper.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2018 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/golang/glog"
|
||||||
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
|
clientset "k8s.io/client-go/kubernetes"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WaitForAPIServer waits for the API Server's /healthz endpoint to report "ok" with 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))
|
||||||
|
glog.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
|
||||||
|
}
|
@ -24,7 +24,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -36,7 +35,6 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
"k8s.io/apimachinery/pkg/util/uuid"
|
"k8s.io/apimachinery/pkg/util/uuid"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
"k8s.io/client-go/discovery"
|
|
||||||
"k8s.io/client-go/informers"
|
"k8s.io/client-go/informers"
|
||||||
restclient "k8s.io/client-go/rest"
|
restclient "k8s.io/client-go/rest"
|
||||||
"k8s.io/client-go/tools/leaderelection"
|
"k8s.io/client-go/tools/leaderelection"
|
||||||
@ -350,34 +348,8 @@ func NewControllerInitializers(loopMode ControllerLoopMode) map[string]InitFunc
|
|||||||
// users don't have to restart their controller manager if they change the apiserver.
|
// users don't have to restart their controller manager if they change the apiserver.
|
||||||
// Until we get there, the structure here needs to be exposed for the construction of a proper ControllerContext.
|
// Until we get there, the structure here needs to be exposed for the construction of a proper ControllerContext.
|
||||||
func GetAvailableResources(clientBuilder controller.ControllerClientBuilder) (map[schema.GroupVersionResource]bool, error) {
|
func GetAvailableResources(clientBuilder controller.ControllerClientBuilder) (map[schema.GroupVersionResource]bool, error) {
|
||||||
var discoveryClient discovery.DiscoveryInterface
|
client := clientBuilder.ClientOrDie("controller-discovery")
|
||||||
|
discoveryClient := client.Discovery()
|
||||||
var healthzContent string
|
|
||||||
// If apiserver is not running we should wait for some time and fail only then. This is particularly
|
|
||||||
// important when we start apiserver and controller manager at the same time.
|
|
||||||
err := wait.PollImmediate(time.Second, 10*time.Second, func() (bool, error) {
|
|
||||||
client, err := clientBuilder.Client("controller-discovery")
|
|
||||||
if err != nil {
|
|
||||||
glog.Errorf("Failed to get api versions from server: %v", err)
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
healthStatus := 0
|
|
||||||
resp := client.Discovery().RESTClient().Get().AbsPath("/healthz").Do().StatusCode(&healthStatus)
|
|
||||||
if healthStatus != http.StatusOK {
|
|
||||||
glog.Errorf("Server isn't healthy yet. Waiting a little while.")
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
content, _ := resp.Raw()
|
|
||||||
healthzContent = string(content)
|
|
||||||
|
|
||||||
discoveryClient = client.Discovery()
|
|
||||||
return true, nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to get api versions from server: %v: %v", healthzContent, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
resourceMap, err := discoveryClient.ServerResources()
|
resourceMap, err := discoveryClient.ServerResources()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utilruntime.HandleError(fmt.Errorf("unable to get all supported resources from server: %v", err))
|
utilruntime.HandleError(fmt.Errorf("unable to get all supported resources from server: %v", err))
|
||||||
@ -407,6 +379,12 @@ func CreateControllerContext(s *config.CompletedConfig, rootClientBuilder, clien
|
|||||||
versionedClient := rootClientBuilder.ClientOrDie("shared-informers")
|
versionedClient := rootClientBuilder.ClientOrDie("shared-informers")
|
||||||
sharedInformers := informers.NewSharedInformerFactory(versionedClient, ResyncPeriod(s)())
|
sharedInformers := informers.NewSharedInformerFactory(versionedClient, ResyncPeriod(s)())
|
||||||
|
|
||||||
|
// If apiserver is not running we should wait for some time and fail only then. This is particularly
|
||||||
|
// important when we start apiserver and controller manager at the same time.
|
||||||
|
if err := genericcontrollerconfig.WaitForAPIServer(versionedClient, 10*time.Second); err != nil {
|
||||||
|
return ControllerContext{}, fmt.Errorf("failed to wait for apiserver being healthy: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
availableResources, err := GetAvailableResources(rootClientBuilder)
|
availableResources, err := GetAvailableResources(rootClientBuilder)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ControllerContext{}, err
|
return ControllerContext{}, err
|
||||||
|
Loading…
Reference in New Issue
Block a user