Skip to content

Instantly share code, notes, and snippets.

@dhananjaipai
Last active May 22, 2020 05:40
Show Gist options
  • Save dhananjaipai/c73d21fd9d04aceb14cddad78bc9023d to your computer and use it in GitHub Desktop.
Save dhananjaipai/c73d21fd9d04aceb14cddad78bc9023d to your computer and use it in GitHub Desktop.
## Pass the secret name as argument
## Example usage to restart all pods: ./FindDependentPods.sh postgres-credentials | xargs -n1 kubectl delete pod
SECRET_NAME=$1
# The code below finds all pods that depend on a specific secret for environment variables or volume mounts
containerEnvFrom=$(kubectl get pods \
-o jsonpath='{range .items[*]}{.metadata.name}{" |"}{.spec.containers[*].envFrom[*].secretRef.name}{"\n"}' \
| grep -w "|${SECRET_NAME}" \
| awk 'BEGIN { FS="|" }{ print $1 }')
containerEnvValueFrom=$(kubectl get pods \
-o jsonpath='{range .items[*]}{.metadata.name}{" |"}{.spec.containers[*].env[*].valueFrom.secretKeyRef.name}{"\n"}' \
| grep -w "|${SECRET_NAME}" \
| awk 'BEGIN { FS="|" }{ print $1 }')
containerVolumeSecret=$(kubectl get pods \
-o jsonpath='{range .items[*]}{.metadata.name}{" |"}{.spec.volumes[*].secret.secretName}{"\n"}' \
| grep -w "|${SECRET_NAME}" \
| awk 'BEGIN { FS=" |" }{ print $1 }')
echo $containerEnvFrom $containerEnvValueFrom $containerVolumeSecret | uniq | xargs -n1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment