1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

kubeswitch: kubectl でcontextをラクに切り替える

Last updated at Posted at 2018-07-31

kubectlでたくさんのクラスタを扱うと、contextをいちいち切り替えるのが大変なので、
簡単なコンテキストスイッチャーをBashコマンドにしました。
下記のやり方はOSXで検証していますが、Linux系でも問題なく動くかと思います。

使い方

tty.gif

Requirements

事前にpecoのインストールが必要です。

osx

brew install peco

実装

下記のコードを、~/.bash_profile などに追加すれば使えます。

~/.bash_profile
function kubeswitch() {
	kcontext=$(kubectl config get-contexts  | peco --initial-index=1 --prompt='kubectl config use-context > ' |  sed -e 's/^\*//' | awk '{print $1}')
	if [ -n "$kcontext" ]; then
		kubectl config use-context $kcontext
	fi
}

亜種: gcloudswitch

gcloudswitch () {
	project=$(gcloud projects list   | peco --initial-index=1 --prompt='gcloud config set project > ' |  sed -e 's/^\*//' | awk '{print $1}')
	if [ -n "$project" ]; then
		echo "Switch to ${project}."
        gcloud config set project $project
    fi
}

亜種2: awswitch

awswitch () {
    choice=$(aws configure list-profiles | peco --initial-index=1 --prompt='Choose profile' | awk '{print $1}')
    if [ -n "$choice" ]; then
		export AWS_PROFILE=$choice
    fi
}
1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?