Introduction
Almost everything tech related I do on my computers revolves around my terminal. So naturally I have spent quite some time setting up my terminal the way I want it to with auto-completing, aliases and what not to optimize my workflow. And I must admit, it never stops evolving. Which is one of the beautiful things with having your own personalized CLI environment.
Why workflow matters
There are alot of tools, products, APIs, IDEs, you name it, I as a cloud engineer come across. A workflow is something personal which you both grow into and enhance with the experience you gain in your technical adventures.
Something I have learned over the years is that everybody has their own preferences on how to interact with all these systems. Some like the GUI, some like CLI tools, some do a mix. My preference lies between the two.
My workflow revolves heavily around Kubernetes and Azure nowadays and I find it tedious to nagivate between subscriptions, namespaces and clusters because it takes away valuable time and made me have to context switch to write long CLI commands. This is however no hard problem to fix and well worth the (small) investment.
I have created three functions in my .zshrc
which solved that particular problem for me. They allow me to navigate between the aforementioned systems/components through a UI without leaving my terminal (the perfect mix). I wanted to share them in this post as a reminder to myself but also for the sake of sharing.
Pre-requisites
To make the following functions work I had to install the following packages
fzf
jq
Now to the functions as hand.
1. swctx
function swctx {
kubectl config get-contexts -o name | fzf | xargs -I {} bash -c 'kubectl config use-context {}'
}
This function allows to be quickly navigate and switch kubeconfig contexts between different clusters. It works by listing existing contexts and piping the outcome to fzf, which allows me to fuzzy search. The outcome from fzf is then piped to kubectl again to set the context to a new cluster.
2. swns
function swns {
kubectl get namespace -o name | awk -F "/" '{print $2}'| fzf | xargs -I {} bash -c 'kubectl config set-context --current --namespace {}'
}
This function allows to be quickly navigate and switch between different k8s namespaces. By piping all existing namespaces in the current context to fzf, I can fuzzy search or orient myself to a new namespace with the arrow keys. Once a new namespace is marked it will modify the current kubeconfig context.
3. accset
function accset {
az account list -o json | jq -r '.[].name' | fzf | xargs -I {} bash -c 'az account set -s "{}"; az account show'
}
Similar to the ones above but for Azure subscriptions instead.
That is it,
Happy coding!