LoginSignup
1
1

More than 5 years have passed since last update.

kubectx/kubensにinteractive modeが追加されたよ

Posted at

はじめに

Kubernetesクラスタ自体の切り替えや、クラスタ内部のNamespaceの切り替えで活躍するkubectxkubensですが、
この度Interactive Modeが追加されました!

デモ

tty.gif

インストール

kubectx/kubensのインストール

sudo git clone https://github.com/ahmetb/kubectx /opt/kubectx
sudo ln -s /opt/kubectx/kubectx /usr/local/bin/kubectx
sudo ln -s /opt/kubectx/kubens /usr/local/bin/kubens

fzfのインストール

Interactive Modeは内部でfzfを利用しているため、fzfをインストールします。
https://github.com/junegunn/fzf-bin/releases
にバイナリがあるので、ダウンロードしてPATHが通るところに配置してください。

Interactive Modeの仕組み

kubectxの中身を覗いたところ、main処理の先頭でfzfがあるかどうかで分岐をさせているようです。

# main
# 略
if [[ -t 1 && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then
  choose_context_interactive
else
  list_contexts
fi
# 略

fzfがある場合は、下記のchoose_context_interactiveでInteractive Modeに。

# choose_context_interactive
choose_context_interactive() {
  local choice
  choice="$(FZF_DEFAULT_COMMAND='kubectx' fzf --ansi || true)"
  if [[ -z "${choice}" ]]; then
    echo 2>&1 "error: you did not choose any of the options"
    exit 1
  else
    set_context "${choice}"
  fi
}

それではよきk8sライフを!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1