Last active
May 22, 2020 05:40
-
-
Save dhananjaipai/c73d21fd9d04aceb14cddad78bc9023d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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