Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions tool/tsh/common/kube_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"crypto/tls"
"encoding/pem"
"fmt"
"io"
"net"
"os"
"os/exec"
Expand Down Expand Up @@ -62,6 +63,7 @@ type proxyKubeCommand struct {

labels string
predicateExpression string
exec bool
}

func newProxyKubeCommand(parent *kingpin.CmdClause) *proxyKubeCommand {
Expand All @@ -84,6 +86,7 @@ func newProxyKubeCommand(parent *kingpin.CmdClause) *proxyKubeCommand {
// This works as an hint to the user that the context name can be customized.
Default(kubeconfig.ContextName("{{.ClusterName}}", "{{.KubeName}}")).
StringVar(&c.overrideContextName)
c.Flag("exec", "Run the proxy in the background and reexec into a new shell with $KUBECONFIG already pointed to our config file.").BoolVar(&c.exec)
return c
}

Expand All @@ -103,6 +106,7 @@ func (c *proxyKubeCommand) run(cf *CLIConf) error {
if err != nil {
return trace.Wrap(err)
}

if cf.Headless {
tc.AllowHeadless = true
}
Expand All @@ -118,7 +122,10 @@ func (c *proxyKubeCommand) run(cf *CLIConf) error {
}
defer localProxy.Close()

if err := c.printTemplate(cf, localProxy); err != nil {
// re-exec into a new shell with $KUBECONFIG already pointed to our config file
// if --exec flag is set or headless mode is enabled.
reexecIntoShell := cf.Headless || c.exec
if err := c.printTemplate(cf.Stdout(), reexecIntoShell, localProxy); err != nil {
return trace.Wrap(err)
}

Expand All @@ -135,7 +142,7 @@ func (c *proxyKubeCommand) run(cf *CLIConf) error {
return trace.Wrap(cf.RunCommand(cmd))
}

if cf.Headless {
if reexecIntoShell {
// If headless, run proxy in the background and reexec into a new shell with $KUBECONFIG already pointed to
// our config file
return trace.Wrap(runHeadlessKubeProxy(cf, localProxy))
Expand Down Expand Up @@ -261,13 +268,13 @@ func (c *proxyKubeCommand) printPrepare(cf *CLIConf, title string, clusters kube
fmt.Fprintln(cf.Stdout(), table.AsBuffer().String())
}

func (c *proxyKubeCommand) printTemplate(cf *CLIConf, localProxy *kubeLocalProxy) error {
if cf.Headless {
return trace.Wrap(proxyKubeHeadlessTemplate.Execute(cf.Stdout(), map[string]interface{}{
func (c *proxyKubeCommand) printTemplate(w io.Writer, isReexec bool, localProxy *kubeLocalProxy) error {
if isReexec {
return trace.Wrap(proxyKubeHeadlessTemplate.Execute(w, map[string]interface{}{
"multipleContexts": len(localProxy.kubeconfig.Contexts) > 1,
}))
}
return trace.Wrap(proxyKubeTemplate.Execute(cf.Stdout(), map[string]interface{}{
return trace.Wrap(proxyKubeTemplate.Execute(w, map[string]interface{}{
"addr": localProxy.GetAddr(),
"format": c.format,
"randomPort": c.port == "",
Expand Down