From db5015ec88663f107e40accd0d36ed7c082991f1 Mon Sep 17 00:00:00 2001 From: Benjamin Schimke Date: Sat, 4 May 2024 16:44:16 +0200 Subject: [PATCH] Wait until kubernetes endpoint becomes available (#397) --- src/k8s/pkg/client/kubernetes/status.go | 8 ++++---- src/k8s/pkg/k8sd/api/worker.go | 4 ++-- src/k8s/pkg/k8sd/app/cluster_util.go | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/k8s/pkg/client/kubernetes/status.go b/src/k8s/pkg/client/kubernetes/status.go index 1c478217d..b09c7352f 100644 --- a/src/k8s/pkg/client/kubernetes/status.go +++ b/src/k8s/pkg/client/kubernetes/status.go @@ -9,14 +9,14 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -// WaitApiServerReady waits until the kube-apiserver becomes available. -func (c *Client) WaitApiServerReady(ctx context.Context) error { +// WaitKubernetesEndpointAvailable waits until the kubernetes endpoint becomes available. +func (c *Client) WaitKubernetesEndpointAvailable(ctx context.Context) error { return control.WaitUntilReady(ctx, func() (bool, error) { // TODO: use the /readyz endpoint instead // We want to retry if an error occurs (=API server not ready) // returning the error would abort, thus checking for nil - _, err := c.CoreV1().Nodes().List(ctx, metav1.ListOptions{}) - return err == nil, nil + endpoint, err := c.CoreV1().Endpoints("default").Get(ctx, "kubernetes", metav1.GetOptions{}) + return err == nil && endpoint != nil, nil }) } diff --git a/src/k8s/pkg/k8sd/api/worker.go b/src/k8s/pkg/k8sd/api/worker.go index 424e15f45..aa900d7af 100644 --- a/src/k8s/pkg/k8sd/api/worker.go +++ b/src/k8s/pkg/k8sd/api/worker.go @@ -48,8 +48,8 @@ func (e *Endpoints) postWorkerInfo(s *state.State, r *http.Request) response.Res if err != nil { return response.InternalError(fmt.Errorf("failed to create kubernetes client: %w", err)) } - if err := client.WaitApiServerReady(s.Context); err != nil { - return response.InternalError(fmt.Errorf("kube-apiserver did not become ready in time: %w", err)) + if err := client.WaitKubernetesEndpointAvailable(s.Context); err != nil { + return response.InternalError(fmt.Errorf("kubernetes endpoints not ready yet: %w", err)) } servers, err := client.GetKubeAPIServerEndpoints(s.Context) if err != nil { diff --git a/src/k8s/pkg/k8sd/app/cluster_util.go b/src/k8s/pkg/k8sd/app/cluster_util.go index f27a129da..8ca695466 100644 --- a/src/k8s/pkg/k8sd/app/cluster_util.go +++ b/src/k8s/pkg/k8sd/app/cluster_util.go @@ -87,8 +87,8 @@ func waitApiServerReady(ctx context.Context, snap snap.Snap) error { return fmt.Errorf("failed to create Kubernetes client: %w", err) } - if err := client.WaitApiServerReady(ctx); err != nil { - return fmt.Errorf("kube-apiserver did not become ready in time: %w", err) + if err := client.WaitKubernetesEndpointAvailable(ctx); err != nil { + return fmt.Errorf("kubernetes endpoints not ready yet: %w", err) } return nil