Skip to content

Support autocompletion for kuberc aliases #1737

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
hendrikKahl opened this issue Apr 25, 2025 · 6 comments · May be fixed by kubernetes/kubernetes#131586
Open

Support autocompletion for kuberc aliases #1737

hendrikKahl opened this issue Apr 25, 2025 · 6 comments · May be fixed by kubernetes/kubernetes#131586
Assignees
Labels
kind/feature Categorizes issue or PR as related to a new feature. triage/accepted Indicates an issue or PR is ready to be actively worked on.

Comments

@hendrikKahl
Copy link

What would you like to be added:

It would be great, if aliases in a kuberc file would trigger / work with regular kubectl autocompletion.

Why is this needed:

For generic aliases autocompletion would super useful. For example, an alias like the following would be even more useful, if autocompletion would work for both kind and resource names.

  - name: gs
    command: get
    flags:
    - name: sort-by
      default: '{..creationTimestamp}'
@hendrikKahl hendrikKahl added the kind/feature Categorizes issue or PR as related to a new feature. label Apr 25, 2025
@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Apr 25, 2025
@ardaguclu
Copy link
Member

Thank you for opening this issue and your interest on kuberc.

I'll mark this as triage accepted to investigate more. Just be aware that this doesn't mean that we'll add this feature soon (or ever).
/triage accepted
/assign

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Apr 25, 2025
@hendrikKahl
Copy link
Author

Thanks for taking a look :)

Just to share a bit more context - I tried it with a v1.33.0 kubectl in a zsh. I could not get autocompletion for the alias but for regular kubectl commands it works perfectly fine.

========= starting completion logic ==========
CURRENT: 3, words[*]: kubectl gs
Truncated words[*]: kubectl gs ,
lastParam: , lastChar:
Adding extra empty parameter
About to call: eval kubectl __complete gs  ""
[Debug] Cleared global flags for plugin completion
[Debug] [Error] unable to find a command for arguments: [gs]
completion output: :0
last line: :0
directive: 0
completions:
flagPrefix:
Calling _describe
_describe did not find completions.
Checking if we should do file completion.
Activating file completion

vs

========= starting completion logic ==========
CURRENT: 3, words[*]: kubectl get
Truncated words[*]: kubectl get ,
lastParam: , lastChar:
Adding extra empty parameter
About to call: eval kubectl __complete get  ""
completion output: componentstatuses
configmaps
endpoints
events
...

@ardaguclu
Copy link
Member

@marckhouzam would you have any thoughts about how we can implement completions for the aliases that actually maps to the actual kubectl commands prior to command execution.

@marckhouzam
Copy link
Member

I had to debug to see why it wasn't working automatically.
You can try a patch like this:

diff --git a/staging/src/k8s.io/kubectl/pkg/kuberc/kuberc.go b/staging/src/k8s.io/kubectl/pkg/kuberc/kuberc.go
index 03c6a2ef9b9..4b156c0f78e 100644
--- a/staging/src/k8s.io/kubectl/pkg/kuberc/kuberc.go
+++ b/staging/src/k8s.io/kubectl/pkg/kuberc/kuberc.go
@@ -194,7 +194,7 @@ func (p *Preferences) applyAliases(rootCmd *cobra.Command, kuberc *config.Prefer
 	var commandName string // first "non-flag" arguments
 	var commandIndex int
 	for index, arg := range args[1:] {
-		if !strings.HasPrefix(arg, "-") {
+		if !strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "__complete") {
 			commandName = arg
 			commandIndex = index + 1
 			break

Let me know if you want more details.

@ardaguclu
Copy link
Member

Many thanks @marckhouzam for your helping (as always :) ). I'll test soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature Categorizes issue or PR as related to a new feature. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants