Skip to content

Instantly share code, notes, and snippets.

@pellizzetti
Created November 26, 2025 19:48
Show Gist options
  • Select an option

  • Save pellizzetti/ded16cf3e439f3b3fd73e98e2eb161a5 to your computer and use it in GitHub Desktop.

Select an option

Save pellizzetti/ded16cf3e439f3b3fd73e98e2eb161a5 to your computer and use it in GitHub Desktop.
PVC annotation fix
#!/usr/bin/env bash
# Requirements:
# 1. kubectl
# 2. jq
# While updating - or replacing - nodes, PVC annotation are kept for dead node.
# This makes it that some pods can't initialize because kubernetes
# can't attach a volume to it.
# This script clean the selected-node annotation from all PVC where the node
# doesn't exist, allowing for them be attached to new nodes.
declare -A nodes
while read node; do
nodes["${node#node/}"]=exists
done < <(kubectl get nodes -o name)
kubectl get pvc -A -o json |
jq '.items[].metadata | [.namespace, .name, .annotations["volume.kubernetes.io/selected-node"]] | @tsv' -r |
while read namespace name node; do
test -n "$node" || continue
if ! [[ ${nodes[$node]-} == "exists" ]]; then
kubectl annotate -n "${namespace}" "pvc/${name}" volume.kubernetes.io/selected-node-
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment