Help us understand the problem. What is going on with this article?

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

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
}
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away