Skip to content

Instantly share code, notes, and snippets.

@DrPsychick
Last active April 19, 2021 17:36
Show Gist options
  • Save DrPsychick/656300fc98198558a940b447ca914ba0 to your computer and use it in GitHub Desktop.
Save DrPsychick/656300fc98198558a940b447ca914ba0 to your computer and use it in GitHub Desktop.
Generic shell functions for common k8s commands
## some generic functions based on `app.kubernetes.io/instance` label
# list pods including instance label
function k8s.pod() {
kubectl get pod -L app.kubernetes.io/instance
}
# list deployments including instance label
function k8s.deployment() {
kubectl get deployment -L app.kubernetes.io/instance
}
# get service pod name by instance
function k8s.instance.servicePod() {
[ -z "$1" ] && {
echo "Usage: ${FUNCNAME[0]} <app.kubernetes.io/instance>"
return
}
kubectl get pod -l app.kubernetes.io/instance=$1 -o jsonpath="{ .items[0].metadata.name }"
}
# get deployment name by instance
function k8s.instance.deployment() {
[ -z "$1" ] && {
echo "Usage: ${FUNCNAME[0]} <app.kubernetes.io/instance>"
return
}
kubectl get deployment -l app.kubernetes.io/instance=$1 -o jsonpath="{ .items[0].metadata.name }"
}
# get replicaset name by instance
function k8s.instance.replicaset() {
[ -z "$1" ] && {
echo "Usage: ${FUNCNAME[0]} <app.kubernetes.io/instance>"
return
}
kubectl get replicaset -l app.kubernetes.io/instance=$1 -o jsonpath="{ .items[0].metadata.name }"
}
# tail logs of a single pod by instance
function k8s.instance.tailLogs() {
[ -z "$1" ] && {
echo "Usage: ${FUNCNAME[0]} <instance>"
return
}
kubectl logs --tail 500 -f $(k8s.instance.servicePod "$1")
}
# scale replicaset by instance
function k8s.instance.scale() {
[ -z "$2" ] && {
echo "Usage: ${FUNCNAME[0]} <instance> <replicas>"
return
}
echo "Scaling $(k8s.instance.replicaset "$1") to $2"
kubectl scale replicaset/$(k8s.instance.replicaset "$1") --replicas $2
}
# scale deployment to 0 = stop, 1 = start
function k8s.instance.stop() {
[ -z "$1" ] && {
echo "Usage: ${FUNCNAME[0]} <instance>"
return
}
echo "Scaling $(k8s.instance.deployment "$1") to $2"
kubectl scale deployment/$(k8s.instance.deployment "$1") --replicas 0
}
function k8s.instance.start() {
[ -z "$1" ] && {
echo "Usage: ${FUNCNAME[0]} <instance>"
return
}
echo "Scaling $(k8s.instance.deployment "$1") to $2"
kubectl scale deployment/$(k8s.instance.deployment "$1") --replicas 1
}
# get variables of first container
function k8s.instance.variable() {
[ -z "$1" ] && {
echo "Usage: ${FUNCNAME[0]} <instance>"
return
}
kubectl get deployment $(k8s.instance.deployment "$1") \
-o jsonpath="{ .spec.template.spec.containers[0].env }"
}
# set variable to given value
function k8s.instance.variable.set() {
[ -z "$3" ] && {
echo "Usage: ${FUNCNAME[0]} <instance> <variable_name> <variable_value>"
return
}
index=$(kubectl get deployment $(k8s.instance.deployment "$1") -o json \
| jq '.spec.template.spec.containers[].env|map(.name == "'$2'")|index(true)')
[ "null" == "$index" ] && {
echo "Error: variable '$2' not found"
return
}
kubectl patch deployment $(k8s.instance.deployment "$1") --type=json \
-p='[{"op":"replace", "path":"/spec/template/spec/containers/0/env/'$index'/value", "value": "'$3'"}]'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment