Skip to content
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

kubectl bash completion causes bash file path completion to fail #48575

Closed
amitkgupta opened this Issue Jul 7, 2017 · 14 comments

Comments

Projects
None yet
10 participants
@amitkgupta
Copy link

amitkgupta commented Jul 7, 2017

Is this a BUG REPORT or FEATURE REQUEST?:

/kind bug

What happened:

Bash file path completion fails when trying to run kubectl commands and source /usr/local/etc/bash_completion.d/kubectl is in ~/.bash_profile. If I run kubectl delete -f ../<TAB> then I get the error message show below in the section on reproduction.

What you expected to happen:

I espect to see files/directories that ../ can complete to.

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

Amits-MacBook-Pro:boku amitgupta$ cat ~/.bash_profile
### kubectl ###
source /usr/local/etc/bash_completion.d/kubectl
Amits-MacBook-Pro:boku amitgupta$ exec -l $SHELL
Amits-MacBook-Pro:boku amitgupta$ kubectl ../bash: __ltrim_colon_completions: command not found

I hit <TAB> after kubectl ../.

Anything else we need to know?:

This problem arises only with the kubectl command, not any others.

It arises no matter what I seem to put in after kubectl, even kubectl <SPACE><TAB> brings up the issue, doesn't have to be something like kubectl delete -f ../<TAB>.

The error message changes. In the example above, it says bash: __ltrim_colon_completions: command not found, but sometimes it says bash: _filedir: command not found in the same place.

When I remove kubectl Bash completions from my ~/.bash_profile (and restart/re-login to the shell), the problem goes away. These file completions came with my Homebrew installation of kubectl.

There is no difference between what Homebrew provided and what kubectl completion bash provides:

$ diff <(kubectl completion bash) /usr/local/etc/bash_completion.d/kubectl

