diff --git a/cmd/tailscale/cli/blueprint_render.go b/cmd/tailscale/cli/blueprint_render.go new file mode 100644 index 000000000..c9af1c7a6 --- /dev/null +++ b/cmd/tailscale/cli/blueprint_render.go @@ -0,0 +1,64 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package cli + +import ( + "fmt" + "io" + "net/netip" + "strings" + + "tailscale.com/tailcfg" +) + +// renderBlueprintConfig writes a human-readable description of cfg to +// w. The output is shared by `tailscale join`, `tailscale leave`, and +// `tailscale join status`; the shape is intentionally identical across +// the three so operators learn one layout. Empty buckets are omitted +// entirely (no "Apps: (none)" lines). +// +// cfg may be nil. A nil cfg indicates the local node is blueprint-bound +// (the caller has a known id) but the projection has not yet arrived +// via map poll; the renderer prints a single explanatory line in that +// case. +func renderBlueprintConfig(w io.Writer, id string, cfg *tailcfg.BlueprintConfig) { + fmt.Fprintf(w, "Blueprint: bp:%s\n", id) + if cfg == nil { + fmt.Fprintln(w, " (projection not yet received)") + return + } + writeBucket(w, "Tags", cfg.Tags) + writeBucketPrefixes(w, "Routes", cfg.Routes) + writeBucket(w, "Apps", cfg.ServeApps) + writeBucket(w, "Services", cfg.ServeServices) + writeBucket(w, "IPSets", cfg.ServeIPSets) + writeBucket(w, "Attrs", cfg.Attrs) + writeBucket(w, "Prefs", cfg.Prefs) +} + +// blueprintLabelWidth is the field-width passed to %-*s when writing a +// bucket line; the value column starts at column 11 (2 spaces + a +// label of up to 9 chars including its trailing colon + at least 1 +// trailing space). "Services:" at 9 chars is the widest label, so this +// width keeps every value column aligned. Changing it shifts every +// value column. +const blueprintLabelWidth = 11 + +func writeBucket(w io.Writer, label string, values []string) { + if len(values) == 0 { + return + } + fmt.Fprintf(w, " %-*s%s\n", blueprintLabelWidth, label+":", strings.Join(values, ", ")) +} + +func writeBucketPrefixes(w io.Writer, label string, values []netip.Prefix) { + if len(values) == 0 { + return + } + ss := make([]string, len(values)) + for i, v := range values { + ss[i] = v.String() + } + fmt.Fprintf(w, " %-*s%s\n", blueprintLabelWidth, label+":", strings.Join(ss, ", ")) +} diff --git a/cmd/tailscale/cli/blueprint_render_test.go b/cmd/tailscale/cli/blueprint_render_test.go new file mode 100644 index 000000000..fa373438e --- /dev/null +++ b/cmd/tailscale/cli/blueprint_render_test.go @@ -0,0 +1,72 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package cli + +import ( + "net/netip" + "strings" + "testing" + + "tailscale.com/tailcfg" +) + +func TestRenderBlueprintConfig_NilCfg(t *testing.T) { + var sb strings.Builder + renderBlueprintConfig(&sb, "github-connector", nil) + got := sb.String() + want := "Blueprint: bp:github-connector\n (projection not yet received)\n" + if got != want { + t.Errorf("got:\n%q\nwant:\n%q", got, want) + } +} + +func TestRenderBlueprintConfig_AllBuckets(t *testing.T) { + cfg := &tailcfg.BlueprintConfig{ + Tags: []string{"tag:bp//github-connector", "tag:prod"}, + Routes: []netip.Prefix{netip.MustParsePrefix("10.0.0.0/24"), netip.MustParsePrefix("10.0.1.0/24")}, + ServeApps: []string{"app:github"}, + ServeServices: []string{"svc:webhook"}, + ServeIPSets: []string{"ipset:corp-internal"}, + Attrs: []string{"nodeAttr:funnel"}, + Prefs: []string{"pref:ssh", "pref:accept-routes"}, + } + var sb strings.Builder + renderBlueprintConfig(&sb, "github-connector", cfg) + got := sb.String() + want := "Blueprint: bp:github-connector\n" + + " Tags: tag:bp//github-connector, tag:prod\n" + + " Routes: 10.0.0.0/24, 10.0.1.0/24\n" + + " Apps: app:github\n" + + " Services: svc:webhook\n" + + " IPSets: ipset:corp-internal\n" + + " Attrs: nodeAttr:funnel\n" + + " Prefs: pref:ssh, pref:accept-routes\n" + if got != want { + t.Errorf("got:\n%s\nwant:\n%s", got, want) + } +} + +func TestRenderBlueprintConfig_EmptyBucketsOmitted(t *testing.T) { + cfg := &tailcfg.BlueprintConfig{ + Tags: []string{"tag:bp//foo"}, + } + var sb strings.Builder + renderBlueprintConfig(&sb, "foo", cfg) + got := sb.String() + want := "Blueprint: bp:foo\n Tags: tag:bp//foo\n" + if got != want { + t.Errorf("got:\n%q\nwant:\n%q", got, want) + } +} + +func TestRenderBlueprintConfig_AllEmptyBuckets(t *testing.T) { + cfg := &tailcfg.BlueprintConfig{} + var sb strings.Builder + renderBlueprintConfig(&sb, "foo", cfg) + got := sb.String() + want := "Blueprint: bp:foo\n" + if got != want { + t.Errorf("got:\n%q\nwant:\n%q", got, want) + } +}