Comments (11)
Since this plugin uses Cobra, making shell completion work is very easy.
You need to provide an executable file called kubectl_complete-kc
which can be found on $PATH
.
Below shows how to generate that file with the proper content.
Kubectl will call that file to obtain the shell completion results.
# Generate the required file
cat <<EOF >kubectl_complete-kc
#!/usr/bin/env sh
# Call the __complete command passing it all arguments
kubectl kc __complete "\$@"
EOF
# Make the file executable
chmod u+x kubectl_complete-kc
Now put the above file somewhere on your $PATH
@sunny0826 You could provide this file in your repo and provide instructions telling users to install it on $PATH
.
Note that krew
does not yet support installing this file. Ref: kubernetes-sigs/krew#812
@nekomeowww Until this is available you can use this project to automatically make such completions work for any kubectl plugin that uses the https://github.com/spf13/cobra project
I just used it with kc
and it works great
from kubecm.
Ok I've located the root cause. Such issue consists two major cause:
- Auto completion script for
zsh
(which issource <(kubectl completion zsh)
) must be incorrectly modified by myself when I playing aroundkrew
,kubectl plugin-completion generate
and messing around with my.zshrc
, it miss-commented out at some point. - When I remove the comment to my
kubectl
, completion plugins is still not working, withwhich kubectl
I found out that mykubectl
somehow pointed to/usr/local/bin/kubectl
, whilezsh
ofkubectl
points to1.28.3
,bash
didn't include the required$PATH
forhomebrew
installation, it still uses the oldv1.22.5
build, must be installed by the other applications or scripts.
I have gathered my needed blocks to test and contribute to kubecm
, I will submit a Pull Request ASAP later this day or tomorrow.
Again, thanks for debugging and providing me such details @marckhouzam , much appreciated.
from kubecm.
@nekomeowww Thanks for the feedback. This is a known issue, in fact, auto-completion does not work when kubecm is run as a kubectl plugin
, this is due to the kubectl mechanism, completion only works when kubecm is run as a standalone CLI.
from kubecm.
Oh, ok. Thanks for replying!
I never heard of this limitation, I might have to take a look at kubectl
cli and its community to seek out a solution or plan.
Is that ok if I create a documentation update Pull Request and state out the limitations of kubectl
and the installation through kubectl krew
?
from kubecm.
Absolutely! Thank you for your contribution!
from kubecm.
FYI, it looks like the completion is currently supported for plugins of kubectl
since kubernetes/kubernetes#105867 was merged.
However I haven't figure out how to use the documented way of this Pull Request to make the completion possible for kubectl kc
. I will keep you updated whether I succeed or failed.
from kubecm.
Thank you very much! Looks like I'm out of date.
from kubecm.
@marckhouzam Thank you so much !
from kubecm.
Thanks for replying and participating this issue! @marckhouzam
@sunny0826 You could provide this file in your repo and provide instructions telling users to install it on $PATH.
Note that krew does not yet support installing this file. Ref: kubernetes-sigs/krew#812@nekomeowww Until this is available you can use this project to automatically make such completions work for any kubectl plugin that uses the https://github.com/spf13/cobra project
I've tried either your plugin kubectl-plugin_completion
or the direct executable shell for _complete
, however I cannot get the completion for kubecm
to work properly.
For the manual, and direct shell, the completion file contains the following content:
#!/usr/bin/env zsh
# Call the __complete command passing it all arguments
kubectl kc __complete "\$@"
However, it would produce the following error when trying to use TAB to request the completion:
❯ kubectl kc (eval):1: command not found: _kubectl
(eval):1: command not found: _kubectl
If I try to execute the so do "proxy" completion shell directly, I get the prompt to say the completion ended:
❯ kubectl_complete-kc
:4
Completion ended with directive: ShellCompDirectiveNoFileComp
I just used it with
kc
and it works great
So how does your kubectl kc
works?
from kubecm.
I've tried either your plugin
kubectl-plugin_completion
or the direct executable shell for_complete
, however I cannot get the completion forkubecm
to work properly.For the manual, and direct shell, the completion file contains the following content:
#!/usr/bin/env zsh # Call the __complete command passing it all arguments kubectl kc __complete "\$@"
There should not be a \
before the $
sign in the script. The \
was in my output because I was using cat
However, it would produce the following error when trying to use TAB to request the completion:
❯ kubectl kc (eval):1: command not found: _kubectl (eval):1: command not found: _kubectl
Not sure about the above. Let's see if the other comments get rid of it.
If I try to execute the so do "proxy" completion shell directly, I get the prompt to say the completion ended:
❯ kubectl_complete-kc :4 Completion ended with directive: ShellCompDirectiveNoFileComp
Try kubectl_complete-kc ''
Assuming you have shell completion working for kubectl this is what I did.
Make sure that $HOME/.kubectl-plugin-completion/
is on your PATH.
$ echo $SHELL
/bin/zsh
$ kubectl version --client
Client Version: v1.28.3
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
$ kubectl krew install kc
Updated the local copy of plugin index.
Installing plugin: kc
W1023 10:12:49.612027 2698 install.go:160] Skipping plugin "kc", it is already installed
$ kubectl plugin-completion generate
[...]
# The above should generate the following file.
# Note that it is a little fancier than what I recommended in the previous
# command as it tries to complete the --namespace flag value as well
# but you don't really need that.
$ cat ~/.kubectl-plugin-completion/kubectl_complete-kc
#!/bin/bash
out=$(kubectl kc __complete "$@")
# If, once we remove the directive, there are completions, let's print them
if [ -n "${out%%:*}" ]; then
printf "%s\n" "${out[@]}"
elif [[ ${@: -2:1} = "-n" ]] || [[ ${@: -2:1} = "--namespace" ]]; then
# If there are no completions we provide completions if the previous argument is -n/--namespace.
# This case is pretty common but normally requires the plugin to have added support for it.
kubectl get namespaces --output go-template='{{ range .items }}{{ .metadata.name }}{{"\n"}}{{ end }}'
# Turn off file completion
echo :4
fi
$ kubectl <TAB>
annotate -- Update the annotations on a resource
api-resources -- Print the supported API resources on the server
api-versions -- Print the supported API versions on the server, in the form of "group/version"
[...]
kc -- The command kc is a plugin installed by the user
[...]
$ kubectl kc <TAB>
add -- Add KubeConfig to $HOME/.kube/config
alias -- Generate alias for all contexts
clear -- Clear lapsed context, cluster and user
cloud -- Manage kubeconfig from cloud
completion -- Generate completion script
create -- Create new KubeConfig(experiment)
delete -- Delete the specified context from the kubeconfig
export -- Export the specified context from the kubeconfig
help -- Help about any command
list -- List KubeConfig
merge -- Merge multiple kubeconfig files into one
namespace -- Switch or change namespace interactively
rename -- Rename the contexts of kubeconfig
switch -- Switch Kube Context interactively
version -- Print version info
from kubecm.
There should not be a \ before the $ sign in the script. The \ was in my output because I was using cat
Try kubectl_complete-kc ''
Thanks for pointing out these incorrect setup for me. I didn't find out these mistakes before.
And I can get
kubectl_complete-kc ''
working right now.
However, both kubectl plugin-completion generate
generated and hand written completion helper failed to be executed when used with kubectl kc
, it still prompts
❯ kubectl kc (eval):1: command not found: _kubectl
(eval):1: command not found: _kubectl
My kubectl version
results as
❯ kubectl version
Client Version: v1.28.3
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Server Version: v1.27.3
which should be compatible with the feature you added.
And indeed my completion for kubectl
somehow be broken at some point of time. Let me investigate it and update when I find out the cause of it.
from kubecm.
Related Issues (20)
- Kubecm throwing `.kube/config: is a directory` error in mac
- Asset names for v0.24.0 got broken HOT 4
- Support manage kubeconfig with Azure ASK
- Unable to switch to a namespace as I have no permissions to list namespaces HOT 3
- Generate context and user name smartly when merging config HOT 6
- Feature request: support completion for sub-commands like switch, delete, rename HOT 3
- error when creating kubeconfig in k8s v1.27.5 HOT 4
- Deleting context removes user even if other context is using it HOT 2
- [TODO] Update go 1.20
- [Feature] docs command
- export kubeconfig causes nil pointer HOT 7
- 这个工具太好用了!!!
- Missing 'v' in documentation HOT 1
- Monthly issue metrics report
- Monthly issue metrics report
- cloud add command fails, if ~/.kube/ does not exits HOT 6
- Monthly issue metrics report
- Monthly issue metrics report
- Monthly issue metrics report
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kubecm.