Why doesn't kubectl bash completion work on macOS/OS X?
Asked Answered
B

8

6

I followed the instructions for installing Bash completion as given by kubectl completion -h:

  1. I installed bash-completion via Homebrew
  2. In my ~/.bashrc, I first source bash-completion then output from the completion kubectl subcommand:
    • source $(brew --prefix)/etc/bash_completion
    • source <(kubectl completion bash)

With these in place, I start up a new shell but the completion doesn't work. How do I get it working?

Bray answered 21/7, 2017 at 15:0 Comment(0)
B
10

Once bash-completion is installed by Homebrew, it appears that its completions need to reside in $(brew --prefix)/etc/bash_completion.d. There you'll find a lot of other completions that come bundled. To add the completion for kubectl:

$ kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl

That did the trick for me.

Bray answered 21/7, 2017 at 15:0 Comment(0)
H
2

I the answer form Ahmet B, the fix says to add the following to your .bashrc file:

export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d"
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"

However, the install of completions 2:

brew install bash-completion@2

finishes with a message to add the export line if you would LIKE TO USE V1 completions. Removing that export enabled kubectl completion for me.

Hukill answered 13/6, 2019 at 19:44 Comment(0)
G
1

The above answers didn't work for me, but I found out this solution:

source /dev/stdin <<<"$(kubectl completion bash)"
Granddad answered 15/12, 2021 at 0:57 Comment(1)
I tried this on bash 3.2 with macOS 11.6, and it didn't work - the 'source from stdin' part works with 3.2, but I believe the kubectl-generated bash script (from parenthesised part) now requires bash 4.2+ and bash-completion v2.Treiber
V
1

Here is an alternative approach:

Install bash and bash-completion

brew install bash bash-completion

Set Terminal to use bash

Preferences > General > Shell open with: Command (complete path):

/opt/homebrew/bin/bash

Check the location bash-completion has been installed to

brew info bash-completion

Bash completion has been installed to: /opt/homebrew/etc/bash_completion.d

Edit ~/.bashrc

alias k="kubectl"
complete -F __start_kubectl k
source /opt/homebrew/etc/profile.d/bash_completion.sh
source <(kubectl completion bash)

Reload Terminal and verify that the completion works

~$ k <TAB>

alpha auth cordon diff get patch run version annotate autoscale cp ...

Environment

  • macOS Monterey 12.2.1
  • Homebrew 3.3.15
  • GNU bash, version 5.1.16(1)-release (aarch64-apple-darwin21.1.0)
  • bash-completion: stable 1.3 (bottled)
  • kubectl v1.23.3
Vaporize answered 17/2, 2022 at 13:44 Comment(1)
This does work, rest everything seems outdated.Dispossess
H
0

See the "On macOS, using bash" section of kubectl documentation: https://kubernetes.io/docs/tasks/tools/install-kubectl/#on-macos-using-bash I recently contributed those so they should be up to date. If not, please send a pull request to fix it.

Also: https://blog.fabric8.io/enable-bash-completion-for-kubernetes-with-kubectl-506bc89fe79e

Hernando answered 21/7, 2017 at 21:58 Comment(0)
I
0
  1. After brew install bash-completion, to actually enable bash completions, you need to:
    source /usr/local/etc/profile.d/bash_completion.sh
    
    Add that line to you bashrc.
  2. Then you can:
    source <(kubectl completion bash)
    
Interject answered 1/4, 2020 at 13:24 Comment(0)
S
0

managed to get it working in MacOS 12.5 by fixing some file permissions. using bash 5.1.16 in terminal type:

brew install bash
echo $BASH_VERSION
5.1.16(1)-release

chsh -s /opt/homebrew/Cellar/bash/5.1.16/bin/bash
brew install bash-completion@2
ls -la /opt/homebrew/etc/profile.d/bash_completion.sh

lrwxr-xr-x  1 myuser  admin  68 Aug  2 17:19 /opt/homebrew/etc/profile.d/bash_completion.sh -> ../../Cellar/bash-completion@2/2.11/etc/profile.d/bash_completion.sh

but the symbolic link target! had no executable flag so:

chmod +x /opt/homebrew/Cellar/bash-completion\@2/2.11/etc/profile.d/bash_completion.sh

then edit your ~/.bash_profile :

if [[ -r "/opt/homebrew/etc/profile.d/bash_completion.sh" ]]; then 
source /opt/homebrew/etc/profile.d/bash_completion.sh
echo "installed bash auto completions"
else
echo "Huston we have an auto compl problem"
fi

source <(kubectl completion bash)
alias k=kubectl
Sprit answered 2/8, 2022 at 15:5 Comment(0)
L
0

One reason auto-completion does not work could be that you use Bash 3.2 which is not compatible with any of the bash-completion versions.

The only way fix your auto-completion issue if you use Bash 3.2 is to upgrade your Bash version.

Warning: There are two versions of bash-completion, v1 and v2. V1 is for Bash 3.2 (which is the default on macOS), and v2 is for Bash 4.1+. The kubectl completion script doesn't work correctly with bash-completion v1 and Bash 3.2. It requires bash-completion v2 and Bash 4.1+. Thus, to be able to correctly use kubectl completion on macOS, you have to install and use Bash 4.1+ (instructions). The following instructions assume that you use Bash 4.1+ (that is, any Bash version of 4.1 or newer). Source: https://kubernetes.io/docs/tasks/tools/install-kubectl-macos/

Fixing the issue with

$ kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl

will work only if you've installed bash-completion via other means.

Lonnalonnard answered 2/5 at 17:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.