cmd/k8s-operator,k8s-operator: apply ProxyClass probes to PeerRelay pods (#22798)

* cmd/k8s-operator,k8s-operator: apply ProxyClass probes to PeerRelay pods

The ProxyClass livenessProbe and readinessProbe from #22461 were applied
only to ProxyGroups. A PeerRelay StatefulSet rolls with RollingUpdate, but
its Pods had no readiness probe, so a template change replaced every
relay within seconds and peers relaying through them fell back to DERP.

The probe helpers move from cmd/k8s-operator to the shared tailscaled
reconciler package, and ApplyProxyClass now applies them to the relay
container. ProxyGroups use the same code, unchanged. With a
readinessProbe set, a PeerRelay rollout waits for each new relay to be
Ready before it replaces the next one.

Fixes #22670

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Nurullah Morshed <nurullah@inworld.ai>

* k8s-operator: report a PeerRelay ProxyClass whose probes can't run

A ProxyGroup reports a ProxyClass that sets probes while its env turns
the /livez and /readyz endpoints off as invalid. A PeerRelay skipped the
probes without saying so.

The check moves to tailscaled.ValidateProbes, shared by both. A PeerRelay
whose ProxyClass fails it gets Ready=False with reason ProxyClassInvalid,
and nothing is created, as for its other invalid specs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Nurullah Morshed <nurullah@inworld.ai>

* k8s-operator/reconciler/tailscaled: rename probes subtest for tsvet

Updates #22670

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Nurullah Morshed <nurullah@inworld.ai>

---------

Signed-off-by: Nurullah Morshed <nurullah@inworld.ai>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Nurullah MorshedandClaude Opus 5.5 authored and GitHub committed 2026-12-04 10:54:35 +00:00
1 parent 44fc559bf8
commit 81fe9507ef
13 files changed
+278 -132

No files matched your search

@@ -1467,7 +1467,8 @@ spec:
Liveness probe against the proxy's /livez endpoint, which fails only when
tailscaled stops responding, not when the tailnet is unreachable. Off
unless set; {} runs it with the default timing. Older proxy images don't
serve /livez and fail the probe. Only used by ProxyGroups.
serve /livez and fail the probe. Only used by ProxyGroups and
PeerRelays.
type: object
properties:
failureThreshold:
@@ -1505,8 +1506,8 @@ spec:
Services, has applied its serve config and has a TLS cert for each
HTTPS-only Tailscale Service. Off unless set; {} runs it with the default
timing. Older proxy images don't serve /readyz and fail the probe.
Only used by ProxyGroups. On kube-apiserver ProxyGroups it only waits
for the tailnet IP address.
Only used by ProxyGroups and PeerRelays. On kube-apiserver ProxyGroups
and PeerRelays it only waits for the tailnet IP address.
type: object
properties:
failureThreshold:
@@ -1882,7 +1883,8 @@ spec:
Liveness probe against the proxy's /livez endpoint, which fails only when
tailscaled stops responding, not when the tailnet is unreachable. Off
unless set; {} runs it with the default timing. Older proxy images don't
serve /livez and fail the probe. Only used by ProxyGroups.
serve /livez and fail the probe. Only used by ProxyGroups and
PeerRelays.
type: object
properties:
failureThreshold:
@@ -1920,8 +1922,8 @@ spec:
Services, has applied its serve config and has a TLS cert for each
HTTPS-only Tailscale Service. Off unless set; {} runs it with the default
timing. Older proxy images don't serve /readyz and fail the probe.
Only used by ProxyGroups. On kube-apiserver ProxyGroups it only waits
for the tailnet IP address.
Only used by ProxyGroups and PeerRelays. On kube-apiserver ProxyGroups
and PeerRelays it only waits for the tailnet IP address.
type: object
properties:
failureThreshold:
@@ -3270,7 +3270,8 @@ spec:
Liveness probe against the proxy's /livez endpoint, which fails only when
tailscaled stops responding, not when the tailnet is unreachable. Off
unless set; {} runs it with the default timing. Older proxy images don't
serve /livez and fail the probe. Only used by ProxyGroups.
serve /livez and fail the probe. Only used by ProxyGroups and
PeerRelays.
properties:
failureThreshold:
default: 3
@@ -3308,8 +3309,8 @@ spec:
Services, has applied its serve config and has a TLS cert for each
HTTPS-only Tailscale Service. Off unless set; {} runs it with the default
timing. Older proxy images don't serve /readyz and fail the probe.
Only used by ProxyGroups. On kube-apiserver ProxyGroups it only waits
for the tailnet IP address.
Only used by ProxyGroups and PeerRelays. On kube-apiserver ProxyGroups
and PeerRelays it only waits for the tailnet IP address.
properties:
failureThreshold:
default: 3
@@ -3685,7 +3686,8 @@ spec:
Liveness probe against the proxy's /livez endpoint, which fails only when
tailscaled stops responding, not when the tailnet is unreachable. Off
unless set; {} runs it with the default timing. Older proxy images don't
serve /livez and fail the probe. Only used by ProxyGroups.
serve /livez and fail the probe. Only used by ProxyGroups and
PeerRelays.
properties:
failureThreshold:
default: 3
@@ -3723,8 +3725,8 @@ spec:
Services, has applied its serve config and has a TLS cert for each
HTTPS-only Tailscale Service. Off unless set; {} runs it with the default
timing. Older proxy images don't serve /readyz and fail the probe.
Only used by ProxyGroups. On kube-apiserver ProxyGroups it only waits
for the tailnet IP address.
Only used by ProxyGroups and PeerRelays. On kube-apiserver ProxyGroups
and PeerRelays it only waits for the tailnet IP address.
properties:
failureThreshold:
default: 3
+2 -10
View File
@@ -319,16 +319,8 @@ func (r *ProxyGroupReconciler) validate(ctx context.Context, pg *tsapi.ProxyGrou
errs = append(errs, fmt.Errorf("the configured ProxyClass %q specifies to use image %q but expected a %q image for ProxyGroup of type %q", pc.Name, image, "tailscale", pg.Spec.Type))
}
if pc != nil && pc.Spec.StatefulSet != nil && pc.Spec.StatefulSet.Pod != nil && pc.Spec.StatefulSet.Pod.TailscaleContainer != nil {
if tc := pc.Spec.StatefulSet.Pod.TailscaleContainer; tc.LivenessProbe != nil || tc.ReadinessProbe != nil {
var env []corev1.EnvVar
for _, e := range tc.Env {
env = append(env, corev1.EnvVar{Name: string(e.Name), Value: e.Value})
}
if _, ok := healthzPort(env); !ok {
errs = append(errs, fmt.Errorf("the configured ProxyClass %q sets probes, but its TS_HEALTHCHECK_ADDR_PORT, TS_ENABLE_HEALTH_CHECK or TS_LOCAL_ADDR_PORT env var turns off the /livez and /readyz endpoints or binds them where the kubelet can't reach them", pc.Name))
}
}
if err := tailscaled.ValidateProbes(pc); err != nil {
errs = append(errs, err)
}
}
+4 -4
View File
@@ -1598,10 +1598,10 @@ func TestProxyGroupTypes(t *testing.T) {
if err := fc.Get(t.Context(), client.ObjectKey{Namespace: tsNamespace, Name: pg.Name}, sts); err != nil {
t.Fatalf("failed to get StatefulSet: %v", err)
}
if diff := cmp.Diff(httpProbe("/livez", defaultLocalAddrPort, &tsapi.Probe{}, 5), sts.Spec.Template.Spec.Containers[0].LivenessProbe); diff != "" {
if diff := cmp.Diff(tailscaled.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 != "" {
if diff := cmp.Diff(tailscaled.HTTPProbe("/readyz", defaultLocalAddrPort, &tsapi.Probe{}, 1), sts.Spec.Template.Spec.Containers[0].ReadinessProbe); diff != "" {
t.Errorf("unexpected readiness probe (-want +got):\n%s", diff)
}
})
@@ -1862,10 +1862,10 @@ 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 != "" {
if diff := cmp.Diff(tailscaled.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 != "" {
if diff := cmp.Diff(tailscaled.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 {
+8 -101
View File
@@ -6,14 +6,12 @@
package main
import (
"cmp"
"context"
_ "embed"
"encoding/json"
"errors"
"fmt"
"maps"
"net"
"net/netip"
"os"
"path"
@@ -30,7 +28,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apiserver/pkg/storage/names"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"
@@ -1278,108 +1275,18 @@ func isMainContainer(c *corev1.Container) bool {
return c.Name == mainContainerName
}
// applyHealthzProbes attaches liveness and readiness probes against the proxy's
// /livez and /readyz endpoints to the main container, and enables those
// endpoints via TS_ENABLE_HEALTH_CHECK. Each probe is off unless the ProxyClass
// sets it. pc may be nil. It is a no-op if neither probe is set, or if the
// container's env leaves the endpoints unreachable, see [healthzPort]. If
// k8sProxy is set, the container runs k8s-proxy, which serves the endpoints on
// defaultLocalAddrPort from its config file and ignores the env.
// applyHealthzProbes attaches the ProxyClass's liveness and readiness probes to
// the main container; see [tailscaled.ApplyProbes]. pc may be nil. If k8sProxy
// is set, the container runs k8s-proxy.
func applyHealthzProbes(pc *tsapi.ProxyClass, ss *appsv1.StatefulSet, k8sProxy bool) {
var liveness, readiness *tsapi.Probe
if pc != nil && pc.Spec.StatefulSet != nil && pc.Spec.StatefulSet.Pod != nil && pc.Spec.StatefulSet.Pod.TailscaleContainer != nil {
liveness = pc.Spec.StatefulSet.Pod.TailscaleContainer.LivenessProbe
readiness = pc.Spec.StatefulSet.Pod.TailscaleContainer.ReadinessProbe
}
if liveness == nil && readiness == nil {
if pc == nil || pc.Spec.StatefulSet == nil || pc.Spec.StatefulSet.Pod == nil {
return
}
for i, c := range ss.Spec.Template.Spec.Containers {
if !isMainContainer(&c) {
continue
for i := range ss.Spec.Template.Spec.Containers {
if c := &ss.Spec.Template.Spec.Containers[i]; isMainContainer(c) {
tailscaled.ApplyProbes(c, pc.Spec.StatefulSet.Pod.TailscaleContainer, k8sProxy)
return
}
port := defaultLocalAddrPort
if !k8sProxy {
var ok bool
if port, ok = healthzPort(c.Env); !ok {
return
}
if _, ok := lookupEnv(c.Env, "TS_ENABLE_HEALTH_CHECK"); !ok {
ss.Spec.Template.Spec.Containers[i].Env = append(ss.Spec.Template.Spec.Containers[i].Env,
corev1.EnvVar{Name: "TS_ENABLE_HEALTH_CHECK", Value: "true"})
}
}
if liveness != nil {
// Longer than for readiness, as a restart drops the traffic a slow tailscaled still carries.
ss.Spec.Template.Spec.Containers[i].LivenessProbe = httpProbe("/livez", port, liveness, 5)
}
if readiness != nil {
ss.Spec.Template.Spec.Containers[i].ReadinessProbe = httpProbe("/readyz", port, readiness, 1)
}
break
}
}
// healthzPort returns the port the main container serves /livez and /readyz
// on given its env. It returns false if the env turns the endpoints off, or
// binds them where the kubelet can't reach them, as probes would then restart
// a healthy proxy.
func healthzPort(env []corev1.EnvVar) (int, bool) {
if _, ok := lookupEnv(env, "TS_HEALTHCHECK_ADDR_PORT"); ok {
// containerboot refuses to start with both this and TS_ENABLE_HEALTH_CHECK.
return 0, false
}
if v, ok := lookupEnv(env, "TS_ENABLE_HEALTH_CHECK"); ok {
if on, err := strconv.ParseBool(v); err != nil || !on {
return 0, false
}
}
v, ok := lookupEnv(env, envVarTSLocalAddrPort)
if !ok {
return defaultLocalAddrPort, true
}
// The host may be an env var reference such as $(POD_IP).
host, port, err := net.SplitHostPort(v)
if err != nil {
return 0, false
}
if ip, err := netip.ParseAddr(host); host == "localhost" || err == nil && ip.IsLoopback() {
return 0, false
}
p, err := strconv.ParseUint(port, 10, 16)
if err != nil || p == 0 {
return 0, false
}
return int(p), true
}
// lookupEnv returns the value of the last env var in env called name, as
// Kubernetes uses the last of any duplicates.
func lookupEnv(env []corev1.EnvVar, name string) (string, bool) {
for _, e := range slices.Backward(env) {
if e.Name == name {
return e.Value, true
}
}
return "", false
}
// httpProbe builds an httpGet probe against path on port, using timeout and the
// Kubernetes defaults for unset timing fields.
func httpProbe(path string, port int, p *tsapi.Probe, timeout int32) *corev1.Probe {
return &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: path,
Port: intstr.FromInt(port),
},
},
InitialDelaySeconds: p.InitialDelaySeconds,
PeriodSeconds: cmp.Or(p.PeriodSeconds, 10),
TimeoutSeconds: cmp.Or(p.TimeoutSeconds, timeout),
FailureThreshold: cmp.Or(p.FailureThreshold, 3),
SuccessThreshold: 1,
}
}
+12
View File
@@ -11,6 +11,7 @@ import (
"maps"
"reflect"
"regexp"
"slices"
"strings"
"testing"
@@ -577,3 +578,14 @@ func TestCleanupUnreportedDevices(t *testing.T) {
func updateMap(a, b map[string]string) {
maps.Copy(a, b)
}
// lookupEnv returns the value of the last env var in env called name, as
// Kubernetes uses the last of any duplicates.
func lookupEnv(env []corev1.EnvVar, name string) (string, bool) {
for _, e := range slices.Backward(env) {
if e.Name == name {
return e.Value, true
}
}
return "", false
}