From 4e54f7c5bbcc6c8b125b722266fc0418f8a574bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Castillo=20Gonz=C3=A1lez?= <82875882+pcbeingused33@users.noreply.github.com> Date: Tue, 1 Dec 2026 10:17:37 -0500 Subject: [PATCH] k8s-operator/reconciler/recorder: requeue Recorders when their Tailnet changes (#22507) * k8s-operator/reconciler/recorder: requeue Recorders when their Tailnet changes A Recorder whose spec.tailnet refers to a Tailnet that is not ready yet is marked RecorderReady=False and not requeued. Watch Tailnet resources and enqueue the Recorders that refer to the changed Tailnet. Fixes #22457 Signed-off-by: Alex Castillo * k8s-operator/reconciler/recorder: requeue when the Tailnet client is not ready The Tailnet reconciler writes Ready and its finalizer before it marks the client ready, and both writes enqueue Recorders. A reconcile in that window still sees the tailnet as unavailable. Return the existing short requeue so that case retries without waiting for another Tailnet event. Move the Tailnet watch mapping test next to the other recorder tests. Updates #22457 Signed-off-by: Alex Castillo * k8s-operator/reconciler/recorder: use FakeClientProvider in the ready test TestRecorderProvisionsWhenTailnetBecomesReady had its own client provider, a copy of reconcilertest.FakeClientProvider. Run two reconcilers on the same fake client instead: the first fails to resolve the tailnet, and the second resolves it with NewFakeClientProvider. Updates #22457 Signed-off-by: Alex Castillo --------- Signed-off-by: Alex Castillo --- .../reconciler/recorder/internal_test.go | 37 ++++++++++++ k8s-operator/reconciler/recorder/recorder.go | 26 +++++++- .../reconciler/recorder/recorder_test.go | 59 ++++++++++++++++++- 3 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 k8s-operator/reconciler/recorder/internal_test.go diff --git a/k8s-operator/reconciler/recorder/internal_test.go b/k8s-operator/reconciler/recorder/internal_test.go new file mode 100644 index 000000000..35796d315 --- /dev/null +++ b/k8s-operator/reconciler/recorder/internal_test.go @@ -0,0 +1,37 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +//go:build !plan9 + +package recorder + +import ( + "testing" + + "go.uber.org/zap" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/controller-runtime/pkg/client" + + tsapi "tailscale.com/k8s-operator/apis/v1alpha1" + "tailscale.com/k8s-operator/reconciler/reconcilertest" +) + +func TestRecordersForTailnet(t *testing.T) { + recorders := []client.Object{ + &tsapi.Recorder{ObjectMeta: metav1.ObjectMeta{Name: "on-a"}, Spec: tsapi.RecorderSpec{Tailnet: "a"}}, + &tsapi.Recorder{ObjectMeta: metav1.ObjectMeta{Name: "on-b"}, Spec: tsapi.RecorderSpec{Tailnet: "b"}}, + &tsapi.Recorder{ObjectMeta: metav1.ObjectMeta{Name: "default"}}, + } + r := &Reconciler{ + Client: reconcilertest.NewClientBuilder().WithObjects(recorders...).Build(), + logger: zap.NewNop().Sugar(), + } + + reqs := r.recordersForTailnet(t.Context(), &tsapi.Tailnet{ObjectMeta: metav1.ObjectMeta{Name: "a"}}) + if len(reqs) != 1 || reqs[0].Name != "on-a" { + t.Fatalf("recordersForTailnet(a) = %v, want only on-a", reqs) + } + if reqs := r.recordersForTailnet(t.Context(), &tsapi.Tailnet{ObjectMeta: metav1.ObjectMeta{Name: "none"}}); len(reqs) != 0 { + t.Fatalf("recordersForTailnet(none) = %v, want none", reqs) + } +} diff --git a/k8s-operator/reconciler/recorder/recorder.go b/k8s-operator/reconciler/recorder/recorder.go index 826ed7371..6e8a30696 100644 --- a/k8s-operator/reconciler/recorder/recorder.go +++ b/k8s-operator/reconciler/recorder/recorder.go @@ -132,9 +132,27 @@ func (r *Reconciler) Register(mgr manager.Manager) error { Watches(&corev1.Secret{}, enqueue). Watches(&rbacv1.Role{}, enqueue). Watches(&rbacv1.RoleBinding{}, enqueue). + Watches(&tsapi.Tailnet{}, handler.EnqueueRequestsFromMapFunc(r.recordersForTailnet)). Complete(r) } +// recordersForTailnet maps a Tailnet event to the Recorders whose spec.tailnet refers to it, so that a Recorder +// that was waiting on a not-yet-ready Tailnet is reconciled once the Tailnet changes. +func (r *Reconciler) recordersForTailnet(ctx context.Context, o client.Object) []reconcile.Request { + var list tsapi.RecorderList + if err := r.List(ctx, &list); err != nil { + r.logger.Errorf("failed to list Recorders for Tailnet %q: %v", o.GetName(), err) + return nil + } + var reqs []reconcile.Request + for _, tsr := range list.Items { + if tsr.Spec.Tailnet != "" && tsr.Spec.Tailnet == o.GetName() { + reqs = append(reqs, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&tsr)}) + } + } + return reqs +} + // Reconcile is invoked when a change occurs to Recorder resources within the cluster. On create/update it ensures a // StatefulSet and the Secrets and RBAC resources each replica needs exist. On delete it removes each replica's // device from the tailnet before releasing the finalizer; the in-cluster resources are garbage collected via their @@ -169,7 +187,13 @@ func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reco tsClient, err := r.clients.For(tsr.Spec.Tailnet) if err != nil { - return setStatusReady(tsr, metav1.ConditionFalse, reasonRecorderTailnetUnavailable, err.Error()) + if _, statusErr := setStatusReady(tsr, metav1.ConditionFalse, reasonRecorderTailnetUnavailable, err.Error()); statusErr != nil { + return reconcile.Result{}, statusErr + } + // The Tailnet reconciler writes Ready and its finalizer before the client + // is marked ready, and both writes enqueue this Recorder. A reconcile in + // that window still fails here; requeue so it does not wait for another event. + return reconcile.Result{RequeueAfter: shortRequeue}, nil } if !tsr.DeletionTimestamp.IsZero() { diff --git a/k8s-operator/reconciler/recorder/recorder_test.go b/k8s-operator/reconciler/recorder/recorder_test.go index 15b5ede59..772d5dbfd 100644 --- a/k8s-operator/reconciler/recorder/recorder_test.go +++ b/k8s-operator/reconciler/recorder/recorder_test.go @@ -12,6 +12,7 @@ import ( "maps" "strings" "testing" + "time" "github.com/google/go-cmp/cmp" "go.uber.org/zap" @@ -22,6 +23,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/tools/record" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/reconcile" tailscaleclient "tailscale.com/client/tailscale/v2" @@ -308,7 +310,9 @@ func TestRecorderTailnetUnavailable(t *testing.T) { Clock: tstest.NewClock(tstest.ClockOpts{}), }) - reconcilertest.ExpectReconciled(t, r, "", tsr.Name) + // 5s is the reconciler's shortRequeue, used as a backstop when the Tailnet + // client is not ready yet. + expectRequeueAfter(t, r, tsr.Name, 5*time.Second) got := &tsapi.Recorder{} reconcilertest.MustGet(t, fc, "", tsr.Name, got) @@ -323,6 +327,59 @@ func TestRecorderTailnetUnavailable(t *testing.T) { } } +func expectRequeueAfter(t *testing.T, r reconcile.Reconciler, name string, want time.Duration) { + t.Helper() + res, err := r.Reconcile(t.Context(), reconcile.Request{NamespacedName: client.ObjectKey{Name: name}}) + if err != nil { + t.Fatalf("Reconcile: unexpected error: %v", err) + } + if res.RequeueAfter != want { + t.Fatalf("RequeueAfter = %v, want %v", res.RequeueAfter, want) + } +} + +func TestRecorderProvisionsWhenTailnetBecomesReady(t *testing.T) { + tsr := &tsapi.Recorder{ + ObjectMeta: metav1.ObjectMeta{Name: "test"}, + Spec: tsapi.RecorderSpec{EnableUI: true, Tailnet: "tn"}, + } + fc := reconcilertest.NewClientBuilder(). + WithObjects(tsr). + WithStatusSubresource(tsr). + Build() + tsClient := reconcilertest.NewFakeClient(reconcilertest.WithLoginURL(tsLoginServer)) + zl, err := zap.NewDevelopment() + if err != nil { + t.Fatal(err) + } + // Two reconcilers share fc. The first cannot resolve the tailnet; the + // second can. That is the window the short requeue covers. + unavailable := recorder.NewReconciler(recorder.ReconcilerOptions{ + Client: fc, + Recorder: record.NewFakeRecorder(1), + TailscaleNamespace: tsNamespace, + Clients: reconcilertest.NewFailingClientProvider(errors.New(`tailnet "tn" not ready`)), + Logger: zl.Sugar(), + Clock: tstest.NewClock(tstest.ClockOpts{}), + }) + + expectRequeueAfter(t, unavailable, tsr.Name, 5*time.Second) + expectReadyCondition(t, fc, tsr.Name, metav1.ConditionFalse, "RecorderTailnetUnavailable", `tailnet "tn" not ready`) + expectRecorderResources(t, fc, tsr, false) + + ready := recorder.NewReconciler(recorder.ReconcilerOptions{ + Client: fc, + Recorder: record.NewFakeRecorder(1), + TailscaleNamespace: tsNamespace, + Clients: reconcilertest.NewFakeClientProvider(tsClient), + Logger: zl.Sugar(), + Clock: tstest.NewClock(tstest.ClockOpts{}), + }) + reconcilertest.ExpectReconciled(t, ready, "", tsr.Name) + expectReadyCondition(t, fc, tsr.Name, metav1.ConditionTrue, "RecorderCreated", "RecorderCreated") + expectRecorderResources(t, fc, tsr, true) +} + func TestRecorderCleanupContinuesPastUnreportedDevice(t *testing.T) { tsr := &tsapi.Recorder{ ObjectMeta: metav1.ObjectMeta{