Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Kuzmenko-Pavel/825f728b5d845383b286dc87062b4b53 to your computer and use it in GitHub Desktop.

Select an option

Save Kuzmenko-Pavel/825f728b5d845383b286dc87062b4b53 to your computer and use it in GitHub Desktop.
Check unused k8s secrets
envSecrets=$(kubectl get pods -o jsonpath='{.items[*].spec.containers[*].env[*].valueFrom.secretKeyRef.name}' | xargs -n1)
envSecrets2=$(kubectl get pods -o jsonpath='{.items[*].spec.containers[*].envFrom[*].secretRef.name}' | xargs -n1)
volumeSecrets=$(kubectl get pods -o jsonpath='{.items[*].spec.volumes[*].secret.secretName}' | xargs -n1)
pullSecrets=$(kubectl get pods -o jsonpath='{.items[*].spec.imagePullSecrets[*].name}' | xargs -n1)
tlsSecrets=$(kubectl get ingress -o jsonpath='{.items[*].spec.tls[*].secretName}' | xargs -n1)
SASecrets=$(kubectl get secrets --field-selector=type=kubernetes.io/service-account-token -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' | xargs -n1)
diff \
<(echo "$envSecrets\n$envSecrets2\n$volumeSecrets\n$pullSecrets\n$tlsSecrets\n$SASecrets" | sort | uniq) \
<(kubectl get secrets -o jsonpath='{.items[*].metadata.name}' | xargs -n1 | sort | uniq)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment