From ad6ed5c56cb1510ac8366bb39f64fa834616fe67 Mon Sep 17 00:00:00 2001 From: Tiago Silva Date: Tue, 8 Oct 2024 16:33:56 +0100 Subject: [PATCH 1/2] [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 --- 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 bc2b1e68f5d7..6ad52c2342c0 100644 --- a/lib/kube/kubeconfig/context_overwrite.go +++ b/lib/kube/kubeconfig/context_overwrite.go @@ -105,3 +105,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 4093707c1261..d94d961b5c71 100644 --- a/tool/tsh/common/kube_proxy.go +++ b/tool/tsh/common/kube_proxy.go @@ -24,6 +24,7 @@ import ( "encoding/pem" "fmt" "io" + "log/slog" "net" "os" "os/exec" @@ -261,9 +262,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.Default().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()) } From dfbe393f0f2515a011631e9a0081300126bfb063 Mon Sep 17 00:00:00 2001 From: Tiago Silva Date: Tue, 8 Oct 2024 18:22:43 +0100 Subject: [PATCH 2/2] Update kube_proxy.go Co-authored-by: Noah Stride --- tool/tsh/common/kube_proxy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tool/tsh/common/kube_proxy.go b/tool/tsh/common/kube_proxy.go index d94d961b5c71..18ff39cb82ca 100644 --- a/tool/tsh/common/kube_proxy.go +++ b/tool/tsh/common/kube_proxy.go @@ -266,7 +266,7 @@ func (c *proxyKubeCommand) printPrepare(cf *CLIConf, title string, clusters kube for _, cluster := range clusters { contextName, err := kubeconfig.ContextNameFromTemplate(c.overrideContextName, cluster.TeleportCluster, cluster.KubeCluster) if err != nil { - slog.Default().WarnContext(cf.Context, "Failed to generate context name.", "error", err) + 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})