Environment:

  • Kubernetes version (use kubectl version):
    $ kubectl version
    Client Version: version.Info{Major:"1", Minor:"7", GitVersion:"v1.7.0", GitCommit:"d3ada0119e776222f11ec7945e6d860061339aad", GitTreeState:"clean", BuildDate:"2017-06-30T09:51:01Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"darwin/amd64"}
    Server Version: version.Info{Major:"1", Minor:"6", GitVersion:"v1.6.6", GitCommit:"7fa1c1756d8bc963f1a389f4a6937dc71f08ada2", GitTreeState:"clean", BuildDate:"2017-06-16T18:21:54Z", GoVersion:"go1.7.6", Compiler:"gc", Platform:"linux/amd64"}
    
  • Cloud provider or hardware configuration**: N/A
  • OS (e.g. from /etc/os-release):
    $ sw_vers
    ProductName:	Mac OS X
    ProductVersion:	10.12.5
    BuildVersion:	16F2104
    
  • Kernel (e.g. uname -a):
    $ uname -a
    Darwin Amits-MacBook-Pro.local 16.6.0 Darwin Kernel Version 16.6.0: Fri Apr 14 16:13:31 PDT 2017; root:xnu-3789.60.24~4/RELEASE_X86_64 x86_64
    
  • Install tools:
    $ brew --version
    Homebrew 1.2.4
    Homebrew/homebrew-core (git revision 36f1; last commit 2017-07-06)
    
    $ brew info --json=v1 kubectl | jq '.[0].name,.[0].installed[0].version' -r
    kubernetes-cli
    1.7.0
    
  • Others:
    $ bash --version
    GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
    Copyright (C) 2007 Free Software Foundation, Inc.
    
@k8s-github-robot

This comment has been minimized.

Copy link
Contributor

k8s-github-robot commented Jul 7, 2017

@amitkgupta There are no sig labels on this issue. Please add a sig label by:
(1) mentioning a sig: @kubernetes/sig-<team-name>-misc
e.g., @kubernetes/sig-api-machinery-* for API Machinery
(2) specifying the label manually: /sig <label>
e.g., /sig scalability for sig/scalability

Note: method (1) will trigger a notification to the team. You can find the team list here and label list here

@amitkgupta

This comment has been minimized.

Copy link
Author

amitkgupta commented Jul 7, 2017

/sig cli

@superbrothers

This comment has been minimized.

Copy link
Member

superbrothers commented Oct 23, 2017

You need to source bash-completion from either /etc/bashrc or ~/.bashrc. See kubectl completion -h for more details.

$ kubectl completion -h
Output shell completion code for the specified shell (bash or zsh). The shell code must be evalutated to provide
interactive completion of kubectl commands.  This can be done by sourcing it from the .bash _profile. 

Note: this requires the bash-completion framework, which is not installed by default on Mac.  This can be installed by
using homebrew: 

  $ brew install bash-completion
  
Once installed, bash completion must be evaluated.  This can be done by adding the following line to the .bash profile 

  $ source $(brew --prefix)/etc/bash_completion
  
Note for zsh users: [1] zsh completions are only supported in versions of zsh >= 5.2

Examples:
  # Install bash completion on a Mac using homebrew
  brew install bash-completion
  printf "
  # Bash completion support
  source $(brew --prefix)/etc/bash_completion
  " >> $HOME/.bash_profile
  source $HOME/.bash_profile
  
  # Load the kubectl completion code for bash into the current shell
  source <(kubectl completion bash)
  
  # Write bash completion code to a file and source if from .bash_profile
  kubectl completion bash > ~/.kube/completion.bash.inc
  printf "
  # Kubectl shell completion
  source '$HOME/.kube/completion.bash.inc'
  " >> $HOME/.bash_profile
  source $HOME/.bash_profile
  
  # Load the kubectl completion code for zsh[1] into the current shell
  source <(kubectl completion zsh)

Usage:
  kubectl completion SHELL [options]

Use "kubectl options" for a list of global command-line options (applies to all commands).
@cblecker

This comment has been minimized.

Copy link
Member

cblecker commented Oct 23, 2017

/kind support
/close

@Amit-PivotalLabs

This comment has been minimized.

Copy link

Amit-PivotalLabs commented Oct 26, 2017

I stated in my issue that I sourced bash completion in my ~/.bash_profile. You (@superbrothers) stated that I need to source it from either /etc/bashrc or ~/.bashrc, and then point to the help output which does not say anything about bashrc and in fact explicitly says to do what I'm already doing, which is to use .bash_profile.

@cblecker could you please re-open this issue?

@cblecker

This comment has been minimized.

Copy link
Member

cblecker commented Oct 26, 2017

@Amit-PivotalLabs Is this still an issue with the latest version of kubectl? Can you provide fresh outputs, as well as the versions of kubectl, and bash? Can you also run a brew upgrade to ensure you have the latest version of bash_completion?

@Amit-PivotalLabs

This comment has been minimized.

Copy link

Amit-PivotalLabs commented Oct 26, 2017

Sure. On the first line below, I'm doing kubectl<SPACE><TAB> and you can see the output I'm getting.

$ kubectl  -bash: __ltrim_colon_completions: command not found

$ kubectl version -c
Flag shorthand -c has been deprecated, please use --client instead.
Client Version: version.Info{Major:"1", Minor:"8", GitVersion:"v1.8.2", GitCommit:"bdaeafa71f6c7c04636251031f93464384d54963", GitTreeState:"clean", BuildDate:"2017-10-24T21:07:53Z", GoVersion:"go1.9.1", Compiler:"gc", Platform:"darwin/amd64"}

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

This comment has been minimized.

Copy link

Amit-PivotalLabs commented Oct 26, 2017

Think I figured it out, I missed the part in the Examples section of the kubectl completion -h output which says to brew install bash-completion. The completion kind of works even without this, e.g. kubectl ap<TAB> gives:

$ kubectl apbash: __ltrim_colon_completions: command not found

api-versions  apply
@Amit-PivotalLabs

This comment has been minimized.

Copy link

Amit-PivotalLabs commented Oct 26, 2017

I'm able to complete other things without installing bash-completion, e.g brew i<TAB> gives:

$ brew i
info     install  irb

so there's something I'm missing about how bash completion generally works, what additional stuff you get when you brew install bash-completion, and how kubectl uses that additional stuff, but for the purposes of this issue I don't have any further questions.

@cblecker

This comment has been minimized.

Copy link
Member

cblecker commented Oct 26, 2017

@Amit-PivotalLabs I'm glad you were able to figure it out!

@simonxy

This comment has been minimized.

Copy link

simonxy commented Jan 26, 2018

I had same problem on OSX with native bash:

$ kubectl  -bash: __ltrim_colon_completions: command not found

It seems like, the whole thing only works when:

...

if [ -f /usr/local/share/bash-completion/bash_completion ]; then
  . /usr/local/share/bash-completion/bash_completion
fi

source ~/.kube/kubectl_autocompletion

above code snippet is what the final addition to my ~/.bash_profile

@koberjoel

This comment has been minimized.

Copy link

koberjoel commented Jun 13, 2018

I used this and it works:

  1. Create the folder and file:
    mkdir ~/.bash_completion.d
    vi ~/.bash_completion.d/kubectl

  2. copy in the following to vi:
    _kubectl()
    {
    local cur=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=( $(compgen -W "$(kubectl get pods --no-headers --output=custom-columns=NAME:.metadata.name )" -- $cur) )
    }
    complete -F _kubectl kubectl

  3. add the following to your .bash_profile and then reload it.
    . ~/.bash_completion.d/kubectl

@andersonVSA

This comment has been minimized.

Copy link

andersonVSA commented Sep 28, 2018

I had the same in Manjaro ( archlinux ), in addition to @simonxy :
pacman -S bash-completion kubectl completion bash > ~/.kube/kubectl_autocompletion
add to .bashrc
if [ -f /usr/local/share/bash-completion/bash_completion ]; then . /usr/local/share/bash-completion/bash_completion fi source ~/.kube/kubectl_autocompletion
and run
source .bashrc

@loganmzz

This comment has been minimized.

Copy link

loganmzz commented Jan 22, 2019

I'm on Windows with MinGW. After digging into system directories, it seems bash_completion script which contains helper functions such as _filedir is missing.

I had to get files from Git repository at https://github.com/scop/bash-completion/blob/master/bash_completion to /usr/share/bash-completion/bash_completion

Then add following line on top of my ~/.bashrc:

source /usr/share/bash-completion/bash_completion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.