为什么 kubectl bash 补全功能在 macOS/OS X 上不起作用?

问题描述 投票:0回答:8

我按照

kubectl completion -h
:

给出的安装 Bash 补全的说明进行操作
  1. 我通过 Homebrew 安装了
    bash-completion
  2. 在我的
  3. ~/.bashrc
     中,我首先获取 
    bash-completion
    ,然后从完成 kubectl 子命令输出:
    
    
    • source $(brew --prefix)/etc/bash_completion
      
      
    • source <(kubectl completion bash)
      
      
完成这些后,我启动了一个新的 shell,但完成不起作用。我怎样才能让它工作?

bash kubernetes homebrew bash-completion kubectl
8个回答
10
投票
一旦

bash-completion

 由 Homebrew 安装,看起来它的完成
需要居住在
$(brew --prefix)/etc/bash_completion.d
。在那里您会发现许多其他捆绑的完成品。添加 kubectl 的补全:

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

这对我有用。


2
投票
我是 Ahmet B 的答案,修复说将以下内容添加到您的 .bashrc 文件中:

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"

但是完成2的安装:

brew install bash-completion@2

如果您想使用 V1 补全,则会显示一条消息以添加导出行。删除该导出为我启用了 kubectl 完成功能。


1
投票
上述答案对我不起作用,但我找到了

this解决方案:

source /dev/stdin <<<"$(kubectl completion bash)"
    

1
投票
这是另一种方法:

安装 bash 和 bash-completion

brew install bash bash-completion

设置终端使用 bash

Preferences

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

/opt/homebrew/bin/bash

检查 bash-completion 已安装到的位置

brew info bash-completion

Bash补全已安装到: /opt/homebrew/etc/bash_completion.d

编辑~/.bashrc

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

重新加载终端并验证是否完成工作

~$ k <TAB>

alpha auth cordon diff 获取补丁运行版本 注释自动缩放 cp ...

环境

    macOS 蒙特利 12.2.1
  • 自制软件 3.3.15
  • GNU bash,版本 5.1.16(1)-发布 (aarch64-apple-darwin21.1.0)
  • bash-completion:稳定 1.3(瓶装)
  • kubectl v1.23.3

0
投票
请参阅 kubectl 文档的“在 macOS 上,使用 bash”部分:

https://kubernetes.io/docs/tasks/tools/install-kubectl/#on-macos-using-bash 我最近贡献了这些,所以它们应该是最新的。如果没有,请发送拉取请求来修复它。

另外:

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


0
投票
  1. brew install bash-completion
    之后,要真正启用 bash 补全,您需要:
    
    
    source /usr/local/etc/profile.d/bash_completion.sh
    
    添加该行给您
    bashrc
  2. 然后您可以:
  3. source <(kubectl completion bash)
    
    

0
投票
通过修复一些文件权限,设法让它在 MacOS 12.5 中工作。使用 bash 5.1.16 在终端类型中:

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
但是符号链接目标!没有可执行标志,所以:

chmod +x /opt/homebrew/Cellar/bash-completion\@2/2.11/etc/profile.d/bash_completion.sh
然后编辑您的 

~/.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
    

0
投票
自动完成功能不起作用的一个原因可能是您使用的 Bash 3.2 与任何 bash 完成版本都不兼容。

如果您使用 Bash 3.2,解决自动完成问题的唯一方法是升级您的 Bash 版本。

警告:bash-completion 有两个版本:v1 和 v2。 V1 适用于 Bash 3.2(这是 macOS 上的默认版本),v2 适用于 Bash 4.1+。 kubectl 完成脚本无法与 bash-completion v1 和 Bash 3.2 一起正常工作。它需要 bash-completion v2 和 Bash 4.1+。因此,为了能够在 macOS 上正确使用 kubectl 补全,您必须安装并使用 Bash 4.1+(说明)。以下说明假设您使用 Bash 4.1+(即 4.1 或更高版本的任何 Bash 版本)。 来源:

https://kubernetes.io/docs/tasks/tools/install-kubectl-macos/

解决问题

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

仅当您通过其他方式安装了 bash-completion 时才有效。

© www.soinside.com 2019 - 2024. All rights reserved.