kubecolor
kubecolor copied to clipboard
autocompletion bug
this could very well have to do with my setup, but I'm getting something weird from the first word of autocomplete.
if i tab from kubecolor
, i get kubecolor $'\033'\[33m
kubecolor de
doesn't complete
but kubecolor get
, does work and gives me all the right options
I'm using zsh as my shell
Encoutering the same bug myself using zsh:
zsh 5.8 (x86_64-ubuntu-linux-gnu)
Using the latest release of kubecolor
Same on:
zsh 5.8 (x86_64-apple-darwin20.1.0) kubecolor 0.0.20
+1
very frustrating
zsh: stable 5.8 (bottled), HEAD
dty1er/tap/kubecolor: stable 0.0.20
+1 Does anyone have a workaround?
It's kinda weird because I'm not experiencing the problem in my zsh:
zsh 5.8 (arm-apple-darwin20.2.0)
And here's my autocompletion config in .zshrc
:
autoload -Uz compinit; compinit
source <(kubectl completion zsh)
alias k=kubecolor
compdef kubecolor=kubectl
Along with the output of kubectl version --short
:
Client Version: v1.21.4
Server Version: v1.21.4
I compiled from source code instead of using brew install
, not sure if that matters. Hope this can help someone.
I have the same issue on Manjaro Linux with kubecolor
installed from arch user repository (aur)
zsh 5.8 (x86_64-pc-linux-gnu)
aur/kubecolor 0.0.20-2 (+3 0.41) (Installed)
Colorize your kubectl output
This happens both with using
compdef kubecolor=kubectl
and modifying the output of kubectl completion zsh
for kubecolor
:
source <(cat <(echo -e "#compdef kubecolor\ncompdef _kubectl kubecolor") <(kubectl completion zsh | tail -n+3))
I have same issue with macbook pro and in CentOS 8 , configurations used are same as above.
This worked for me, at the moment:
# get zsh complete kubectl
source <(kubectl completion zsh)
alias kubectl=kubecolor
alias k=kubecolor
# make completion work with kubecolor
autoload -U +X bashcompinit && bashcompinit
complete -o nospace -C /usr/local/bin/kubecolor kubecolor
I'm tested this on zsh (MacBook Pro), at the bottom of ~/.zshrc
.
Let me know if this works also for you.
Above solution also worked for me.
@fernaspiazu Thanks, it works. I have only two lines (brew already sources completion in my case):
alias k=kubecolor
complete -o nospace -C /usr/local/bin/kubecolor kubecolor
@Noksa's suggestions works for me with /usr/bin
instead of /usr/local/bin
on Manjaro Linux
alias k=kubecolor
complete -o nospace -C /usr/bin/kubecolor kubecolor
There is an issue with above fix, it fixes the color code characters , but now autocomplete do not complete for pod/services/resources/etc name.
Running Bash 5.0.x
with Ubuntu 20.04
I had this error trying completion for namespaces:
k -n bash: 4: syntax error: invalid arithmetic operator (error token is "")
Fixed in my .bashrc
with:
alias k=kubecolor
complete -o nospace -F __start_kubectl k
Was using default
vs nospace
now in complete option.
I have a similar issue as @Misteur-Z. When I type k -n [TAB]
I get:
-bash: 4: syntax error: invalid arithmetic operator (error token is "")
The suggested fix didn't solve the problem. My .bashrc
source <(kubectl completion bash)
alias k=kubecolor
complete -o default -F __start_kubectl k
I'm on CentOS7
$ bash --version
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
I have the same problem as @Misteur-Z and @hussam-qasem. But similar to @hussam-qasem the workaround by @Misteur-Z does not work for me.
kubectl version: Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.3", GitCommit:"c92036820499fedefec0f847e2054d824aea6cd1", GitTreeState:"archive", BuildDate:"1980-01-01T00:00:00Z", GoVersion:"go1.16.9", Compiler:"gc", Platform:"linux/amd64"}
kubcolor release 0.0.20
GNU bash, version 5.1.8(1)-release
This is still an issue. I can replicate in macOS 12.1
❯ system_profiler SPSoftwareDataType
Software:
System Software Overview:
System Version: macOS 12.1 (21C52)
Kernel Version: Darwin 21.2.0
Boot Volume: Macintosh HD
Boot Mode: Normal
...
❯ zsh --version
zsh 5.8 (x86_64-apple-darwin21.0)
❯ kubecolor --kubecolor-version
0.0.20
Although the above workaround -o nospace
removes the colour codes from the tab complete, as mentioned, it removes completion for kubernetes resources pod/services/resources/etc
.
Current .zshrc
source <(kubectl completion zsh)
command -v kubecolor >/dev/null 2>&1 && alias kubectl="kubecolor"
compdef kubecolor=kubectl
alias k='kubectl'
I also faced this problem. It just stopped working. UPDATE: Sorry I need to be more specific. I wasn't able to find what had been updated on my side. I do realize things aren't become broken just itself. kubecolor is 0.20, kubectl is 1.21.1 and was so since released. oh-my-zsh hasn't been upgraded for a while. In general I don't use autocomplete often but aliases so it might be some regression in v0.20
source <(kubectl completion zsh)
alias kubectl=kubecolor
# make completion work with kubecolor
alias k=kubecolor
compdef kubecolor=kubectl
Same thing on fish shell, except it spews a lot of errors too. With official fish kubectl completion that was added a while ago
Any update on this one?
Ok so I did some digging into this, it seems the issue is here: https://github.com/hidetatz/kubecolor/blob/828af61cd8a55c052fb42c755d734c856a775af2/command/subcommand.go#L26-L29
Basically, when entering in kubecolor <tab>
, the code treats this input as not having found a subcommandFound
, thus, it assumes that the input being received is something like kubecolor --help
, when in reality there is no input being passed. Kubecolor tries to colorize the <tab>
(for autocomplete), which is why you see the ANSI escape color characters.
I tested this by modifying the code to return false, subcommandInfo
, the result is that autocompletion
now works for me, however, things like kubecolor --help
are no longer colored as a result:

I'm not sure what the correct way to fix this or what people would prefer, working autocompletion but a non-colored --help
output, or a colored --help
output, but non-working autocompletion. Or perhaps there is a better way to detect when colorization should be applied (eg, only if enter
/ newline
is received, then parse the stdin/out/err for colorization, otherwise skip).
I'm happy to submit a PR to change the behavior of the return to return false, subcommandInfo
or if there are suggestions on how to better detect the input being received.
For now, I just cloned the repository, changed the return I mentioned above to return false, subcommandInfo
and don't care that the help
/ usage
output is in plain white text (before it was in a darkish yellow text, so if anything it is easier to read for me).
This worked for me, at the moment:
# get zsh complete kubectl source <(kubectl completion zsh) alias kubectl=kubecolor alias k=kubecolor # make completion work with kubecolor autoload -U +X bashcompinit && bashcompinit complete -o nospace -C /usr/local/bin/kubecolor kubecolor
I'm tested this on zsh (MacBook Pro), at the bottom of
~/.zshrc
.Let me know if this works also for you.
this worked for me too, i using ubuntu! only changed kubecolor path.
the above solution only fix special char issue, but than it do not show autocomplete options for running pods. I have tested above in macbook.
I'm facing the same problem in ubuntu 20.04.4 (wsl2) but it only happens for the autocomplete when trying to autocomplete a list of "some" things, like contexts or namespaces, but it works ok for pods or other elements...
kubecolor --context (tab tab) -bash: 4: syntax error: invalid arithmetic operator (error token is "")
kubecolor --context contexto -n (tab tab) -bash: 4: syntax error: invalid arithmetic operator (error token is "")
kubecolor --context contexto -n istio-system get pods (tab tab)
istio-egressgateway-79dd756dc-wpp99 istio-elasticsearch-es-master-2 istio-redis-ha-server-1 istiod-1-13-2-55df56f675-v78cc
istio-egressgateway-79dd756dc-xt4bx
kubecolor works ok with some kubectl versions, like v1.21.12 (which matches @SignorMercurio comment in this thread, and fails (at least) with version v1.22.9.
+1 for https://github.com/hidetatz/kubecolor/issues/78#issuecomment-981552699
Here are bash debug logs for kubectl(working) and kubecolor(not working):
# kubectl without kubecolor
$ set -x
$ kubectl -n [TAB] + local cur prev words cword split
+ declare -A flaghash # <- call __start_kubectl, elliding to the buggy codes for brevity
+ declare -A aliashash
+ declare -F _init_completion
...
+ [[ -n '' ]]
++ eval 'k __completeNoDesc -n ""'
+ out='calico-apiserver
calico-system
default
keda
kube-node-lease
kube-public
kube-system
local-path-storage
mysql-cluster
mysql-operator
nfs-provisioner
prometheus
tigera-operator
:4'
+ directive=4
+ out='calico-apiserver
calico-system
default
keda
kube-node-lease
kube-public
kube-system
local-path-storage
mysql-cluster
mysql-operator
nfs-provisioner
prometheus
tigera-operator
'
+ '[' 4 = 'calico-apiserver
calico-system
default
keda
kube-node-lease
kube-public
kube-system
local-path-storage
mysql-cluster
mysql-operator
nfs-provisioner
prometheus
tigera-operator
' ']'
+ __kubectl_debug '__kubectl_handle_go_custom_completion: the completion directive is: 4'
+ [[ -n '' ]]
+ __kubectl_debug '__kubectl_handle_go_custom_completion: the completions are: calico-apiserver
calico-system
default
keda
kube-node-lease
kube-public
kube-system
local-path-storage
mysql-cluster
mysql-operator
nfs-provisioner
prometheus
tigera-operator
'
+ [[ -n '' ]]
+ '[' 0 -ne 0 ']'
+ '[' 0 -ne 0 ']'
+ '[' 4 -ne 0 ']'
...
+ return
+ return
# kubecolor
$ set -x
$ kubecolor -n + local cur prev words cword split
+ declare -A flaghash
+ declare -A aliashash
+ declare -F _init_completion
+ _init_completion -s
...
++ eval 'kubecolor __completeNoDesc -n ""'
+ out='calico-apiserver
calico-system
default
keda
kube-node-lease
kube-public
kube-system
local-path-storage
mysql-cluster
mysql-operator
nfs-provisioner
prometheus
tigera-operator
:4'
+ directive='4'
+ out='calico-apiserver
calico-system
default
keda
kube-node-lease
kube-public
kube-system
local-path-storage
mysql-cluster
mysql-operator
nfs-provisioner
prometheus
tigera-operator
'
+ '[' '4' = 'calico-apiserver
calico-system
default
keda
kube-node-lease
kube-public
kube-system
local-path-storage
mysql-cluster
mysql-operator
nfs-provisioner
prometheus
tigera-operator
' ']'
+ __kubectl_debug '__kubectl_handle_go_custom_completion: the completion directive is: 4'
+ [[ -n '' ]]
+ __kubectl_debug '__kubectl_handle_go_custom_completion: the completions are: calico-apiserver
calico-system
default
keda
kube-node-lease
kube-public
kube-system
local-path-storage
mysql-cluster
mysql-operator
nfs-provisioner
prometheus
tigera-operator
'
+ [[ -n '' ]]
-bash: 4: syntax error: invalid arithmetic operator (error token is "")
I guess the buggy code line of kubectl bash completion is about L 209 (cannot find reference link):
...
205 local completions
206 completions=("${commands[@]}")
207 if [[ ${#must_have_one_noun[@]} -ne 0 ]]; then
208 completions+=("${must_have_one_noun[@]}")
209 elif [[ -n "${has_completion_function}" ]]; then
210 # if a go completion function is provided, defer to that function
211 __kubectl_handle_go_custom_completion
212 fi
213 if [[ ${#must_have_one_flag[@]} -ne 0 ]]; then
214 completions+=("${must_have_one_flag[@]}")
215 fi
216 while IFS='' read -r comp; do
217 COMPREPLY+=("$comp")
218 done < <(compgen -W "${completions[*]}" -- "$cur")
...
It's kinda weird because I'm not experiencing the problem in my zsh:
zsh 5.8 (arm-apple-darwin20.2.0)
And here's my autocompletion config in
.zshrc
:autoload -Uz compinit; compinit source <(kubectl completion zsh) alias k=kubecolor compdef kubecolor=kubectl
Along with the output of
kubectl version --short
:Client Version: v1.21.4 Server Version: v1.21.4
I compiled from source code instead of using
brew install
, not sure if that matters. Hope this can help someone.
Hi I have almost the same setup as yours in terms of .zshrc. Differences: Ubuntu, not Mac and no k alias. So I do kubectl g(tab) and I get this: kubectl $'\033'[33mget I do kubectl get po(tab) and I get this: kubectl get pod. Tab again and I get a correct list of options.
So it works if I add a command to kubectl but not before that. Does the same for get, create, describe and everything else I have tried so far. Does this make any sense to anybody?
I have also tried with a k alias for kubectl. Same behavior
All help appreciated
I have a fresh Kubernetes installation, where it worked just fine. When I upgraded Kubernetes, kubecolor
auto-completion started to exhibit this behavior. I'm not sure why a Kubernetes upgrade would affect kubecolor
.
I'm new to zsh and kubectl, just toying with kubecolor as is easier to read json and yaml outputs. I successfully ran kubectl auto completion as per official documentation with
autoload -Uz compinit; compinit
source <(kubectl completion zsh)
Kubecolor works fine by just aliasing the kubect
command to kubecolor
, but autocompletion misbehave, it somehow works, but definitively is doing weird things.
This is my .zshrc
section for kubectl
completion and kubecolor
aliasing. I'm using vanilla kubectl
command to contrast proper autocompletion and using the aliased k
command to see how kubecolor is behaving.
NOTE: I had to put kubectl
autocompletion at the very end of my .zshrc
in order to work, if placed somewhere else, it simply didn't work.
autoload -Uz compinit; compinit
# get zsh complete kubectl
source <(kubectl completion zsh)
#alias kubectl=kubecolor
alias k=kubecolor
# make completion work with kubecolor
autoload -U +X bashcompinit && bashcompinit
complete -o nospace -C /usr/bin/kubecolor kubecolor
As one image worth more than a thousand words, in the following gif you can see how kubectl properly autocompletes, and how kubecolor is autocompleting. First, just inserting the command kubecolor
and doing tab-tab
, doesn't show the proper parameters for kubectl
, but I can start typing the parameter, and it shows and completes them, but when arriving at the pod name or something more 'deeper', it simply can't obtain them.
On the other hand, it prints a completely different output as it is parsing bad the output, so while kubectl tab-tab
shows
Completing completions
alpha -- Commands for features in alpha
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"
apply -- Apply a configuration to a resource by file name or stdin
attach -- Attach to a running container
auth -- Inspect authorization
autoscale -- Auto-scale a deployment, replica set, stateful set, or replication controller
certificate -- Modify certificate resources.
cluster-info -- Display cluster information
completion -- Output shell completion code for the specified shell (bash, zsh or fish)
config -- Modify kubeconfig files
cordon -- Mark node as unschedulable
cp -- Copy files and directories to and from containers
create -- Create a resource from a file or from stdin
debug -- Create debugging sessions for troubleshooting workloads and nodes
delete -- Delete resources by file names, stdin, resources and names, or by resources and label selector
describe -- Show details of a specific resource or group of resources
diff -- Diff the live version against a would-be applied version
drain -- Drain node in preparation for maintenance
edit -- Edit a resource on the server
exec -- Execute a command in a container
explain -- Get documentation for a resource
expose -- Take a replication controller, service, deployment or pod and expose it as a new Kubernetes service
get -- Display one or many resources
help -- Help about any command
kustomize -- Build a kustomization target from a directory or URL.
label -- Update the labels on a resource
logs -- Print the logs for a container in a pod
options -- Print the list of flags inherited by all commands
patch -- Update fields of a resource
plugin -- Provides utilities for interacting with plugins
port-forward -- Forward one or more local ports to a pod
proxy -- Run a proxy to the Kubernetes API server
replace -- Replace a resource by file name or stdin
rollout -- Manage the rollout of a resource
run -- Run a particular image on the cluster
scale -- Set a new size for a deployment, replica set, or replication controller
set -- Set specific features on objects
taint -- Update the taints on one or more nodes
top -- Display resource (CPU/memory) usage
uncordon -- Mark node as schedulable
version -- Print the client and server version information
wait -- Experimental: Wait for a specific condition on one or many resources
kubecolor tab-tab
shows
\"kubectl Wait documentation plugins$'\033'\[0m
$'\033'\[33m \[flags\] drain pod
$'\033'\[33m$'\033'\[0m \[options\]$'\033'\[0m edit pod$'\033'\[0m
$'\033'\[33mAdvanced a exec port-forward
$'\033'\[33mBasic about explain ports
$'\033'\[33mCluster against expose preparation
$'\033'\[33mDeploy all features proxy
$'\033'\[33mOther alpha fields replace
$'\033'\[33mSettings alpha$'\033'\[0m file replica
$'\033'\[33mTroubleshooting and files replication
$'\033'\[33mUsage:$'\033'\[0m annotate files$'\033'\[0m resource
$'\033'\[33mUse annotations fish\)$'\033'\[0m resource$'\033'\[0m
$'\033'\[33mkubectl api-resources for resources
\(Beginner\):$'\033'\[0m api-versions form resources$'\033'\[0m
\(CPU/memory\) applied from resources.$'\033'\[0m
\(Intermediate\):$'\033'\[0m apply get rollout
\(applies as given run
\(bash, at: global running
--help\" attach group scale
\<command\> auth group/version$'\033'\[0m schedulable$'\033'\[0m
API authorization$'\033'\[0m https://kubernetes.io/docs/reference/kubectl/overview/$'\033'\[0m selector$'\033'\[0m
Apply autoscale image server
Attach by in server$'\033'\[0m
Auto-scale certificate information server,
Build client information$'\033'\[0m service$'\033'\[0m
Commands cluster interacting service,
Commands:$'\033'\[0m cluster$'\033'\[0m it sessions
Copy cluster-info kubeconfig set
Create code kubectl set,
Debugging command kustomization shell
Delete command-line kustomize size
Diff command.$'\033'\[0m label specific
Display commands\).$'\033'\[0m labels specified
Drain completion list stateful
Edit condition live stdin$'\033'\[0m
Execute config local stdin,
Experimental: configuration logs supported
Find container maintenance$'\033'\[0m taint
Forward container$'\033'\[0m manager.$'\033'\[0m taints
Get containers$'\033'\[0m many target
Inspect controller$'\033'\[0m more the
Kubernetes controller, name to
Manage controls names, top
Management cordon new troubleshooting
Mark cp node uncordon
Modify create nodes$'\033'\[0m unschedulable$'\033'\[0m
Output debug objects$'\033'\[0m usage$'\033'\[0m
Print debugging of utilities
Provides delete on version
Replace deployment one version$'\033'\[0m
Run deployment, options versions
Set describe options\" wait
Show details or with
Take diff particular workloads
URL.$'\033'\[0m directories patch would-be
Update directory plugin zsh
Please note it didn't say Completing completions
at top, and it prints the kubectl tab-tab
output completely borked. Besides that it shows those weird symbols $'\033'\[0m
and $'\033'\[33mB
that I don't really understand what's trying to render.
I'm on Arch Linux, kubecolor installed through AUR package, and my versions are
zsh --version
zsh 5.9 (x86_64-pc-linux-gnu)
zsh-completions-0.34.0-2
kubecolor --kubecolor-version
0.0.20
kubectl version --short
Flag --short has been deprecated, and will be removed in the future. The --short output will become the default.
Client Version: v1.24.3
Kustomize Version: v4.5.6
Server Version: v1.24.3+k3s1
I'm getting the same error when using kubecolor in combination with the autocomplete feature for kubectl. The error happens when I press tab to autocomplete the namespaces:
k -n -bash: 4: syntax error: invalid arithmetic operator (error token is "")
I'm using on Debian 10
using kubecolor v0.0.20
and bash 5.0.3(1)-release
. My client version is kubectl v1.22.12
.
My .bashrc
looks like this. I tried moving some parts around, but the error happens no matter what I try.
source <(kubectl completion bash)
alias kubectl="kubecolor"
alias k="kubecolor"
complete -o default -F __start_kubectl k
I would like to see this fixed as well, as this is an incredibly helpful project.
I solved the problem temporarily. The cause of this problem is that the new version of kubectl has changed. The output results of different versions of kubectl to exec command “kubectl completion bash” are different. When I use kubectl v1.22.0, The problem disappears.