bash, kubectl, helm の autocompletion(自動補完機能)について

6 min 688 views

kubectl や helm のサブコマンド候補を自動補完する方法

kubectl や helm コマンドはサブコマンドがたくさんあり、全てを正確に覚えておくのは大変です。サブコマンドを含めると入力内容も長くなるので、タイプミスも起こりがちです。そこで、コマンド入力中にサブコマンドの候補を表示したり、自動補完する autocompletion という機能の導入解説をします。

前提

  • 本記事で使用している OS は CentOS 8
  • helm は v3 を使用

bash-completion のインストール

はじめに bash-completion パッケージが OS に入っていない場合は、インストールします。

sudo dnf -y install bash-completion

kubectl コマンドのインストール

最新の kubectl バイナリファイルをダウンロードします。既に kubectl を導入済みの場合はこの手順をスキップしてください。

コマンドを実行するとカレントディレクトリに kubectl バイナリファイルがダウンロードされます。

sudo curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl"

ダウンロードしたファイルに実行権限を付与します。

sudo chmod +x ./kubectl

kubectl バイナリを PATH のいずれかに移動します。ここでは、/usr/local/bin の下に移動しています。

$ echo $PATH
/home/admin/.local/bin:/home/admin/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
$ sudo mv ./kubectl /usr/local/bin/

kubectl コマンドが使えるようになったか確認します。

kubectl version --client

kubectl の autocompletion を有効にする

以下のコマンドを実行して .bashrc に “kubectl completion bash” の行を追記します。

echo 'source <(kubectl completion bash)' >>~/.bashrc

上記を実行すると以下のように .bashrc ファイルに行が追加されます。

$ cat .bashrc | tail -n 3

# User specific aliases and functions
source <(kubectl completion bash)
$

.bashrc ファイルを再読み込みして設定を反映させます。

source ~/.bashrc

kubectl autocompletion の動作確認

kubectl コマンドの冒頭 “ku” を入力して [tab] ボタンを押します。そうすると、”kubectl” と入力されます。そこからさらに [tab] ボタンを 2回ほど押すと、kubectl コマンドのサブコマンド候補が以下のように表示されます。なお、各環境によって OS にインストールされているコマンド状況は異なるため、多少 autocompletion(自動補完)される条件が異なる可能があります。

$ kubectl 
alpha          auth           cordon         diff           get            patch          run            version
annotate       autoscale      cp             drain          help           plugin         scale          wait
api-resources  certificate    create         edit           kustomize      port-forward   set            
api-versions   cluster-info   debug          exec           label          proxy          taint          
apply          completion     delete         explain        logs           replace        top            
attach         config         describe       expose         options        rollout        uncordon       
$

helm コマンドのインストール

次に helm コマンドも自動補完を有効にします。helm コマンドを導入していない場合は、下記コマンドでインストールします。コマンドを実行するとカレントディレクトリに get_helm.sh というファイル名でスクリプトファイルがダウンロードされます。

curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3

次の手順で helm をインストールします。

$ chmod 700 get_helm.sh
$ ./get_helm.sh 
Downloading <https://get.helm.sh/helm-v3.8.0-linux-amd64.tar.gz>
Verifying checksum... Done.
Preparing to install helm into /usr/local/bin
helm installed into /usr/local/bin/helm
$

もしこの時に以下のようなエラーが出る場合は、メッセージ出力にあるとおり「$ sudo dnf install openssl」で openssl パッケージを入れるか、「VERIFY_CHECKSUM=false」を実行して回避してください。

$ ./get_helm.sh 
In order to verify checksum, openssl must first be installed.
Please install openssl or set VERIFY_CHECKSUM=false in your environment.
Failed to install helm
        For support, go to <https://github.com/helm/helm>.
$

helm autocompletion の有効化

helm コマンドをインストールしたら、下記コマンドで現在操作しているシェルに helm の自動補完機能を有効にすることができます。

source <(helm completion bash)

シェルを起動した時に毎回有効にするには、以下のコマンドを root ユーザーで実行します。

helm completion bash > /etc/bash_completion.d/helm

なお、手元の環境では sudo コマンドを付けても以下のように Permission denied となりました。関連フォルダやファイルをチェックしてパーミッションには問題がなく、原因が分かりませんでした。そのため、素直に root ユーザーになって上記コマンドを実行して回避しました。”helm completion bash” コマンドは、autocompletion 機能を提供するシェルの内容を出力するのみの動作になります。ここの手順では、その出力を /etc/bash_completion.d/helm というファイルに書き込むという内容になります。

$ sudo helm completion bash > /etc/bash_completion.d/helm 
-bash: /etc/bash_completion.d/helm: Permission denied

helm autocompletion の動作確認

helm と入力してから [tab] ボタンを 2回ほど押してみます。そうすると、helm コマンドのサブコマンド候補が以下のように出力されます。

helm 
completion  (generate autocompletion scripts for the specified shell)
create      (create a new chart with the given name)
dependency  (manage a chart's dependencies)
env         (helm client environment information)
get         (download extended information of a named release)
help        (Help about any command)
history     (fetch release history)
install     (install a chart)
lint        (examine a chart for possible issues)
list        (list releases)
package     (package a chart directory into a chart archive)
plugin      (install, list, or uninstall Helm plugins)
pull        (download a chart from a repository and (optionally) unpack it in local directory)
push        (push a chart to remote)
registry    (login to or logout from a registry)
repo        (add, list, remove, update, and index chart repositories)
rollback    (roll back a release to a previous revision)
search      (search for a keyword in charts)
show        (show information of a chart)
status      (display the status of the named release)
template    (locally render templates)
test        (run tests for a release)
uninstall   (uninstall a release)
upgrade     (upgrade a release)
verify      (verify that a chart at the given path has been signed and is valid)
version     (print the client version information)
#
関連記事