Commit 4f28561e authored by Brad Davidson's avatar Brad Davidson Committed by Brad Davidson

Wait for apiserver readyz instead of healthz

Signed-off-by: 's avatarBrad Davidson <brad.davidson@rancher.com> (cherry picked from commit 3449d5b9)
parent 17f1aa36
...@@ -37,7 +37,6 @@ import ( ...@@ -37,7 +37,6 @@ import (
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
v1 "k8s.io/client-go/kubernetes/typed/core/v1" v1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd"
"k8s.io/controller-manager/app"
app2 "k8s.io/kubernetes/cmd/kube-proxy/app" app2 "k8s.io/kubernetes/cmd/kube-proxy/app"
kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config" kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
utilsnet "k8s.io/utils/net" utilsnet "k8s.io/utils/net"
...@@ -128,7 +127,7 @@ func run(ctx context.Context, cfg cmds.Agent, proxy proxy.Proxy) error { ...@@ -128,7 +127,7 @@ func run(ctx context.Context, cfg cmds.Agent, proxy proxy.Proxy) error {
return err return err
} }
app.WaitForAPIServer(coreClient, 30*time.Second) util.WaitForAPIServerReady(coreClient, 30*time.Second)
if !nodeConfig.NoFlannel { if !nodeConfig.NoFlannel {
if err := flannel.Run(ctx, nodeConfig, coreClient.CoreV1().Nodes()); err != nil { if err := flannel.Run(ctx, nodeConfig, coreClient.CoreV1().Nodes()); err != nil {
......
...@@ -16,7 +16,7 @@ import ( ...@@ -16,7 +16,7 @@ import (
"github.com/rancher/k3s/pkg/daemons/config" "github.com/rancher/k3s/pkg/daemons/config"
"github.com/rancher/k3s/pkg/daemons/control/deps" "github.com/rancher/k3s/pkg/daemons/control/deps"
"github.com/rancher/k3s/pkg/daemons/executor" "github.com/rancher/k3s/pkg/daemons/executor"
util2 "github.com/rancher/k3s/pkg/util" "github.com/rancher/k3s/pkg/util"
"github.com/rancher/k3s/pkg/version" "github.com/rancher/k3s/pkg/version"
"github.com/rancher/wrangler-api/pkg/generated/controllers/rbac" "github.com/rancher/wrangler-api/pkg/generated/controllers/rbac"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
...@@ -26,9 +26,8 @@ import ( ...@@ -26,9 +26,8 @@ import (
"k8s.io/apiserver/pkg/authentication/authenticator" "k8s.io/apiserver/pkg/authentication/authenticator"
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd"
app2 "k8s.io/controller-manager/app"
"k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes" "k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes"
"k8s.io/kubernetes/pkg/proxy/util" proxyutil "k8s.io/kubernetes/pkg/proxy/util"
// for client metric registration // for client metric registration
_ "k8s.io/component-base/metrics/prometheus/restclient" _ "k8s.io/component-base/metrics/prometheus/restclient"
...@@ -47,7 +46,7 @@ func Server(ctx context.Context, cfg *config.Control) error { ...@@ -47,7 +46,7 @@ func Server(ctx context.Context, cfg *config.Control) error {
} }
cfg.Runtime.Tunnel = setupTunnel() cfg.Runtime.Tunnel = setupTunnel()
util.DisableProxyHostnameCheck = true proxyutil.DisableProxyHostnameCheck = true
var auth authenticator.Request var auth authenticator.Request
var handler http.Handler var handler http.Handler
...@@ -96,7 +95,7 @@ func controllerManager(cfg *config.Control, runtime *config.ControlRuntime) erro ...@@ -96,7 +95,7 @@ func controllerManager(cfg *config.Control, runtime *config.ControlRuntime) erro
"kubeconfig": runtime.KubeConfigController, "kubeconfig": runtime.KubeConfigController,
"service-account-private-key-file": runtime.ServiceKey, "service-account-private-key-file": runtime.ServiceKey,
"allocate-node-cidrs": "true", "allocate-node-cidrs": "true",
"cluster-cidr": util2.JoinIPNets(cfg.ClusterIPRanges), "cluster-cidr": util.JoinIPNets(cfg.ClusterIPRanges),
"root-ca-file": runtime.ServerCA, "root-ca-file": runtime.ServerCA,
"port": "10252", "port": "10252",
"profiling": "false", "profiling": "false",
...@@ -157,7 +156,7 @@ func apiServer(ctx context.Context, cfg *config.Control, runtime *config.Control ...@@ -157,7 +156,7 @@ func apiServer(ctx context.Context, cfg *config.Control, runtime *config.Control
argsMap["allow-privileged"] = "true" argsMap["allow-privileged"] = "true"
argsMap["authorization-mode"] = strings.Join([]string{modes.ModeNode, modes.ModeRBAC}, ",") argsMap["authorization-mode"] = strings.Join([]string{modes.ModeNode, modes.ModeRBAC}, ",")
argsMap["service-account-signing-key-file"] = runtime.ServiceKey argsMap["service-account-signing-key-file"] = runtime.ServiceKey
argsMap["service-cluster-ip-range"] = util2.JoinIPNets(cfg.ServiceIPRanges) argsMap["service-cluster-ip-range"] = util.JoinIPNets(cfg.ServiceIPRanges)
argsMap["service-node-port-range"] = cfg.ServiceNodePortRange.String() argsMap["service-node-port-range"] = cfg.ServiceNodePortRange.String()
argsMap["advertise-port"] = strconv.Itoa(cfg.AdvertisePort) argsMap["advertise-port"] = strconv.Itoa(cfg.AdvertisePort)
if cfg.AdvertiseIP != "" { if cfg.AdvertiseIP != "" {
...@@ -345,14 +344,14 @@ func setupStorageBackend(argsMap map[string]string, cfg *config.Control) { ...@@ -345,14 +344,14 @@ func setupStorageBackend(argsMap map[string]string, cfg *config.Control) {
func cloudControllerManager(ctx context.Context, cfg *config.Control, runtime *config.ControlRuntime) error { func cloudControllerManager(ctx context.Context, cfg *config.Control, runtime *config.ControlRuntime) error {
argsMap := map[string]string{ argsMap := map[string]string{
"profiling": "false", "profiling": "false",
"allocate-node-cidrs": "true", // ccmOptions.KubeCloudShared.AllocateNodeCIDRs = true "allocate-node-cidrs": "true",
"cloud-provider": version.Program, // ccmOptions.KubeCloudShared.CloudProvider.Name = version.Program "cloud-provider": version.Program,
"cluster-cidr": util2.JoinIPNets(cfg.ClusterIPRanges), // ccmOptions.KubeCloudShared.ClusterCIDR = util2.JoinIPNets(cfg.ClusterIPRanges) "cluster-cidr": util.JoinIPNets(cfg.ClusterIPRanges),
"configure-cloud-routes": "false", // ccmOptions.KubeCloudShared.ConfigureCloudRoutes = false "configure-cloud-routes": "false",
"kubeconfig": runtime.KubeConfigCloudController, // ccmOptions.Kubeconfig = runtime.KubeConfigCloudController "kubeconfig": runtime.KubeConfigCloudController,
"node-status-update-frequency": "1m0s", // ccmOptions.NodeStatusUpdateFrequency = metav1.Duration{Duration: 1 * time.Minute} "node-status-update-frequency": "1m0s",
"bind-address": "127.0.0.1", // ccmOptions.SecureServing.BindAddress = localhostIP "bind-address": "127.0.0.1",
"port": "0", // ccmOptions.SecureServing.BindPort = 0 "port": "0",
} }
if cfg.NoLeaderElect { if cfg.NoLeaderElect {
argsMap["leader-elect"] = "false" argsMap["leader-elect"] = "false"
...@@ -453,7 +452,7 @@ func waitForAPIServerInBackground(ctx context.Context, runtime *config.ControlRu ...@@ -453,7 +452,7 @@ func waitForAPIServerInBackground(ctx context.Context, runtime *config.ControlRu
select { select {
case <-ctx.Done(): case <-ctx.Done():
return return
case err := <-promise(func() error { return app2.WaitForAPIServer(k8sClient, 30*time.Second) }): case err := <-promise(func() error { return util.WaitForAPIServerReady(k8sClient, 30*time.Second) }):
if err != nil { if err != nil {
logrus.Infof("Waiting for API server to become available") logrus.Infof("Waiting for API server to become available")
continue continue
......
package util package util
import ( import (
"context"
"fmt"
"net" "net"
"net/http"
"strconv" "strconv"
"time"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
clientset "k8s.io/client-go/kubernetes"
) )
func GetAddresses(endpoint *v1.Endpoints) []string { func GetAddresses(endpoint *v1.Endpoints) []string {
...@@ -26,3 +33,32 @@ func GetAddresses(endpoint *v1.Endpoints) []string { ...@@ -26,3 +33,32 @@ func GetAddresses(endpoint *v1.Endpoints) []string {
} }
return serverAddresses return serverAddresses
} }
// WaitForAPIServerReady waits for the API Server's /readyz endpoint to report "ok" with timeout.
// This is cribbed from the Kubernetes controller-manager app, but checks the readyz endpoint instead of the deprecated healthz endpoint.
func WaitForAPIServerReady(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("/readyz").Do(context.TODO()).StatusCode(&healthStatus)
if result.Error() != nil {
lastErr = fmt.Errorf("failed to get apiserver /readyz status: %v", result.Error())
return false, nil
}
if healthStatus != http.StatusOK {
content, _ := result.Raw()
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 true, nil
})
if err != nil {
return fmt.Errorf("%v: %v", err, lastErr)
}
return 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