From 5aed346ad76e2f0e4d889200d86cfd28c17700d1 Mon Sep 17 00:00:00 2001 From: chaosinthecrd Date: Mon, 26 Oct 2026 06:43:49 +0000 Subject: [PATCH] cmd/k8s-proxy,cmd/k8s-operator: test k8s-proxy /livez and /readyz Covers /livez and /readyz with and without tailnet IPs and with the LocalAPI failing or timing out, and the probes that a ProxyClass enables on the kube-apiserver ProxyGroup StatefulSet, which stay on port 9002 when the ProxyClass sets TS_LOCAL_ADDR_PORT. Updates tailscale/corp#38175 Signed-off-by: chaosinthecrd --- cmd/k8s-operator/proxygroup_test.go | 44 ++++++++++++++++++++--- cmd/k8s-proxy/health_test.go | 55 +++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 cmd/k8s-proxy/health_test.go diff --git a/cmd/k8s-operator/proxygroup_test.go b/cmd/k8s-operator/proxygroup_test.go index 31f44bce1..90a8da04e 100644 --- a/cmd/k8s-operator/proxygroup_test.go +++ b/cmd/k8s-operator/proxygroup_test.go @@ -1551,14 +1551,41 @@ func TestProxyGroupTypes(t *testing.T) { }) t.Run("kubernetes_api_server_type", func(t *testing.T) { + healthzPC := &tsapi.ProxyClass{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-healthz", + Generation: 1, + }, + Spec: tsapi.ProxyClassSpec{ + StatefulSet: &tsapi.StatefulSet{ + Pod: &tsapi.Pod{ + TailscaleContainer: &tsapi.Container{ + // k8s-proxy ignores this, so the probes must stay on defaultLocalAddrPort. + Env: []tsapi.Env{{Name: "TS_LOCAL_ADDR_PORT", Value: "0.0.0.0:8080"}}, + LivenessProbe: &tsapi.Probe{}, + ReadinessProbe: &tsapi.Probe{}, + }, + }, + }, + }, + } + mustCreate(t, fc, healthzPC) + mustUpdateStatus(t, fc, "", healthzPC.Name, func(p *tsapi.ProxyClass) { + p.Status.Conditions = []metav1.Condition{{ + Type: string(tsapi.ProxyClassReady), + Status: metav1.ConditionTrue, + ObservedGeneration: 1, + }} + }) pg := &tsapi.ProxyGroup{ ObjectMeta: metav1.ObjectMeta{ Name: "test-k8s-apiserver", UID: "test-k8s-apiserver-uid", }, Spec: tsapi.ProxyGroupSpec{ - Type: tsapi.ProxyGroupTypeKubernetesAPIServer, - Replicas: new(int32(2)), + Type: tsapi.ProxyGroupTypeKubernetesAPIServer, + Replicas: new(int32(2)), + ProxyClass: healthzPC.Name, KubeAPIServer: &tsapi.KubeAPIServerConfig{ Mode: new(tsapi.APIServerProxyModeNoAuth), }, @@ -1589,6 +1616,15 @@ func TestProxyGroupTypes(t *testing.T) { if sts.Spec.PodManagementPolicy != appsv1.ParallelPodManagement { t.Errorf("unexpected PodManagementPolicy %q, want %q", sts.Spec.PodManagementPolicy, appsv1.ParallelPodManagement) } + if diff := cmp.Diff(httpProbe("/livez", defaultLocalAddrPort, &tsapi.Probe{}, 5), sts.Spec.Template.Spec.Containers[0].LivenessProbe); diff != "" { + t.Errorf("unexpected liveness probe (-want +got):\n%s", diff) + } + if diff := cmp.Diff(httpProbe("/readyz", defaultLocalAddrPort, &tsapi.Probe{}, 1), sts.Spec.Template.Spec.Containers[0].ReadinessProbe); diff != "" { + t.Errorf("unexpected readiness probe (-want +got):\n%s", diff) + } + if _, ok := lookupEnv(sts.Spec.Template.Spec.Containers[0].Env, "TS_ENABLE_HEALTH_CHECK"); ok { + t.Errorf("TS_ENABLE_HEALTH_CHECK set on k8s-proxy container, which doesn't read it") + } }) } @@ -2355,9 +2391,7 @@ func expectProxyGroupResources(t *testing.T, fc client.WithWatch, pg *tsapi.Prox t.Fatal(err) } statefulSet.Annotations = defaultProxyClassAnnotations - if pg.Spec.Type != tsapi.ProxyGroupTypeKubernetesAPIServer { - applyHealthzProbes(proxyClass, statefulSet, false) - } + applyHealthzProbes(proxyClass, statefulSet, pg.Spec.Type == tsapi.ProxyGroupTypeKubernetesAPIServer) if shouldExist { expectEqual(t, fc, role) diff --git a/cmd/k8s-proxy/health_test.go b/cmd/k8s-proxy/health_test.go new file mode 100644 index 000000000..f2b7523dd --- /dev/null +++ b/cmd/k8s-proxy/health_test.go @@ -0,0 +1,55 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +//go:build !plan9 + +package main + +import ( + "context" + "errors" + "net/http" + "net/http/httptest" + "net/netip" + "testing" + + "tailscale.com/ipn/ipnstate" + "tailscale.com/util/httpm" +) + +type fakeHealthLocalClient struct { + status *ipnstate.Status + err error +} + +func (f *fakeHealthLocalClient) StatusWithoutPeers(context.Context) (*ipnstate.Status, error) { + return f.status, f.err +} + +func TestHealthChecks(t *testing.T) { + withIPs := &ipnstate.Status{Self: &ipnstate.PeerStatus{TailscaleIPs: []netip.Addr{netip.MustParseAddr("100.64.0.1")}}} + + for name, tt := range map[string]struct { + lc *fakeHealthLocalClient + wantLivez int + wantReadyz int + }{ + "tailnet_ips": {lc: &fakeHealthLocalClient{status: withIPs}, wantLivez: http.StatusOK, wantReadyz: http.StatusOK}, + "no_tailnet_ips": {lc: &fakeHealthLocalClient{status: &ipnstate.Status{Self: &ipnstate.PeerStatus{}}}, wantLivez: http.StatusOK, wantReadyz: http.StatusServiceUnavailable}, + "no_self": {lc: &fakeHealthLocalClient{status: &ipnstate.Status{}}, wantLivez: http.StatusOK, wantReadyz: http.StatusServiceUnavailable}, + "localapi_failing": {lc: &fakeHealthLocalClient{err: errors.New("connection refused")}, wantLivez: http.StatusServiceUnavailable, wantReadyz: http.StatusServiceUnavailable}, + "localapi_timedout": {lc: &fakeHealthLocalClient{err: context.DeadlineExceeded}, wantLivez: http.StatusServiceUnavailable, wantReadyz: http.StatusServiceUnavailable}, + } { + t.Run(name, func(t *testing.T) { + mux := http.NewServeMux() + registerHealthChecks(mux, tt.lc, t.Logf) + for path, want := range map[string]int{"/livez": tt.wantLivez, "/readyz": tt.wantReadyz} { + w := httptest.NewRecorder() + mux.ServeHTTP(w, httptest.NewRequest(httpm.GET, path, nil)) + if w.Code != want { + t.Errorf("GET %s = %d, want %d (body %q)", path, w.Code, want, w.Body.String()) + } + } + }) + } +}