From f353b52c6075ea72a360ff22a053022c983c987b Mon Sep 17 00:00:00 2001 From: Tiago Silva Date: Wed, 9 Oct 2024 12:52:30 +0100 Subject: [PATCH] [v14] [kube] Enhance `tsh proxy kube` output table with kubeconfig context 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 * Update kube_proxy.go Co-authored-by: Noah Stride --------- Co-authored-by: Noah Stride --- lib/kube/kubeconfig/context_overwrite.go | 13 +++++++++++++ tool/tsh/common/kube_proxy.go | 10 ++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/kube/kubeconfig/context_overwrite.go b/lib/kube/kubeconfig/context_overwrite.go index 4351a7e8ff326..59cd4bba7ae40 100644 --- a/lib/kube/kubeconfig/context_overwrite.go +++ b/lib/kube/kubeconfig/context_overwrite.go @@ -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) +} diff --git a/tool/tsh/common/kube_proxy.go b/tool/tsh/common/kube_proxy.go index 1fe163db1ff4e..9b7b6d3fd7be8 100644 --- a/tool/tsh/common/kube_proxy.go +++ b/tool/tsh/common/kube_proxy.go @@ -22,6 +22,7 @@ import ( "encoding/pem" "fmt" "io" + "log/slog" "net" "os" "os/exec" @@ -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()) }