Skip to content

Commit

Permalink
[v14] [kube] Enhance tsh proxy kube output table with kubeconfig co…
Browse files Browse the repository at this point in the history
…ntext name (#47381)

* [kube] Enhance `tsh proxy kube` output table with kubeconfig context name

This PR introduces a third column to the table shown by `tsh proxy kube` to include the information about the context name for the specified kubernetes clusters.
Signed-off-by: Tiago Silva <[email protected]>

* Update kube_proxy.go

Co-authored-by: Noah Stride <[email protected]>

---------

Co-authored-by: Noah Stride <[email protected]>
  • Loading branch information
tigrato and strideynet authored Oct 9, 2024
1 parent 6c27b85 commit f353b52
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
13 changes: 13 additions & 0 deletions lib/kube/kubeconfig/context_overwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,16 @@ func executeKubeContextTemplate(tmpl *template.Template, clusterName, kubeName s
err := tmpl.Execute(&buf, contextEntry)
return buf.String(), trace.Wrap(err)
}

// ContextNameFromTemplate generates a kubernetes context name from the given template.
func ContextNameFromTemplate(temp string, clusterName, kubeName string) (string, error) {
tmpl, err := parseContextOverrideTemplate(temp)
if err != nil {
return "", trace.Wrap(err)
}
if tmpl == nil {
return ContextName(clusterName, kubeName), nil
}
s, err := executeKubeContextTemplate(tmpl, clusterName, kubeName)
return s, trace.Wrap(err)
}
10 changes: 8 additions & 2 deletions tool/tsh/common/kube_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/pem"
"fmt"
"io"
"log/slog"
"net"
"os"
"os/exec"
Expand Down Expand Up @@ -258,9 +259,14 @@ func (c *proxyKubeCommand) prepare(cf *CLIConf, tc *client.TeleportClient) (*cli

func (c *proxyKubeCommand) printPrepare(cf *CLIConf, title string, clusters kubeconfig.LocalProxyClusters) {
fmt.Fprintln(cf.Stdout(), title)
table := asciitable.MakeTable([]string{"Teleport Cluster Name", "Kube Cluster Name"})
table := asciitable.MakeTable([]string{"Teleport Cluster Name", "Kube Cluster Name", "Context Name"})
for _, cluster := range clusters {
table.AddRow([]string{cluster.TeleportCluster, cluster.KubeCluster})
contextName, err := kubeconfig.ContextNameFromTemplate(c.overrideContextName, cluster.TeleportCluster, cluster.KubeCluster)
if err != nil {
slog.WarnContext(cf.Context, "Failed to generate context name.", "error", err)
contextName = kubeconfig.ContextName(cluster.TeleportCluster, cluster.KubeCluster)
}
table.AddRow([]string{cluster.TeleportCluster, cluster.KubeCluster, contextName})
}
fmt.Fprintln(cf.Stdout(), table.AsBuffer().String())
}
Expand Down

0 comments on commit f353b52

Please sign in to comment.