From f2b02df99ea85f7bee60376b089e99cea1742cdf Mon Sep 17 00:00:00 2001 From: Kabir Sikand Date: Wed, 27 May 2026 19:54:07 +0000 Subject: [PATCH] cmd/tailscale/cli, ipn/ipnstate: add blueprint projection wait helper Adds a pure waitForBlueprintProjection helper that polls a Status fetcher until the daemon reports a matching projection or a deadline elapses. Extracted from runJoin so the wait-loop behavior can be unit tested without a fake LocalClient. Also threads PeerStatus.BlueprintConfig (populated only on Self, from tailcfg.Node.BlueprintConfig) so the helper's success condition can be expressed in terms of the public Status surface. Peer entries never read this field; the helper checks only Status.Self. Updates #cleanup Signed-off-by: Kabir Sikand --- cmd/tailscale/cli/blueprint_wait.go | 52 ++++++++++ cmd/tailscale/cli/blueprint_wait_test.go | 121 +++++++++++++++++++++++ ipn/ipnlocal/local.go | 3 + ipn/ipnstate/ipnstate.go | 8 ++ 4 files changed, 184 insertions(+) create mode 100644 cmd/tailscale/cli/blueprint_wait.go create mode 100644 cmd/tailscale/cli/blueprint_wait_test.go diff --git a/cmd/tailscale/cli/blueprint_wait.go b/cmd/tailscale/cli/blueprint_wait.go new file mode 100644 index 000000000..ef770be8c --- /dev/null +++ b/cmd/tailscale/cli/blueprint_wait.go @@ -0,0 +1,52 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package cli + +import ( + "context" + "errors" + "time" + + "tailscale.com/ipn" + "tailscale.com/ipn/ipnstate" + "tailscale.com/tailcfg" +) + +// errProjectionTimeout is returned by waitForBlueprintProjection when +// the deadline passes without the daemon reporting a matching +// projection. The caller treats this as success-of-binding + +// failure-of-display: the underlying join itself is unaffected. +var errProjectionTimeout = errors.New("blueprint projection not received before deadline") + +// waitForBlueprintProjection polls fetch every interval until the +// daemon reports the node is Running with Self.BlueprintID == wantID +// and Self.BlueprintConfig != nil, or until timeout elapses, or until +// ctx is canceled. +// +// fetch errors are treated as transient: the function keeps polling +// until success or timeout. ctx cancellation propagates out +// immediately. +// +// This function exists separate from runJoin so the wait-loop +// behavior can be tested without a fake LocalClient. +func waitForBlueprintProjection(ctx context.Context, fetch func(context.Context) (*ipnstate.Status, error), wantID string, interval, timeout time.Duration) (*tailcfg.BlueprintConfig, error) { + deadline := time.Now().Add(timeout) + tick := time.NewTicker(interval) + defer tick.Stop() + for { + st, err := fetch(ctx) + if err == nil && st != nil && st.BackendState == ipn.Running.String() && + st.Self != nil && st.Self.BlueprintID == wantID && st.Self.BlueprintConfig != nil { + return st.Self.BlueprintConfig, nil + } + if time.Now().After(deadline) { + return nil, errProjectionTimeout + } + select { + case <-ctx.Done(): + return nil, ctx.Err() + case <-tick.C: + } + } +} diff --git a/cmd/tailscale/cli/blueprint_wait_test.go b/cmd/tailscale/cli/blueprint_wait_test.go new file mode 100644 index 000000000..ead80a6a8 --- /dev/null +++ b/cmd/tailscale/cli/blueprint_wait_test.go @@ -0,0 +1,121 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package cli + +import ( + "context" + "errors" + "testing" + "time" + + "tailscale.com/ipn" + "tailscale.com/ipn/ipnstate" + "tailscale.com/tailcfg" +) + +func TestWaitForBlueprintProjection_Success(t *testing.T) { + calls := 0 + want := &tailcfg.BlueprintConfig{Tags: []string{"tag:bp//foo"}} + fetch := func(ctx context.Context) (*ipnstate.Status, error) { + calls++ + if calls < 3 { + return &ipnstate.Status{ + BackendState: ipn.Starting.String(), + Self: &ipnstate.PeerStatus{}, + }, nil + } + return &ipnstate.Status{ + BackendState: ipn.Running.String(), + Self: &ipnstate.PeerStatus{ + BlueprintID: "foo", + BlueprintConfig: want, + }, + }, nil + } + + got, err := waitForBlueprintProjection(context.Background(), fetch, "foo", 1*time.Millisecond, 1*time.Second) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if got != want { + t.Errorf("returned cfg = %p; want %p", got, want) + } + if calls < 3 { + t.Errorf("expected at least 3 fetch calls; got %d", calls) + } +} + +func TestWaitForBlueprintProjection_Timeout(t *testing.T) { + fetch := func(ctx context.Context) (*ipnstate.Status, error) { + // Never advances past Starting. + return &ipnstate.Status{ + BackendState: ipn.Starting.String(), + Self: &ipnstate.PeerStatus{}, + }, nil + } + _, err := waitForBlueprintProjection(context.Background(), fetch, "foo", 1*time.Millisecond, 10*time.Millisecond) + if !errors.Is(err, errProjectionTimeout) { + t.Errorf("err = %v; want errProjectionTimeout", err) + } +} + +func TestWaitForBlueprintProjection_MismatchedID(t *testing.T) { + // Status is Running with a projection, but for a DIFFERENT blueprint + // (stale netmap during a re-join). The helper must keep polling. + calls := 0 + fetch := func(ctx context.Context) (*ipnstate.Status, error) { + calls++ + return &ipnstate.Status{ + BackendState: ipn.Running.String(), + Self: &ipnstate.PeerStatus{ + BlueprintID: "stale", + BlueprintConfig: &tailcfg.BlueprintConfig{}, + }, + }, nil + } + _, err := waitForBlueprintProjection(context.Background(), fetch, "fresh", 1*time.Millisecond, 5*time.Millisecond) + if !errors.Is(err, errProjectionTimeout) { + t.Errorf("err = %v; want errProjectionTimeout (mismatched id should not declare success)", err) + } + if calls < 2 { + t.Errorf("expected multiple polls; got %d", calls) + } +} + +func TestWaitForBlueprintProjection_FetchError(t *testing.T) { + // A status fetch error mid-loop is transient; keep polling until timeout. + calls := 0 + fetch := func(ctx context.Context) (*ipnstate.Status, error) { + calls++ + if calls < 3 { + return nil, errors.New("daemon not ready") + } + return &ipnstate.Status{ + BackendState: ipn.Running.String(), + Self: &ipnstate.PeerStatus{ + BlueprintID: "foo", + BlueprintConfig: &tailcfg.BlueprintConfig{}, + }, + }, nil + } + cfg, err := waitForBlueprintProjection(context.Background(), fetch, "foo", 1*time.Millisecond, 1*time.Second) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if cfg == nil { + t.Error("cfg = nil; want non-nil") + } +} + +func TestWaitForBlueprintProjection_ContextCanceled(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + fetch := func(ctx context.Context) (*ipnstate.Status, error) { + return &ipnstate.Status{BackendState: ipn.Starting.String(), Self: &ipnstate.PeerStatus{}}, nil + } + _, err := waitForBlueprintProjection(ctx, fetch, "foo", 1*time.Millisecond, 1*time.Second) + if !errors.Is(err, context.Canceled) { + t.Errorf("err = %v; want context.Canceled", err) + } +} diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 0f3a58d7e..1c3adc53c 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -1500,6 +1500,9 @@ func peerStatusFromNode(ps *ipnstate.PeerStatus, n tailcfg.NodeView) { ps.PublicKey = n.Key() ps.ID = n.StableID() ps.BlueprintID = n.BlueprintID() + if bc := n.BlueprintConfig(); bc.Valid() { + ps.BlueprintConfig = bc.AsStruct() + } ps.Created = n.Created() ps.ExitNodeOption = buildfeatures.HasUseExitNode && tsaddr.ContainsExitRoutes(n.AllowedIPs()) if n.Tags().Len() != 0 { diff --git a/ipn/ipnstate/ipnstate.go b/ipn/ipnstate/ipnstate.go index df5e059bf..de630fb72 100644 --- a/ipn/ipnstate/ipnstate.go +++ b/ipn/ipnstate/ipnstate.go @@ -252,6 +252,11 @@ type PeerStatus struct { // tailnet's policy. BlueprintID string `json:",omitempty"` + // BlueprintConfig, if non-nil, is the local node's projected + // Blueprint configuration. Populated only on Self (from + // tailcfg.Node.BlueprintConfig); always nil on peer entries. + BlueprintConfig *tailcfg.BlueprintConfig `json:",omitempty"` + // PrimaryRoutes are the routes this node is currently the primary // subnet router for, as determined by the control plane. It does // not include the IPs in TailscaleIPs. @@ -456,6 +461,9 @@ func (sb *StatusBuilder) AddPeer(peer key.NodePublic, st *PeerStatus) { if v := st.BlueprintID; v != "" { e.BlueprintID = v } + if v := st.BlueprintConfig; v != nil { + e.BlueprintConfig = v + } if v := st.DNSName; v != "" { e.DNSName = v }