Files
tailscale/k8s-operator/reconciler/peerrelay/statefulset.go
chaosinthecrd 9eea2c890c cmd/k8s-operator,k8s-operator: support ProxyClass metrics for PeerRelay
The PeerRelay reconciler ignored a ProxyClass's spec.metrics, so enabling metrics created no metrics Service or ServiceMonitor and the relay Pods never served /metrics. This fixes this by setting TS_ENABLE_METRICS in ApplyProxyClass and calling tailscaled.EnsureMetricsResources for PeerRelay. PeerRelay now also waits for its ProxyClass to be ready before creating anything, as the other proxies do, so the existing ProxyClass test fixture is marked ready.

ApplyProxyClass now replaces an env var that the ProxyClass also sets instead of appending a second one. The PeerRelay StatefulSet is applied with server-side apply, which rejects duplicate env names, so a ProxyClass that sets an env var the operator also sets, such as TS_ENABLE_METRICS, would stop the StatefulSet from being applied.

Updates #22034

Signed-off-by: chaosinthecrd <[email protected]>
2026-11-05 17:22:05 +00:00

128 lines
3.8 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
//go:build !plan9
package peerrelay
import (
"context"
"fmt"
"net/netip"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"tailscale.com/ipn"
tsapi "tailscale.com/k8s-operator/apis/v1alpha1"
"tailscale.com/k8s-operator/reconciler"
"tailscale.com/k8s-operator/reconciler/tailscaled"
"tailscale.com/kube/kubetypes"
)
func configSecretName(prName string, idx int32) string {
return replicaName(prName, idx) + "-config"
}
func peerRelayHostname(pr *tsapi.PeerRelay, idx int32) string {
prefix := string(pr.Spec.HostnamePrefix)
if prefix == "" {
prefix = pr.Name
}
return fmt.Sprintf("%s-%d", prefix, idx)
}
func peerRelayTailscaledConfig(pr *tsapi.PeerRelay, idx int32, endpoints []tsapi.PeerRelayEndpoint, authKey *string, loginServer string) ipn.ConfigVAlpha {
conf := ipn.ConfigVAlpha{
Version: "alpha0",
AcceptDNS: "false",
AcceptRoutes: "false",
Locked: "false",
Hostname: new(peerRelayHostname(pr, idx)),
RelayServerPort: new(peerRelayPort(pr)),
AuthKey: authKey,
}
if loginServer != "" {
conf.ServerURL = &loginServer
}
for _, endpoint := range endpoints {
addr, err := netip.ParseAddr(endpoint.Address)
if err != nil {
continue
}
conf.RelayServerStaticEndpoints = append(conf.RelayServerStaticEndpoints, netip.AddrPortFrom(addr, uint16(endpoint.Port)))
}
return conf
}
func (r *Reconciler) peerRelayConfigSecret(pr *tsapi.PeerRelay, idx int32, endpoints []tsapi.PeerRelayEndpoint, authKey *string, loginServer string) (*corev1.Secret, error) {
labels := peerRelayServiceLabels(pr.Name, idx)
return tailscaled.NewConfigSecret(tailscaled.ConfigSecretOptions{
Name: configSecretName(pr.Name, idx),
Namespace: r.tailscaleNamespace,
Labels: labels,
Config: peerRelayTailscaledConfig(pr, idx, endpoints, authKey, loginServer),
})
}
func (r *Reconciler) peerRelayStatefulSet(pr *tsapi.PeerRelay, replicas int32, pc *tsapi.ProxyClass) *appsv1.StatefulSet {
labels := peerRelayLabels(pr.Name)
ss := tailscaled.NewStatefulSet(tailscaled.StatefulSetOptions{
Name: resourceName(pr.Name),
Namespace: r.tailscaleNamespace,
Labels: labels,
Image: r.proxyImage,
Replicas: replicas,
ServiceAccountName: "proxies",
ConfigSecretNameFunc: func(idx int32) string {
return configSecretName(pr.Name, idx)
},
})
// The relay server force-sets its UDP socket buffer sizes, which requires NET_ADMIN. Without it, tailscaled falls
// back to the kernel defaults and relay throughput suffers. A ProxyClass security context replaces this entirely.
ss.Spec.Template.Spec.Containers[0].SecurityContext = &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Add: []corev1.Capability{"NET_ADMIN"},
},
}
return tailscaled.ApplyProxyClass(ss, pc, managedLabelKeys, nil)
}
var managedLabelKeys = []string{
kubetypes.LabelManaged,
reconciler.LabelParentType,
reconciler.LabelParentName,
}
func (r *Reconciler) metricsOptions(pr *tsapi.PeerRelay) tailscaled.MetricsOptions {
return tailscaled.MetricsOptions{
StatefulSetName: resourceName(pr.Name),
Namespace: r.tailscaleNamespace,
PodLabels: peerRelayLabels(pr.Name),
ProxyType: parentTypePeerRelay,
ParentName: pr.Name,
// Lets the Service watch enqueue this PeerRelay when its metrics Service changes.
Labels: peerRelayLabels(pr.Name),
}
}
func (r *Reconciler) getProxyClass(ctx context.Context, pr *tsapi.PeerRelay) (*tsapi.ProxyClass, error) {
if pr.Spec.ProxyClass == "" {
return nil, nil
}
var pc tsapi.ProxyClass
if err := r.Get(ctx, types.NamespacedName{Name: pr.Spec.ProxyClass}, &pc); err != nil {
return nil, fmt.Errorf("failed to get ProxyClass %q: %w", pr.Spec.ProxyClass, err)
}
return &pc, nil
}