Kubectl: Autocomplete for kubectl pod name

Created on 6 Jun 2018  路  6Comments  路  Source: kubernetes/kubectl

Is this a request for help? (If yes, you should use our troubleshooting guide and community support channels, see http://kubernetes.io/docs/troubleshooting/.):
No
What keywords did you search in Kubernetes issues before filing this one? (If you have found any duplicates, you should instead reply there.):

autocomplete

Is this a BUG REPORT or FEATURE REQUEST? (choose one):
Feature request

Autocomplete of the very long pod name
For example, after I deploy a pod
I want to type less to get the description of the pod
So, I would like tab to work
To autocomplete the pod name for me

Kubernetes version (use kubectl version):
1.10.3

Environment:

  • Cloud provider or hardware configuration: minikube
  • OS (e.g. from /etc/os-release):
  • Kernel (e.g. uname -a):
  • Install tools:
  • Others:

What happened:

What you expected to happen:

How to reproduce it (as minimally and precisely as possible):

Anything else we need to know:

Most helpful comment

I am running into the same issue.
my environment is looks like:
```!bash
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.11", GitCommit:"637c7e288581ee40ab4ca210618a89a555b6e7e9", GitTreeState:"clean", BuildDate:"2018-11-26T14:38:32Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"13", GitVersion:"v1.13.2", GitCommit:"cff46ab41ff0bb44d8584413b598ad8360ec1def", GitTreeState:"clean", BuildDate:"2019-01-10T23:28:14Z", GoVersion:"go1.11.4", Compiler:"gc", Platform:"linux/amd64"}

$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.

$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.14.4
BuildVersion: 18E226

$ kubectl get pods
NAME READY STATUS RESTARTS AGE
academic-service-6f6c598b8d-x7hx8 1/1 Running 52 6d
admissions-service-86cb4d8cf4-fbrhc 1/1 Running 68 4d
course-schedule-service-674858c77d-45b59 1/1 Running 0 9h
course-schedule-service-674858c77d-qtxlr 1/1 Running 3 5h
sr-admin-service-858d54c8c5-gkhdv 1/1 Running 0 3h
student-document-service-77f9848c98-vvn7w 1/1 Running 10 4d
student-service-7bd7b5c557-q6nj8 1/1 Running 0 3m
````

However, when I type
```!bash
$ kubectl get pods student-service
````
and then TAB, it doesn't autocomplete.

All 6 comments

In my environment, autocompletion works for pods which have the longest name (253 characters). https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names

$ bash --version
GNU bash, version 4.4.12(1)-release (x86_64-unknown-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.3", GitCommit:"2bba0127d85d5a46ab4b778548be28623b32d0b0", GitTreeState:"clean", BuildDate:"2018-05-21T09:17:39Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.0", GitCommit:"fc32d2f3698e36b93322a3465f63a14e9f0eaead", GitTreeState:"clean", BuildDate:"2018-03-26T16:44:10Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}
$ source "$(brew --prefix)/etc/bash_completion"
$ source <(kubectl completion bash)
$ cat <<EOL > pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
spec:
  containers:
  - name: nginx
    image: nginx
EOL
$ kubectl apply -f pod.yaml 
pod "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" created
$ kubectl get pod <tab><tab>
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

@whs-dot-hk Could you please try the above steps?

@superbrothers There is already auto-complete in linux? I was on a mac in my office (only choice). But, I use linux (ubuntu b4 now fedora) all of the time. Will have a try again soon.

Yes, there is. However autocompletion also works on macOS.

@whs-dot-hk Were you able to resolve this issue? If you have still any problem, please feel free to reopen this issue.

/close

I am running into the same issue.
my environment is looks like:
```!bash
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.11", GitCommit:"637c7e288581ee40ab4ca210618a89a555b6e7e9", GitTreeState:"clean", BuildDate:"2018-11-26T14:38:32Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"13", GitVersion:"v1.13.2", GitCommit:"cff46ab41ff0bb44d8584413b598ad8360ec1def", GitTreeState:"clean", BuildDate:"2019-01-10T23:28:14Z", GoVersion:"go1.11.4", Compiler:"gc", Platform:"linux/amd64"}

$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.

$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.14.4
BuildVersion: 18E226

$ kubectl get pods
NAME READY STATUS RESTARTS AGE
academic-service-6f6c598b8d-x7hx8 1/1 Running 52 6d
admissions-service-86cb4d8cf4-fbrhc 1/1 Running 68 4d
course-schedule-service-674858c77d-45b59 1/1 Running 0 9h
course-schedule-service-674858c77d-qtxlr 1/1 Running 3 5h
sr-admin-service-858d54c8c5-gkhdv 1/1 Running 0 3h
student-document-service-77f9848c98-vvn7w 1/1 Running 10 4d
student-service-7bd7b5c557-q6nj8 1/1 Running 0 3m
````

However, when I type
```!bash
$ kubectl get pods student-service
````
and then TAB, it doesn't autocomplete.

I am using a centos machine to run kubectl and executing the below instruction worked for me

yum install bash-completion

echo 'source <(kubectl completion bash)' >>~/.bashrc 
kubectl completion bash >/etc/bash_completion.d/kubectl 
echo "alias k=\'kubectl\'" >> ~/.bashrc 
echo 'complete -F __start_kubectl k' >>~/.bashrc 
source ~/.bashrc 

once it's completed, it should be able to get the response with TAB TAB

Was this page helpful?
0 / 5 - 0 ratings