From 9586b83d829b3d6b0d02786c0f05209e05a883f2 Mon Sep 17 00:00:00 2001 From: Fabiano Parente Date: Sat, 1 Mar 2025 21:19:46 -0300 Subject: [PATCH] feat: pass context and token flags to kubectl exec commands When executing shell in a container, only the kubeconfig flag was being passed to kubectl exec commands. This change ensures that context and bearer token flags are also passed when available, maintaining consistency with the initial k9s configuration parameters. --- internal/view/exec.go | 3 +- internal/view/pod.go | 19 ++++++++---- internal/view/pod_int_test.go | 56 +++++++++++++++++++++++++++++++---- 3 files changed, 67 insertions(+), 11 deletions(-) diff --git a/internal/view/exec.go b/internal/view/exec.go index d7d0fa04e7..e1b66745d8 100644 --- a/internal/view/exec.go +++ b/internal/view/exec.go @@ -337,7 +337,8 @@ func sshIn(a *App, fqn, co string) error { return fmt.Errorf("os detect failed: %w", err) } - args := buildShellArgs("exec", fqn, co, a.Conn().Config().Flags().KubeConfig) + flags := a.Conn().Config().Flags() + args := buildShellArgs("exec", fqn, co, flags.KubeConfig, flags.Context, flags.BearerToken) args = append(args, "--") if len(cfg.Command) > 0 { args = append(args, cfg.Command...) diff --git a/internal/view/pod.go b/internal/view/pod.go index 879bd6b487..6ce3ea494a 100644 --- a/internal/view/pod.go +++ b/internal/view/pod.go @@ -394,7 +394,9 @@ func shellIn(a *App, fqn, co string) { if err != nil { log.Warn().Err(err).Msgf("os detect failed") } - args := computeShellArgs(fqn, co, a.Conn().Config().Flags().KubeConfig, os) + + flags := a.Conn().Config().Flags() + args := computeShellArgs(fqn, co, flags.KubeConfig, flags.Context, flags.BearerToken, os) c := color.New(color.BgGreen).Add(color.FgBlack).Add(color.Bold) err = runK(a, shellOpts{clear: true, banner: c.Sprintf(bannerFmt, fqn, co), args: args}) @@ -438,22 +440,23 @@ func resumeAttachIn(a *App, c model.Component, path, co string) { } func attachIn(a *App, path, co string) { - args := buildShellArgs("attach", path, co, a.Conn().Config().Flags().KubeConfig) + flags := a.Conn().Config().Flags() + args := buildShellArgs("attach", path, co, flags.KubeConfig, flags.Context, flags.BearerToken) c := color.New(color.BgGreen).Add(color.FgBlack).Add(color.Bold) if err := runK(a, shellOpts{clear: true, banner: c.Sprintf(bannerFmt, path, co), args: args}); err != nil { a.Flash().Errf("Attach exec failed: %s", err) } } -func computeShellArgs(path, co string, kcfg *string, os string) []string { - args := buildShellArgs("exec", path, co, kcfg) +func computeShellArgs(path, co string, kcfg, ctx, token *string, os string) []string { + args := buildShellArgs("exec", path, co, kcfg, ctx, token) if os == windowsOS { return append(args, "--", powerShell) } return append(args, "--", "sh", "-c", shellCheck) } -func buildShellArgs(cmd, path, co string, kcfg *string) []string { +func buildShellArgs(cmd, path, co string, kcfg, ctx, token *string) []string { args := make([]string, 0, 15) args = append(args, cmd, "-it") ns, po := client.Namespaced(path) @@ -464,6 +467,12 @@ func buildShellArgs(cmd, path, co string, kcfg *string) []string { if kcfg != nil && *kcfg != "" { args = append(args, "--kubeconfig", *kcfg) } + if ctx != nil && *ctx != "" { + args = append(args, "--context", *ctx) + } + if token != nil && *token != "" { + args = append(args, "--token", *token) + } if co != "" { args = append(args, "-c", co) } diff --git a/internal/view/pod_int_test.go b/internal/view/pod_int_test.go index 3a84f7272f..fbd588e649 100644 --- a/internal/view/pod_int_test.go +++ b/internal/view/pod_int_test.go @@ -11,25 +11,56 @@ import ( ) func TestComputeShellArgs(t *testing.T) { - config, empty := "coolConfig", "" + config, ctx, token, empty := "coolConfig", "coolContext", "coolToken", "" _ = config uu := map[string]struct { - fqn, co, os string - cfg *string - e string + fqn, co, os string + cfg, ctx, token *string + e string }{ "config": { "fred/blee", "c1", "darwin", &config, + nil, + nil, "exec -it -n fred blee --kubeconfig coolConfig -c c1 -- sh -c " + shellCheck, }, + "context": { + "fred/blee", + "c1", + "darwin", + &config, + &ctx, + nil, + "exec -it -n fred blee --kubeconfig coolConfig --context coolContext -c c1 -- sh -c " + shellCheck, + }, + "token": { + "fred/blee", + "c1", + "darwin", + &config, + nil, + &token, + "exec -it -n fred blee --kubeconfig coolConfig --token coolToken -c c1 -- sh -c " + shellCheck, + }, + "config-context-token": { + "fred/blee", + "c1", + "darwin", + &config, + &ctx, + &token, + "exec -it -n fred blee --kubeconfig coolConfig --context coolContext --token coolToken -c c1 -- sh -c " + shellCheck, + }, "no-config": { "fred/blee", "c1", "linux", nil, + nil, + nil, "exec -it -n fred blee -c c1 -- sh -c " + shellCheck, }, "empty-config": { @@ -37,6 +68,17 @@ func TestComputeShellArgs(t *testing.T) { "", "", &empty, + nil, + nil, + "exec -it -n fred blee -- sh -c " + shellCheck, + }, + "empty-config-context-token": { + "fred/blee", + "", + "", + &empty, + &empty, + &empty, "exec -it -n fred blee -- sh -c " + shellCheck, }, "single-container": { @@ -44,6 +86,8 @@ func TestComputeShellArgs(t *testing.T) { "", "linux", &empty, + nil, + nil, "exec -it -n fred blee -- sh -c " + shellCheck, }, "windows": { @@ -51,6 +95,8 @@ func TestComputeShellArgs(t *testing.T) { "c1", windowsOS, &empty, + nil, + nil, "exec -it -n fred blee -c c1 -- powershell", }, } @@ -58,7 +104,7 @@ func TestComputeShellArgs(t *testing.T) { for k := range uu { u := uu[k] t.Run(k, func(t *testing.T) { - args := computeShellArgs(u.fqn, u.co, u.cfg, u.os) + args := computeShellArgs(u.fqn, u.co, u.cfg, u.ctx, u.token, u.os) assert.Equal(t, u.e, strings.Join(args, " ")) }) }