Skip to content

Instantly share code, notes, and snippets.

@JCMais
Last active March 11, 2019 14:38
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 JCMais/0db495b381b0ce4f5414e00e09a9043c to your computer and use it in GitHub Desktop.
Save JCMais/0db495b381b0ce4f5414e00e09a9043c to your computer and use it in GitHub Desktop.
I will use this keep notes of stuff I keep forgetting, and some aliases I use
### Just some aliases that can be added to your source
### some k8s ones were copied from https://gist.github.com/tuannvm/04487aec37d2056d8a4b4c4b9d53dd16#file-k8s-bashrc
# Get resources
# kget <pod|deployment|svc> <resource-name> <option>
kget() {
kubectl get $@
}
# Change context
# kctx <context-name>
kctx() {
kubectl config use-context $1
}
# Change namespace
# kns <namespace-name>
kns() {
kubectl config set-context $(kubectl config current-context) --namespace=$1
}
# Get current context
kcurrent() {
kubectl config current-context
}
# Delete resources
# kdel <pod|deployment|svc> <resource-name> <option>
kdel() {
kubectl delete $@
}
# Describe resources
# kdes <pod|deployment|svc> <resource-name> <option>
kdes() {
kubectl describe $@
}
# Exec by pod label
# Pod must have a single container
kexelb() {
kubectl exec -it $(kubectl get pod -l $1 -o jsonpath='{.items[0].metadata.name}') -- /bin/sh
}
# Get the value of a field in a secret
# kgetsecretfval <secret-name> <field-name>
kgetsecretfval() {
kubectl get secret $1 -o yaml | grep $2 -m 1 | awk '{print $2}' | base64 --decode
}
# Restart Deployment with 0 downtime
# Taken from: https://github.com/kubernetes/kubernetes/issues/27081#issuecomment-238078103
kresdeploy() {
kubectl patch deployment $1 -p "{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"date\":\"`date +'%s'`\"}}}}}"
}

Exit Script on Error

set -e

Set Defaults for Variable

ENV=${ENV:-development}

Compare if Variable Value Are in Array

dirs=('a' 'b' 'c')
if [[ ! " ${dirs[@]} " =~ " ${1} " ]]; then
  # do something
fi

Verify if Environment Variable is Defined

if [[ ! -z "$MYENV" ]]; then
  # $MYENV defined
fi

Replace All Environemnt Variables Inside File (Templating Using Bash)

function envsubst_config_file {
  if [[ ! -f "$1" ]]; then
    echo "File $1 not found"
    exit 1
  fi

  filename=$(basename -- "$1")
  filedir=$(dirname -- "$1")

  echo "Replacing envs in file $filename in dir $filedir"
  cat $1 | envsubst > $filedir/out/$filename
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment