From 325f1484f985b8d8daa24a53f790a7044b327eb5 Mon Sep 17 00:00:00 2001 From: Damian Czaja Date: Mon, 19 Feb 2024 17:43:10 +0100 Subject: [PATCH] kubectl: respect namespace flag, when completing ApiResourceResources --- completers/kubectl_completer/cmd/apply_editLastApplied.go | 4 +++- completers/kubectl_completer/cmd/apply_viewLastApplied.go | 4 +++- completers/kubectl_completer/cmd/attach.go | 6 +++++- completers/kubectl_completer/cmd/auth_canI.go | 6 +++++- completers/kubectl_completer/cmd/edit.go | 6 +++++- completers/kubectl_completer/cmd/exec.go | 6 +++++- completers/kubectl_completer/cmd/logs.go | 6 +++++- completers/kubectl_completer/cmd/portForward.go | 6 +++++- completers/kubectl_completer/cmd/rollout_history.go | 6 +++++- completers/kubectl_completer/cmd/rollout_pause.go | 6 +++++- completers/kubectl_completer/cmd/rollout_restart.go | 6 +++++- completers/kubectl_completer/cmd/rollout_resume.go | 6 +++++- completers/kubectl_completer/cmd/rollout_status.go | 6 +++++- completers/kubectl_completer/cmd/rollout_undo.go | 6 +++++- completers/kubectl_completer/cmd/set_env.go | 8 ++++++-- pkg/actions/tools/kubectl/api.go | 8 ++++++-- 16 files changed, 78 insertions(+), 18 deletions(-) diff --git a/completers/kubectl_completer/cmd/apply_editLastApplied.go b/completers/kubectl_completer/cmd/apply_editLastApplied.go index 943ddd982d..bb74e5f4d1 100644 --- a/completers/kubectl_completer/cmd/apply_editLastApplied.go +++ b/completers/kubectl_completer/cmd/apply_editLastApplied.go @@ -43,7 +43,9 @@ func init() { if apply_editLastAppliedCmd.Flag("filename").Changed { return carapace.ActionValues() } else { - return kubectl.ActionApiResourceResources() + return kubectl.ActionApiResourceResources(kubectl.ApiResourceResourcesOpts{ + Namespace: rootCmd.Flag("namespace").Value.String(), + }) } }), ) diff --git a/completers/kubectl_completer/cmd/apply_viewLastApplied.go b/completers/kubectl_completer/cmd/apply_viewLastApplied.go index 42d1cc40b9..a507ad1348 100644 --- a/completers/kubectl_completer/cmd/apply_viewLastApplied.go +++ b/completers/kubectl_completer/cmd/apply_viewLastApplied.go @@ -34,7 +34,9 @@ func init() { if apply_editLastAppliedCmd.Flag("filename").Changed { return carapace.ActionValues() } else { - return kubectl.ActionApiResourceResources() + return kubectl.ActionApiResourceResources(kubectl.ApiResourceResourcesOpts{ + Namespace: rootCmd.Flag("namespace").Value.String(), + }) } }), ) diff --git a/completers/kubectl_completer/cmd/attach.go b/completers/kubectl_completer/cmd/attach.go index 2719472e69..30aba73f1b 100644 --- a/completers/kubectl_completer/cmd/attach.go +++ b/completers/kubectl_completer/cmd/attach.go @@ -37,6 +37,10 @@ func init() { }) carapace.Gen(attachCmd).PositionalCompletion( - kubectl.ActionApiResourceResources(), + carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return kubectl.ActionApiResourceResources(kubectl.ApiResourceResourcesOpts{ + Namespace: rootCmd.Flag("namespace").Value.String(), + }) + }), ) } diff --git a/completers/kubectl_completer/cmd/auth_canI.go b/completers/kubectl_completer/cmd/auth_canI.go index 58bf8bb4ab..252de5b92b 100644 --- a/completers/kubectl_completer/cmd/auth_canI.go +++ b/completers/kubectl_completer/cmd/auth_canI.go @@ -26,6 +26,10 @@ func init() { carapace.Gen(auth_canICmd).PositionalCompletion( kubectl.ActionResourceVerbs(), - kubectl.ActionApiResourceResources(), + carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return kubectl.ActionApiResourceResources(kubectl.ApiResourceResourcesOpts{ + Namespace: rootCmd.Flag("namespace").Value.String(), + }) + }), ) } diff --git a/completers/kubectl_completer/cmd/edit.go b/completers/kubectl_completer/cmd/edit.go index 21209b7012..3017184632 100644 --- a/completers/kubectl_completer/cmd/edit.go +++ b/completers/kubectl_completer/cmd/edit.go @@ -43,6 +43,10 @@ func init() { }) carapace.Gen(editCmd).PositionalCompletion( - kubectl.ActionApiResourceResources(), + carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return kubectl.ActionApiResourceResources(kubectl.ApiResourceResourcesOpts{ + Namespace: rootCmd.Flag("namespace").Value.String(), + }) + }), ) } diff --git a/completers/kubectl_completer/cmd/exec.go b/completers/kubectl_completer/cmd/exec.go index 3dcb212723..bbd90f37fd 100644 --- a/completers/kubectl_completer/cmd/exec.go +++ b/completers/kubectl_completer/cmd/exec.go @@ -38,6 +38,10 @@ func init() { }) carapace.Gen(execCmd).PositionalCompletion( - kubectl.ActionApiResourceResources(), + carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return kubectl.ActionApiResourceResources(kubectl.ApiResourceResourcesOpts{ + Namespace: rootCmd.Flag("namespace").Value.String(), + }) + }), ) } diff --git a/completers/kubectl_completer/cmd/logs.go b/completers/kubectl_completer/cmd/logs.go index 359580da7d..05ca82c525 100644 --- a/completers/kubectl_completer/cmd/logs.go +++ b/completers/kubectl_completer/cmd/logs.go @@ -46,6 +46,10 @@ func init() { }) carapace.Gen(logsCmd).PositionalCompletion( - kubectl.ActionApiResourceResources(), + carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return kubectl.ActionApiResourceResources(kubectl.ApiResourceResourcesOpts{ + Namespace: rootCmd.Flag("namespace").Value.String(), + }) + }), ) } diff --git a/completers/kubectl_completer/cmd/portForward.go b/completers/kubectl_completer/cmd/portForward.go index 1e208067b8..1cded7a123 100644 --- a/completers/kubectl_completer/cmd/portForward.go +++ b/completers/kubectl_completer/cmd/portForward.go @@ -22,7 +22,11 @@ func init() { rootCmd.AddCommand(portForwardCmd) carapace.Gen(portForwardCmd).PositionalCompletion( - kubectl.ActionApiResourceResources(), + carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return kubectl.ActionApiResourceResources(kubectl.ApiResourceResourcesOpts{ + Namespace: rootCmd.Flag("namespace").Value.String(), + }) + }), carapace.ActionMultiParts(":", func(c carapace.Context) carapace.Action { switch len(c.Parts) { case 0: diff --git a/completers/kubectl_completer/cmd/rollout_history.go b/completers/kubectl_completer/cmd/rollout_history.go index 1998907023..1c3b040f44 100644 --- a/completers/kubectl_completer/cmd/rollout_history.go +++ b/completers/kubectl_completer/cmd/rollout_history.go @@ -34,6 +34,10 @@ func init() { }) carapace.Gen(rollout_historyCmd).PositionalCompletion( - kubectl.ActionApiResourceResources(), + carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return kubectl.ActionApiResourceResources(kubectl.ApiResourceResourcesOpts{ + Namespace: rootCmd.Flag("namespace").Value.String(), + }) + }), ) } diff --git a/completers/kubectl_completer/cmd/rollout_pause.go b/completers/kubectl_completer/cmd/rollout_pause.go index 5f34545e19..f3635b48a5 100644 --- a/completers/kubectl_completer/cmd/rollout_pause.go +++ b/completers/kubectl_completer/cmd/rollout_pause.go @@ -34,6 +34,10 @@ func init() { }) carapace.Gen(rollout_pauseCmd).PositionalCompletion( - kubectl.ActionApiResourceResources(), + carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return kubectl.ActionApiResourceResources(kubectl.ApiResourceResourcesOpts{ + Namespace: rootCmd.Flag("namespace").Value.String(), + }) + }), ) } diff --git a/completers/kubectl_completer/cmd/rollout_restart.go b/completers/kubectl_completer/cmd/rollout_restart.go index d746a2a250..37fc9c78ba 100644 --- a/completers/kubectl_completer/cmd/rollout_restart.go +++ b/completers/kubectl_completer/cmd/rollout_restart.go @@ -34,6 +34,10 @@ func init() { }) carapace.Gen(rollout_restartCmd).PositionalCompletion( - kubectl.ActionApiResourceResources(), + carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return kubectl.ActionApiResourceResources(kubectl.ApiResourceResourcesOpts{ + Namespace: rootCmd.Flag("namespace").Value.String(), + }) + }), ) } diff --git a/completers/kubectl_completer/cmd/rollout_resume.go b/completers/kubectl_completer/cmd/rollout_resume.go index 7a1014b182..bde97f1358 100644 --- a/completers/kubectl_completer/cmd/rollout_resume.go +++ b/completers/kubectl_completer/cmd/rollout_resume.go @@ -34,6 +34,10 @@ func init() { }) carapace.Gen(rollout_resumeCmd).PositionalCompletion( - kubectl.ActionApiResourceResources(), + carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return kubectl.ActionApiResourceResources(kubectl.ApiResourceResourcesOpts{ + Namespace: rootCmd.Flag("namespace").Value.String(), + }) + }), ) } diff --git a/completers/kubectl_completer/cmd/rollout_status.go b/completers/kubectl_completer/cmd/rollout_status.go index 1c2e2c20c0..823c159a06 100644 --- a/completers/kubectl_completer/cmd/rollout_status.go +++ b/completers/kubectl_completer/cmd/rollout_status.go @@ -30,6 +30,10 @@ func init() { }) carapace.Gen(rollout_statusCmd).PositionalCompletion( - kubectl.ActionApiResourceResources(), + carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return kubectl.ActionApiResourceResources(kubectl.ApiResourceResourcesOpts{ + Namespace: rootCmd.Flag("namespace").Value.String(), + }) + }), ) } diff --git a/completers/kubectl_completer/cmd/rollout_undo.go b/completers/kubectl_completer/cmd/rollout_undo.go index d4b9572999..1d33ef668d 100644 --- a/completers/kubectl_completer/cmd/rollout_undo.go +++ b/completers/kubectl_completer/cmd/rollout_undo.go @@ -37,6 +37,10 @@ func init() { }) carapace.Gen(rollout_undoCmd).PositionalCompletion( - kubectl.ActionApiResourceResources(), + carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return kubectl.ActionApiResourceResources(kubectl.ApiResourceResourcesOpts{ + Namespace: rootCmd.Flag("namespace").Value.String(), + }) + }), ) } diff --git a/completers/kubectl_completer/cmd/set_env.go b/completers/kubectl_completer/cmd/set_env.go index 2ab5fdc447..55dce2f414 100644 --- a/completers/kubectl_completer/cmd/set_env.go +++ b/completers/kubectl_completer/cmd/set_env.go @@ -41,12 +41,16 @@ func init() { carapace.Gen(set_envCmd).FlagCompletion(carapace.ActionMap{ "dry-run": kubectl.ActionDryRunModes(), "filename": carapace.ActionFiles(), - "from": kubectl.ActionApiResourceResources(), + "from": kubectl.ActionApiResourceResources(kubectl.ApiResourceResourcesOpts{}), "output": kubectl.ActionOutputFormats(), "template": carapace.ActionFiles(), }) carapace.Gen(set_envCmd).PositionalCompletion( - kubectl.ActionApiResourceResources(), + carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return kubectl.ActionApiResourceResources(kubectl.ApiResourceResourcesOpts{ + Namespace: rootCmd.Flag("namespace").Value.String(), + }) + }), ) } diff --git a/pkg/actions/tools/kubectl/api.go b/pkg/actions/tools/kubectl/api.go index 177872ff0e..ea3d1dcd1c 100644 --- a/pkg/actions/tools/kubectl/api.go +++ b/pkg/actions/tools/kubectl/api.go @@ -37,17 +37,21 @@ func ActionApiGroups() carapace.Action { ) } +type ApiResourceResourcesOpts struct { + Namespace string +} + // ActionApiResourceResources completes api resources and resources separately // // apiservices/v1.admissionregistration.k8s.io // endpoints/kubernetes -func ActionApiResourceResources() carapace.Action { +func ActionApiResourceResources(opts ApiResourceResourcesOpts) carapace.Action { return carapace.ActionMultiParts("/", func(c carapace.Context) carapace.Action { switch len(c.Parts) { case 0: return ActionApiResources().Invoke(c).Suffix("/").ToA() case 1: - return ActionResources(ResourceOpts{Namespace: "", Types: c.Parts[0]}) + return ActionResources(ResourceOpts{Namespace: opts.Namespace, Types: c.Parts[0]}) default: return carapace.ActionValues() }