Skip to content

Instantly share code, notes, and snippets.

@weibeld
Created September 8, 2022 12:28
Show Gist options
  • Save weibeld/020b8ffdd1c2f9bf72bf6a53728e2f95 to your computer and use it in GitHub Desktop.
Save weibeld/020b8ffdd1c2f9bf72bf6a53728e2f95 to your computer and use it in GitHub Desktop.
Check user of all containers in a Kubernetes cluster
#!/bin/bash
kubectl get pods --all-namespaces -o 'custom-columns=:.metadata.namespace,:.metadata.name,:.spec.initContainers[*].name' |
while read line; do
namespace=$(awk '{print $1}' <<<"$line")
pod=$(awk '{print $2}' <<<"$line")
containers=($(awk '{print $3}' <<<"$line" | tr , ' '))
for container in "${containers[@]}"; do
echo -n "$namespace | $pod | $container: "
kubectl exec "$pod" -n "$namespace" -c "$container" -- whoami 2>/dev/null || echo "<unknown>"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment