Skip to content

Instantly share code, notes, and snippets.

@juan131
Created March 11, 2021 09:38
Show Gist options
  • Save juan131/f3cb8022114cc63f68b3ff88737bda35 to your computer and use it in GitHub Desktop.
Save juan131/f3cb8022114cc63f68b3ff88737bda35 to your computer and use it in GitHub Desktop.
Simple script useful to inspect exiting claims on Kubernetes
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
# Constants
RESET='\033[0m'
GREEN='\033[38;5;2m'
RED='\033[38;5;1m'
YELLOW='\033[38;5;3m'
# Axiliar functions
########################
# Log message to stderr
# Arguments:
# $1 - Message to log
#########################
log() {
printf "%b\n" "${*}" >&2
}
########################
# Log info message
# Arguments:
# $1 - Message to log
#########################
info() {
log "${GREEN}INFO ${RESET} ==> ${*}"
}
########################
# Log error message
# Arguments:
# $1 - Message to log
#########################
error() {
log "${RED}ERROR ${RESET} ==> ${*}"
}
########################
# Print menu
#########################
print_menu() {
local script
script=$(basename "${BASH_SOURCE[0]}")
log "${RED}NAME${RESET}"
log " $(basename -s .sh "${BASH_SOURCE[0]}")"
log ""
log "${RED}SYNOPSIS${RESET}"
log " $script [${YELLOW}-dh${RESET}] [${YELLOW}-n ${GREEN}\"namespace\"${RESET}] [${YELLOW}-c ${GREEN}\"claim-name\"${RESET}]"
log ""
log "${RED}DESCRIPTION${RESET}"
log " Script to create a single-container POD on your K8s cluster to inspect existing PVCs."
log ""
log " The options are as follow:"
log ""
log " ${YELLOW}-n, --namespace ${GREEN}[namespace]${RESET} Namespace where te create the POD."
log " ${YELLOW}-c, --claim${RESET} Name of the existing PVC to inspect (can be set several times)."
log " ${YELLOW}-h, --help${RESET} Print this help menu."
log " ${YELLOW}-u, --dry-run${RESET} Enable \"dry run\" mode."
log ""
log "${RED}EXAMPLES${RESET}"
log " $script --help"
log " $script --namespace \"default\" --claim \"claim-0\""
log ""
}
namespace="default"
claims=()
help_menu=0
dry_run=0
while [[ "$#" -gt 0 ]]; do
case "$1" in
-h|--help)
help_menu=1
;;
-u|--dry-run)
dry_run=1
;;
-n|--namespace)
shift; namespace="${1:?missing namespace}"
;;
-c|--claim)
shift; claims+=("${1:?missing claim}")
;;
*)
error "Invalid command line flag $1" >&2
exit 1
;;
esac
shift
done
if [[ ${#claims[@]} -eq 0 ]]; then
help_menu=1
error "No PVC(s) were indicated"
fi
if [[ "$help_menu" -eq 1 ]] ; then
print_menu
exit 0
fi
used_pvcs() {
kubectl get pods -n "$namespace" -o json | jq -c '.items[] | {claimName: .spec | select( has ("volumes") ).volumes[] | select( has ("persistentVolumeClaim") ).persistentVolumeClaim.claimName }' | sort -u
}
for c in "${claims[@]}"; do
if ! kubectl get pvc -n "$namespace" "$c" >/dev/null 2>&1; then
error "PVC $c not found"
exit 1
fi
if [[ "$(kubectl get pvc -n "$namespace" "$c" -o json | jq '.status.accessModes[]')" = *"ReadWriteOnce"* ]] && [[ "$(used_pvcs)" = *"$c"* ]]; then
error "PVC $c has ReadWriteOnce access mode and it's already in use"
exit 1
fi
done
pod_template="(mktemp)"
cleanup() {
rm "$pod_template"
}
trap cleanup EXIT
cat > "$pod_template" << EOF
apiVersion: v1
kind: Pod
metadata:
name: claim-inspector
namespace: $namespace
spec:
restartPolicy: Never
containers:
- image: docker.io/bitnami/bitnami-shell:latest
name: inspector
command:
- sleep
- infinity
volumeMounts:
EOF
for c in "${claims[@]}"; do
cat >> "$pod_template" << EOF
- mountPath: /vol/${c}
name: pv-${c}
EOF
done
cat >> "$pod_template" << EOF
volumes:
EOF
for c in "${claims[@]}"; do
cat >> "$pod_template" << EOF
- name: pv-${c}
persistentVolumeClaim:
claimName: ${c}
EOF
done
if [[ "$dry_run" -eq 1 ]]; then
info "DRY RUN mode enabled!"
info "Namespace: $namespace"
info "Generated POD manifest:"
cat "$pod_template"
exit 0
fi
info "Creating inspector POD"
if kubectl get pods -n "$namespace" claim-inspector >/dev/null 2>&1; then
error "There's an claim inspector POD already running"
exit 1
fi
if ! kubectl create -f "$pod_template" >/dev/null 2>&1; then
error "POD creating failed"
exit 1
fi
info "POD successfully created!"
info "Use the command below to access the container:"
log ""
log " kubectl exec -n $namespace -it claim-inspector -c inspector -- bash"
log ""
info "Then, inspect the data in the volumes mounted at \"/vol\":"
log ""
log " ls -la /vol"
log ""
info "Remember to delete the POD by running the command below once it's no needed anymore:"
log ""
log " kubectl delete pod -n $namespace claim-inspector"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment