mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-04 23:52:20 -08:00
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 <[email protected]> Signed-off-by: Nurullah Morshed <[email protected]> * 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 <[email protected]> Signed-off-by: Nurullah Morshed <[email protected]> * k8s-operator/reconciler/tailscaled: rename probes subtest for tsvet Updates #22670 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Nurullah Morshed <[email protected]> --------- Signed-off-by: Nurullah Morshed <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
+2
-2
@@ -195,8 +195,8 @@ _Appears in:_
|
||||
| `resources` _[ResourceRequirements](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.3/#resourcerequirements-v1-core)_ | Container resource requirements.<br />By default Tailscale Kubernetes operator does not apply any resource<br />requirements. The amount of resources required wil depend on the<br />amount of resources the operator needs to parse, usage patterns and<br />cluster size.<br />https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/#resources | | Optional: \{\} <br /> |
|
||||
| `securityContext` _[SecurityContext](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.3/#securitycontext-v1-core)_ | Container security context.<br />Security context specified here will override the security context set by the operator.<br />By default the operator sets the Tailscale container and the Tailscale init container to privileged<br />for proxies created for Tailscale ingress and egress Service, Connector and ProxyGroup.<br />You can reduce the permissions of the Tailscale container to cap NET_ADMIN by<br />installing device plugin in your cluster and configuring the proxies tun device to be created<br />by the device plugin, see https://github.com/tailscale/tailscale/issues/10814#issuecomment-2479977752<br />https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/#security-context | | Optional: \{\} <br /> |
|
||||
| `debug` _[Debug](#debug)_ | Configuration for enabling extra debug information in the container.<br />Not recommended for production use. | | Optional: \{\} <br /> |
|
||||
| `livenessProbe` _[Probe](#probe)_ | Liveness probe against the proxy's /livez endpoint, which fails only when<br />tailscaled stops responding, not when the tailnet is unreachable. Off<br />unless set; \{\} runs it with the default timing. Older proxy images don't<br />serve /livez and fail the probe. Only used by ProxyGroups. | | Optional: \{\} <br /> |
|
||||
| `readinessProbe` _[Probe](#probe)_ | Readiness probe against the proxy's /readyz endpoint, which passes once<br />the proxy has a tailnet IP address and, for proxies that serve Tailscale<br />Services, has applied its serve config and has a TLS cert for each<br />HTTPS-only Tailscale Service. Off unless set; \{\} runs it with the default<br />timing. Older proxy images don't serve /readyz and fail the probe.<br />Only used by ProxyGroups. On kube-apiserver ProxyGroups it only waits<br />for the tailnet IP address. | | Optional: \{\} <br /> |
|
||||
| `livenessProbe` _[Probe](#probe)_ | Liveness probe against the proxy's /livez endpoint, which fails only when<br />tailscaled stops responding, not when the tailnet is unreachable. Off<br />unless set; \{\} runs it with the default timing. Older proxy images don't<br />serve /livez and fail the probe. Only used by ProxyGroups and<br />PeerRelays. | | Optional: \{\} <br /> |
|
||||
| `readinessProbe` _[Probe](#probe)_ | Readiness probe against the proxy's /readyz endpoint, which passes once<br />the proxy has a tailnet IP address and, for proxies that serve Tailscale<br />Services, has applied its serve config and has a TLS cert for each<br />HTTPS-only Tailscale Service. Off unless set; \{\} runs it with the default<br />timing. Older proxy images don't serve /readyz and fail the probe.<br />Only used by ProxyGroups and PeerRelays. On kube-apiserver ProxyGroups<br />and PeerRelays it only waits for the tailnet IP address. | | Optional: \{\} <br /> |
|
||||
|
||||
|
||||
#### DNSConfig
|
||||
|
||||
@@ -434,7 +434,8 @@ type Container struct {
|
||||
// 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.
|
||||
// +optional
|
||||
LivenessProbe *Probe `json:"livenessProbe,omitempty"`
|
||||
// Readiness probe against the proxy's /readyz endpoint, which passes once
|
||||
@@ -442,8 +443,8 @@ type Container struct {
|
||||
// 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.
|
||||
// +optional
|
||||
ReadinessProbe *Probe `json:"readinessProbe,omitempty"`
|
||||
}
|
||||
|
||||
@@ -98,6 +98,7 @@ const (
|
||||
ReasonStaticEndpointsInvalid = "StaticEndpointsInvalid"
|
||||
ReasonTailnetUnavailable = "TailnetUnavailable"
|
||||
ReasonProxyClassNotReady = "ProxyClassNotReady"
|
||||
ReasonProxyClassInvalid = "ProxyClassInvalid"
|
||||
ReasonReady = "PeerRelayReady"
|
||||
)
|
||||
|
||||
@@ -266,6 +267,17 @@ func (r *Reconciler) createOrUpdate(ctx context.Context, logger *zap.SugaredLogg
|
||||
return reconcile.Result{}, nil
|
||||
}
|
||||
|
||||
// A ProxyClass that sets probes but turns their endpoints off would run the relay without them; say so
|
||||
// instead, as ProxyGroups do. The ProxyClass watch re-enqueues this PeerRelay once it is fixed.
|
||||
if err := tailscaled.ValidateProbes(pc); err != nil {
|
||||
logger.Info(err.Error())
|
||||
operatorutils.SetPeerRelayCondition(pr, tsapi.PeerRelayReady, metav1.ConditionFalse, ReasonProxyClassInvalid, err.Error(), r.clock, logger)
|
||||
if err := r.Status().Update(ctx, pr); err != nil {
|
||||
return reconcile.Result{}, fmt.Errorf("failed to update PeerRelay status for %q: %w", pr.Name, err)
|
||||
}
|
||||
return reconcile.Result{}, nil
|
||||
}
|
||||
|
||||
for i := int32(0); i < replicas; i++ {
|
||||
desired := r.peerRelayService(pr, i)
|
||||
if err := r.ensureService(ctx, logger, desired); err != nil {
|
||||
|
||||
@@ -1063,6 +1063,34 @@ func TestReconciler_Reconcile(t *testing.T) {
|
||||
ExpectedReadyStatus: metav1.ConditionFalse,
|
||||
ExpectedReadyReason: peerrelay.ReasonProxyClassNotReady,
|
||||
},
|
||||
{
|
||||
// A ProxyClass that sets probes but turns their endpoints off is reported, not silently ignored.
|
||||
Name: "proxyclass-probes-with-endpoints-off",
|
||||
Request: reconcile.Request{NamespacedName: types.NamespacedName{Name: "test"}},
|
||||
PeerRelay: &tsapi.PeerRelay{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "test"},
|
||||
Spec: tsapi.PeerRelaySpec{ProxyClass: "custom"},
|
||||
},
|
||||
ExistingResources: []client.Object{
|
||||
&tsapi.ProxyClass{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "custom"},
|
||||
Spec: tsapi.ProxyClassSpec{
|
||||
StatefulSet: &tsapi.StatefulSet{
|
||||
Pod: &tsapi.Pod{
|
||||
TailscaleContainer: &tsapi.Container{
|
||||
Env: []tsapi.Env{{Name: "TS_ENABLE_HEALTH_CHECK", Value: "false"}},
|
||||
ReadinessProbe: &tsapi.Probe{},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Status: readyProxyClassStatus(),
|
||||
},
|
||||
},
|
||||
ExpectStatefulSetGone: true,
|
||||
ExpectedReadyStatus: metav1.ConditionFalse,
|
||||
ExpectedReadyReason: peerrelay.ReasonProxyClassInvalid,
|
||||
},
|
||||
{
|
||||
Name: "deletion",
|
||||
Request: reconcile.Request{NamespacedName: types.NamespacedName{Name: "test"}},
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
//go:build !plan9
|
||||
|
||||
package tailscaled
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/netip"
|
||||
"slices"
|
||||
"strconv"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
|
||||
tsapi "tailscale.com/k8s-operator/apis/v1alpha1"
|
||||
)
|
||||
|
||||
// ApplyProbes attaches liveness and readiness probes against the proxy's /livez
|
||||
// and /readyz endpoints to c, and enables those endpoints via
|
||||
// TS_ENABLE_HEALTH_CHECK. Each probe is off unless tc sets it. tc may be nil.
|
||||
// It is a no-op if neither probe is set, or if c's env leaves the endpoints
|
||||
// unreachable, see [HealthzPort]. If k8sProxy is set, the container runs
|
||||
// k8s-proxy, which serves the endpoints on HealthCheckPort from its config file
|
||||
// and ignores the env.
|
||||
func ApplyProbes(c *corev1.Container, tc *tsapi.Container, k8sProxy bool) {
|
||||
if tc == nil || (tc.LivenessProbe == nil && tc.ReadinessProbe == nil) {
|
||||
return
|
||||
}
|
||||
port := HealthCheckPort
|
||||
if !k8sProxy {
|
||||
var ok bool
|
||||
if port, ok = HealthzPort(c.Env); !ok {
|
||||
return
|
||||
}
|
||||
if _, ok := lookupEnv(c.Env, "TS_ENABLE_HEALTH_CHECK"); !ok {
|
||||
c.Env = append(c.Env, corev1.EnvVar{Name: "TS_ENABLE_HEALTH_CHECK", Value: "true"})
|
||||
}
|
||||
}
|
||||
if tc.LivenessProbe != nil {
|
||||
// Longer than for readiness, as a restart drops the traffic a slow tailscaled still carries.
|
||||
c.LivenessProbe = HTTPProbe("/livez", port, tc.LivenessProbe, 5)
|
||||
}
|
||||
if tc.ReadinessProbe != nil {
|
||||
c.ReadinessProbe = HTTPProbe("/readyz", port, tc.ReadinessProbe, 1)
|
||||
}
|
||||
}
|
||||
|
||||
// ValidateProbes reports an error if pc sets a liveness or readiness probe but
|
||||
// its Tailscale container env turns off the /livez and /readyz endpoints, or
|
||||
// binds them where the kubelet can't reach them. [ApplyProbes] skips the probes
|
||||
// in that case, so the caller should surface this error instead of running
|
||||
// the proxy without them. pc may be nil.
|
||||
func ValidateProbes(pc *tsapi.ProxyClass) error {
|
||||
if pc == nil || pc.Spec.StatefulSet == nil || pc.Spec.StatefulSet.Pod == nil || pc.Spec.StatefulSet.Pod.TailscaleContainer == nil {
|
||||
return nil
|
||||
}
|
||||
tc := pc.Spec.StatefulSet.Pod.TailscaleContainer
|
||||
if tc.LivenessProbe == nil && tc.ReadinessProbe == nil {
|
||||
return 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 {
|
||||
return 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)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// HealthzPort returns the port a proxy 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, "TS_LOCAL_ADDR_PORT")
|
||||
if !ok {
|
||||
return HealthCheckPort, 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
|
||||
}
|
||||
|
||||
// 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,
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
@@ -76,6 +76,7 @@ func ApplyProxyClass(ss *appsv1.StatefulSet, pc *tsapi.ProxyClass, managedLabels
|
||||
}
|
||||
|
||||
applyContainerOverlay(c, wantsPod.TailscaleContainer)
|
||||
ApplyProbes(c, wantsPod.TailscaleContainer, false)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,3 +87,57 @@ func TestApplyProxyClass_RuntimeClassName(t *testing.T) {
|
||||
t.Errorf("RuntimeClassName = %v, want %q", got, "some-runtime")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyProxyClass_Probes(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
relay := func(env ...corev1.EnvVar) *appsv1.StatefulSet {
|
||||
ss := &appsv1.StatefulSet{}
|
||||
ss.Spec.Template.Spec.Containers = []corev1.Container{{
|
||||
Name: "tailscaled",
|
||||
Env: append([]corev1.EnvVar{{Name: "TS_ENABLE_HEALTH_CHECK", Value: "true"}}, env...),
|
||||
}}
|
||||
return ss
|
||||
}
|
||||
withProbes := func(env ...tsapi.Env) *tsapi.ProxyClass {
|
||||
return &tsapi.ProxyClass{
|
||||
Spec: tsapi.ProxyClassSpec{
|
||||
StatefulSet: &tsapi.StatefulSet{
|
||||
Pod: &tsapi.Pod{
|
||||
TailscaleContainer: &tsapi.Container{
|
||||
Env: env,
|
||||
LivenessProbe: &tsapi.Probe{},
|
||||
ReadinessProbe: &tsapi.Probe{FailureThreshold: 6},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("set", func(t *testing.T) {
|
||||
c := tailscaled.ApplyProxyClass(relay(), withProbes(), nil, nil).Spec.Template.Spec.Containers[0]
|
||||
if diff := cmp.Diff(tailscaled.HTTPProbe("/livez", tailscaled.HealthCheckPort, &tsapi.Probe{}, 5), c.LivenessProbe); diff != "" {
|
||||
t.Errorf("liveness probe mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
if diff := cmp.Diff(tailscaled.HTTPProbe("/readyz", tailscaled.HealthCheckPort, &tsapi.Probe{FailureThreshold: 6}, 1), c.ReadinessProbe); diff != "" {
|
||||
t.Errorf("readiness probe mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("unset", func(t *testing.T) {
|
||||
pc := &tsapi.ProxyClass{Spec: tsapi.ProxyClassSpec{StatefulSet: &tsapi.StatefulSet{Pod: &tsapi.Pod{TailscaleContainer: &tsapi.Container{}}}}}
|
||||
c := tailscaled.ApplyProxyClass(relay(), pc, nil, nil).Spec.Template.Spec.Containers[0]
|
||||
if c.LivenessProbe != nil || c.ReadinessProbe != nil {
|
||||
t.Errorf("got probes %v, %v; want none", c.LivenessProbe, c.ReadinessProbe)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("endpoints-off", func(t *testing.T) {
|
||||
pc := withProbes(tsapi.Env{Name: "TS_ENABLE_HEALTH_CHECK", Value: "false"})
|
||||
c := tailscaled.ApplyProxyClass(relay(), pc, nil, nil).Spec.Template.Spec.Containers[0]
|
||||
if c.LivenessProbe != nil || c.ReadinessProbe != nil {
|
||||
t.Errorf("got probes %v, %v; want none when the env turns /livez and /readyz off", c.LivenessProbe, c.ReadinessProbe)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user