Why Use Aliases?
I’m constantly immersed in Kubernetes tasks throughout the day. Whether it’s troubleshooting or conducting routine checks, one of the biggest challenges I face is the need to repeatedly type lengthy commands. It got me thinking about how we can streamline this process and minimize the need for typing out these complex commands over and over again. Sure, creating aliases in the shell is an option, but is there more we can do to enhance usability?
That’s when I stumbled upon a fantastic tool called fuzzy finder. Fuzzy finder is an interactive Unix filter for the command-line that can be applied to a wide range of lists, including files, command history, processes, hostnames, bookmarks, git commits, and more. If you’re interested in setting up Bash with ‘oh-my-posh’ and installing ‘fzf,’ I’ve detailed the steps in one of my previous posts.
By harnessing the power of ‘fzf,’ we can take our aliases to the next level and seamlessly pass inputs to commands using simple arrow keys or mouse clicks. Below, you’ll find a selection of aliases that I personally use and find incredibly helpful.
Kubernetes Aliases
Get the yaml formatted output of any resource.
Usage:
getyaml <resource_type>
1
alias getyaml='function get_k8s_yaml() { kubectl get $1 -o=json | jq -r ".items[].metadata.name" | fzf --preview "kubectl get $1 {} -o=yaml" --preview-window=up:60% | xargs -I {} sh -c "kubectl get $1 {} -o=yaml | yq ."; }; get_k8s_yaml'
Get the json formatted output of any resource.
Usage:
getjson <resource_type>
1
alias getjson='function get_k8s_json() { kubectl get $1 -o=json | jq -r ".items[].metadata.name" | fzf --preview "kubectl get $1 {} -o=json | jq ." --preview-window=up:60% | xargs -I {} sh -c "kubectl get $1 {} -o=json | jq ."; }; get_k8s_json'
Describe the resource
Usage:
describe <resource_type>
1
alias describe='function describe_k8s_resource() { local resource_type="$1"; kubectl get ${resource_type} -o=json | jq -r ".items[].metadata.name" | fzf --preview "kubectl describe ${resource_type} {}" --preview-window=up:60% | xargs -I {} sh -c "kubectl describe ${resource_type} {} | tee /dev/tty"; }; describe_k8s_resource'
Get particular field of inputted resource
Usage:
getfield <resource_type> <field_name>
1 2 3 4 5 6 7 8 9 10 11 12 13
alias getfield='function get_k8s_field() { local k8s_object="$1" local field="$2" local selected_item selected_item=$(kubectl get "$k8s_object" -o=json | jq -r ".items[].metadata.name" | fzf) if [ -n "$selected_item" ]; then echo "$k8s_object: $selected_item" local field_value field_value=$(kubectl get "$k8s_object" "$selected_item" -o=json | jq -r ".$field") echo "$field: $field_value" fi } get_k8s_field'
Get logs of inputted container
Usage:
getlogs
1
alias getlogs='selected_pod=$(kubectl get pods --no-headers | fzf --preview-window=up:30%:wrap --header="Select a Pod to View Logs" | awk "{print \$1}"); if [ -n "$selected_pod" ]; then selected_container=$(kubectl get pod $selected_pod -o jsonpath="{.spec.containers[*].name}" | tr " " "\n" | fzf --preview "kubectl logs $selected_pod -c {}" --preview-window=up:30%:wrap --header="Select a Container" | awk "{print \$1}"); if [ -n "$selected_container" ]; then kubectl logs $selected_pod -c $selected_container; fi; fi'
Show events on a namespace sorted by time.
1
events='kubectl get events --sort-by=.lastTimestamp'
Show all resources available in a namespace
1
alias kresources='kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get --show-kind --ignore-not-found'
FluxCD Aliases
List all Flux sources
1
alias src='flux get source all -A 2>/dev/null'
List all Kustomizations
1
alias ks='flux get ks -A 2>/dev/null'
List all HelmReleases
1
alias hr='flux get hr -A 2>/dev/null'
Kustomization Reconciliation
1
alias ksrec='NAMESPACE_KSNAME=$(flux get ks -A 2>/dev/null | tail -n +2 | grep -v ^$ | awk "{print \$1\"/\"\$2}" | fzf); if [[ ! -z $NAMESPACE_KSNAME ]]; then NAMESPACE=$(NAMESPACE_KSNAME | cut -d "/" -f 1); KSNAME=$(echo $NAMESPACE_KSNAME | cut -d "/" -f 2); flux reconcile kustomization --namespace $NAMESPACE $KSNAME --with-source; fi'
HelmRelease Reconciliation
1
alias hrrec='NAMESPACE_HRNAME=$(flux get hr -A 2>/dev/null | tail -n +2 | grep -v ^$ | awk "{print \$1\"/\"\$2}" | fzf); if [[ ! -z $NAMESPACE_HRNAME ]]; then NAMESPACE=$(NAMESPACE_HRNAME | cut -d "/" -f 1); HRNAME=$(echo $NAMESPACE_HRNAME | cut -d "/" -f 2); flux reconcile helmrelease --namespace $NAMESPACE $KSNAME --with-source; fi'
Git Source Reconciliation
1
alias srcrecgit='NAMESPACE_SRCNAME=$(flux get source git -A 2>/dev/null | tail -n +2 | grep -v ^$ | awk "{print \$1\"/\"\$2}" | fzf); if [[ ! -z $NAMESPACE_SRCNAME ]]; then NAMESPACE=$(NAMESPACE_SRCNAME | cut -d "/" -f 1); SRCNAME=$(echo $NAMESPACE_SRCNAME | cut -d "/" -f 2); flux reconcile source git --namespace $NAMESPACE $SRCNAME; fi'
Helm Source Reconciliation
1
alias srcrechelm='NAMESPACE_SRCNAME=$(flux get source helm -A 2>/dev/null | tail -n +2 | grep -v ^$ | awk "{print \$1\"/\"\$2}" | fzf); if [[ ! -z $NAMESPACE_SRCNAME ]]; then NAMESPACE=$(NAMESPACE_SRCNAME | cut -d "/" -f 1); SRCNAME=$(echo $NAMESPACE_SRCNAME | cut -d "/" -f 2); flux reconcile helmrelease --namespace $NAMESPACE $SRCNAME; fi'
Git Aliases
Switch between repositories
1
alias repo='cd ~/github/$(ls ~/github | grep ^ | fzf)'
Switch between branches
1
alias branch='branch=$(git branch -a | fzf) && if [[ $branch == *"origin"* ]]; then git switch --track $branch; else git switch $branch; fi'