Files
tailscale/cmd/k8s-operator/metrics_resources.go
chaosinthecrd 31652c9978 cmd/k8s-operator,k8s-operator/reconciler: move metrics handling to tailscaled
The metrics Service and ServiceMonitor handling lived in cmd/k8s-operator,
and the ProxyClass reconciler kept its own copy of the ServiceMonitor CRD
check, so reconcilers in k8s-operator/reconciler could not share it. This
moves it into the tailscaled package as EnsureMetricsResources and
EnsureMetricsResourcesDeleted, which take the proxy type and parent, and
switches cmd/k8s-operator and the ProxyClass reconciler over to it. No
behaviour change.

Updates #22034

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

56 lines
1.8 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
//go:build !plan9
package main
import (
"context"
"go.uber.org/zap"
"sigs.k8s.io/controller-runtime/pkg/client"
tsapi "tailscale.com/k8s-operator/apis/v1alpha1"
"tailscale.com/k8s-operator/reconciler/tailscaled"
)
func reconcileMetricsResources(ctx context.Context, logger *zap.SugaredLogger, opts *metricsOpts, pc *tsapi.ProxyClass, cl client.Client) error {
if opts.proxyType == proxyTypeEgress {
// Metrics are currently not being enabled for standalone egress proxies.
return nil
}
return tailscaled.EnsureMetricsResources(ctx, cl, logger, opts.metricsOptions(), pc)
}
// maybeCleanupMetricsResources ensures that any metrics resources created for a proxy are deleted.
func maybeCleanupMetricsResources(ctx context.Context, opts *metricsOpts, cl client.Client) error {
return tailscaled.EnsureMetricsResourcesDeleted(ctx, cl, opts.metricsOptions())
}
type metricsOpts struct {
proxyStsName string // name of StatefulSet for proxy
tsNamespace string // namespace in which Tailscale is installed
proxyLabels map[string]string // labels of the proxy StatefulSet
proxyType string
}
// metricsOptions converts opts for the tailscaled metrics helpers.
func (opts *metricsOpts) metricsOptions() tailscaled.MetricsOptions {
mo := tailscaled.MetricsOptions{
StatefulSetName: opts.proxyStsName,
Namespace: opts.tsNamespace,
PodLabels: opts.proxyLabels,
ProxyType: opts.proxyType,
ParentName: opts.proxyLabels[LabelParentName],
}
if isNamespacedProxyType(opts.proxyType) {
mo.ParentNamespace = opts.proxyLabels[LabelParentNamespace]
}
return mo
}
func isNamespacedProxyType(typ string) bool {
return typ == proxyTypeIngressResource || typ == proxyTypeIngressService
}