Skip to content

feat: pass context and token flags to kubectl exec commands #3169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion internal/view/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabianonunes I think it will be best to just pass in the k8s flags at the call site and let buildShellArgs figure out what it need to extract out.

args = append(args, "--")
if len(cfg.Command) > 0 {
args = append(args, cfg.Command...)
Expand Down
19 changes: 14 additions & 5 deletions internal/view/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
56 changes: 51 additions & 5 deletions internal/view/pod_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,100 @@ 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": {
"fred/blee",
"",
"",
&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": {
"fred/blee",
"",
"linux",
&empty,
nil,
nil,
"exec -it -n fred blee -- sh -c " + shellCheck,
},
"windows": {
"fred/blee",
"c1",
windowsOS,
&empty,
nil,
nil,
"exec -it -n fred blee -c c1 -- powershell",
},
}

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, " "))
})
}
Expand Down
Loading