Skip to content

Commit

Permalink
Merge pull request #2252 from Trojan295/fix/kubectl/logs-namespaces
Browse files Browse the repository at this point in the history
kubectl: respect namespace flag, when completing ApiResourceResources
  • Loading branch information
rsteube authored Feb 19, 2024
2 parents 49a5ff1 + 325f148 commit 7c273d5
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 18 deletions.
4 changes: 3 additions & 1 deletion completers/kubectl_completer/cmd/apply_editLastApplied.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}
}),
)
Expand Down
4 changes: 3 additions & 1 deletion completers/kubectl_completer/cmd/apply_viewLastApplied.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}
}),
)
Expand Down
6 changes: 5 additions & 1 deletion completers/kubectl_completer/cmd/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}),
)
}
6 changes: 5 additions & 1 deletion completers/kubectl_completer/cmd/auth_canI.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}),
)
}
6 changes: 5 additions & 1 deletion completers/kubectl_completer/cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}),
)
}
6 changes: 5 additions & 1 deletion completers/kubectl_completer/cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}),
)
}
6 changes: 5 additions & 1 deletion completers/kubectl_completer/cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}),
)
}
6 changes: 5 additions & 1 deletion completers/kubectl_completer/cmd/portForward.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 5 additions & 1 deletion completers/kubectl_completer/cmd/rollout_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}),
)
}
6 changes: 5 additions & 1 deletion completers/kubectl_completer/cmd/rollout_pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}),
)
}
6 changes: 5 additions & 1 deletion completers/kubectl_completer/cmd/rollout_restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}),
)
}
6 changes: 5 additions & 1 deletion completers/kubectl_completer/cmd/rollout_resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}),
)
}
6 changes: 5 additions & 1 deletion completers/kubectl_completer/cmd/rollout_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}),
)
}
6 changes: 5 additions & 1 deletion completers/kubectl_completer/cmd/rollout_undo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}),
)
}
8 changes: 6 additions & 2 deletions completers/kubectl_completer/cmd/set_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}),
)
}
8 changes: 6 additions & 2 deletions pkg/actions/tools/kubectl/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit 7c273d5

Please sign in to comment.