Skip to content

Instantly share code, notes, and snippets.

@spmason
Created September 12, 2023 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spmason/bb9700c887520d12d5c4349ab8ed467a to your computer and use it in GitHub Desktop.
Save spmason/bb9700c887520d12d5c4349ab8ed467a to your computer and use it in GitHub Desktop.
Kubernetes helper scripts
#!/bin/bash
#
# A generic command that makes it easy to run k9s against a specific cluster
# - without messing around with contexts (like kubectx does), eg:
#
# $ k9 prod-cluster get pods
#
# See the `kube` script alongside this one for more info (it's exactly the same except the final command is different)
#
set -ue -o pipefail
CLUSTER=$1
shift
CLUSTER_KUBECONFIG="$HOME/.kube/config.${CLUSTER}"
if ! [ -f $CLUSTER_KUBECONFIG ]; then
echo "current-context: ${CLUSTER}" > $CLUSTER_KUBECONFIG
fi
export KUBECONFIG=${CLUSTER_KUBECONFIG}:$HOME/.kube/config
exec k9s $@
#!/bin/bash
#
# A generic command that makes it easy to run kubectl commands against a specific cluster
# - without messing around with contexts (like kubectx does), eg:
#
# $ kube prod-cluster get pods
#
# vs the complicated way, which makes it less expicit which cluster your commands will run against!
#
# $ kube set-context prod-cluster
# $ kubectl get pods
#
# It just works by looking for a ~/.kube/config.${1} kubernetes config,
# then telling kubectl to "merge" that with its defalt config file
#
# It'll create a config file if one does not exist, assuming the context name is as given
# you can also just create your own and do things like override the default namespace etc
#
set -ue -o pipefail
CLUSTER=$1
shift
# Each cluster has its own config file
CLUSTER_KUBECONFIG="$HOME/.kube/config.${CLUSTER}"
if ! [ -f $CLUSTER_KUBECONFIG ]; then
# All the config file does is override the current kubectl context
# You can edit this file to add a default namespace too, if desired
echo "current-context: ${CLUSTER}" > $CLUSTER_KUBECONFIG
fi
export KUBECONFIG=${CLUSTER_KUBECONFIG}:$HOME/.kube/config
exec kubectl $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment