From 3312a475ed7c3ec63020fb083ca7c637513baa5b Mon Sep 17 00:00:00 2001 From: David Bond Date: Mon, 23 Nov 2026 04:23:36 +0000 Subject: [PATCH] cmd/tailscale/cli: add --stdout flag to configure kubeconfig GUI builds of the macOS client run in a sandbox that can only reach ~/.kube/config, so "tailscale configure kubeconfig" fails for users who keep several kubeconfigs, or keep one somewhere else. Add a --stdout flag that prints a complete, fresh kubeconfig for the target instead of updating the kubeconfig file, so the shell can redirect it anywhere. It reads and writes no files. Without the flag, behaviour is unchanged. New tests cover both modes. RELNOTE: tailscale configure kubeconfig can print a kubeconfig with --stdout Fixes #18464 Change-Id: I043c07f83f2a2df8e44cb3c956227de5d2f0ade3 Signed-off-by: David Bond --- cmd/tailscale/cli/configure-kube.go | 40 ++++++-- cmd/tailscale/cli/configure-kube_test.go | 115 +++++++++++++++++++++++ 2 files changed, 147 insertions(+), 8 deletions(-) diff --git a/cmd/tailscale/cli/configure-kube.go b/cmd/tailscale/cli/configure-kube.go index 89c036ef7..52d8d5e57 100644 --- a/cmd/tailscale/cli/configure-kube.go +++ b/cmd/tailscale/cli/configure-kube.go @@ -9,6 +9,7 @@ import ( "errors" "flag" "fmt" + "io" "net/netip" "net/url" "os" @@ -27,7 +28,8 @@ import ( ) var configureKubeconfigArgs struct { - http bool // Use HTTP instead of HTTPS (default) for the auth proxy. + http bool // Use HTTP instead of HTTPS (default) for the auth proxy. + stdout bool // Print a fresh kubeconfig to stdout instead of updating the file. } func configureKubeconfigCmd() *ffcli.Command { @@ -40,11 +42,17 @@ Run this command to configure kubectl to connect to a Kubernetes cluster over Ta The hostname argument should be set to the Tailscale hostname of the peer running as an auth proxy in the cluster. +To save the kubeconfig to a separate file, print it with --stdout and redirect the output, then point KUBECONFIG or 'kubectl --kubeconfig' at the file: + + $ tailscale configure kubeconfig --stdout my-cluster > my-cluster.yaml + $ kubectl --kubeconfig my-cluster.yaml get pods + See: https://tailscale.com/s/k8s-auth-proxy `), FlagSet: (func() *flag.FlagSet { fs := newFlagSet("kubeconfig") fs.BoolVar(&configureKubeconfigArgs.http, "http", false, "use HTTP instead of HTTPS to connect to the auth proxy. Ignored if you include a scheme in the hostname argument.") + fs.BoolVar(&configureKubeconfigArgs.stdout, "stdout", false, "print a complete kubeconfig to stdout instead of updating your kubeconfig file. No files are read or written.") return fs })(), Exec: runConfigureKubeconfig, @@ -136,7 +144,7 @@ func isWritable(path string) error { // guidance when the process is sandboxed. func kubeconfigAccessErr(path string, err error) error { if version.IsSandboxedMacOS() { - return fmt.Errorf("cannot write kubeconfig at %q: %w; GUI builds of the macOS client run in a sandbox and can only access files under your home directory, use the open-source tailscaled distribution for other locations", path, err) + return fmt.Errorf("cannot write kubeconfig at %q: %w; GUI builds of the macOS client run in a sandbox and can only access files under your home directory, use the open-source tailscaled distribution for other locations, or use --stdout to print the kubeconfig instead", path, err) } return fmt.Errorf("cannot write kubeconfig at %q: %w", path, err) } @@ -167,18 +175,34 @@ func runConfigureKubeconfig(ctx context.Context, args []string) error { return err } targetFQDN = strings.TrimSuffix(targetFQDN, ".") - kubeconfig := kubeconfigPath() - if err := checkKubeconfigWritable(kubeconfig); err != nil { - return err - } scheme := "https://" if http { scheme = "http://" } - if err = setKubeconfigForPeer(scheme, targetFQDN, kubeconfig); err != nil { + return writeKubeconfig(Stdout, configureKubeconfigArgs.stdout, scheme, targetFQDN) +} + +// writeKubeconfig configures kubectl to use the auth proxy at scheme+fqdn. +// If toStdout is true, it writes a fresh kubeconfig to w and does not touch +// the filesystem. Otherwise, it updates the current user's kubeconfig file and +// reports that to w. +func writeKubeconfig(w io.Writer, toStdout bool, scheme, fqdn string) error { + if toStdout { + b, err := updateKubeconfig(nil, scheme, fqdn) + if err != nil { + return err + } + _, err = w.Write(b) return err } - printf("kubeconfig configured for %q at URL %q\n", targetFQDN, scheme+targetFQDN) + kubeconfig := kubeconfigPath() + if err := checkKubeconfigWritable(kubeconfig); err != nil { + return err + } + if err := setKubeconfigForPeer(scheme, fqdn, kubeconfig); err != nil { + return err + } + fmt.Fprintf(w, "kubeconfig configured for %q at URL %q\n", fqdn, scheme+fqdn) return nil } diff --git a/cmd/tailscale/cli/configure-kube_test.go b/cmd/tailscale/cli/configure-kube_test.go index bc6fb865b..d5f756bd8 100644 --- a/cmd/tailscale/cli/configure-kube_test.go +++ b/cmd/tailscale/cli/configure-kube_test.go @@ -255,6 +255,121 @@ users: } } +func TestWriteKubeconfig(t *testing.T) { + const fqdn = "foo.tail-scale.ts.net" + const otherCluster = `apiVersion: v1 +clusters: +- cluster: + server: https://192.168.1.1:8443 + name: some-cluster +contexts: +- context: + cluster: some-cluster + user: some-auth + name: some-cluster +current-context: some-cluster +kind: Config +users: +- name: some-auth + user: + token: asdfasdf +` + kubeconfig := func(in, scheme string) string { + b, err := updateKubeconfig([]byte(in), scheme, fqdn) + if err != nil { + t.Fatal(err) + } + return string(b) + } + configured := func(scheme string) string { + return fmt.Sprintf("kubeconfig configured for %q at URL %q\n", fqdn, scheme+fqdn) + } + + tests := []struct { + name string + stdout bool + http bool + existing string // contents of $KUBECONFIG before; empty if it doesn't exist + wantOut string + wantFile string // contents of $KUBECONFIG after; empty if it mustn't exist + }{ + { + name: "stdout", + stdout: true, + wantOut: kubeconfig("", "https://"), + }, + { + name: "stdout_http", + stdout: true, + http: true, + wantOut: kubeconfig("", "http://"), + }, + { + name: "stdout_existing_file", + stdout: true, + existing: otherCluster, + wantOut: kubeconfig("", "https://"), + wantFile: otherCluster, + }, + { + name: "file", + wantOut: configured("https://"), + wantFile: kubeconfig("", "https://"), + }, + { + name: "file_existing", + http: true, + existing: otherCluster, + wantOut: configured("http://"), + wantFile: kubeconfig(otherCluster, "http://"), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + scheme := "https://" + if tt.http { + scheme = "http://" + } + path := filepath.Join(t.TempDir(), ".kube", "config") + if tt.existing != "" { + if err := os.Mkdir(filepath.Dir(path), 0755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(tt.existing), 0600); err != nil { + t.Fatal(err) + } + } + t.Setenv("KUBECONFIG", path) + + var out bytes.Buffer + if err := writeKubeconfig(&out, tt.stdout, scheme, fqdn); err != nil { + t.Fatalf("writeKubeconfig() error = %v", err) + } + if d := cmp.Diff(tt.wantOut, out.String()); d != "" { + t.Errorf("output mismatch (-want +got):\n%s", d) + } + if !strings.HasSuffix(out.String(), "\n") { + t.Errorf("output %q does not end with a newline", out.String()) + } + + if tt.wantFile == "" { + // Neither the file nor its .kube directory should be created. + if _, err := os.Stat(filepath.Dir(path)); !os.IsNotExist(err) { + t.Errorf("os.Stat(%q) error = %v, want not exist", filepath.Dir(path), err) + } + return + } + got, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + if d := cmp.Diff(tt.wantFile, string(got)); d != "" { + t.Errorf("kubeconfig file mismatch (-want +got):\n%s", d) + } + }) + } +} + func TestCheckKubeconfigWritable(t *testing.T) { t.Run("nonexistent-file-in-writable-dir", func(t *testing.T) { dir := t.TempDir()