Skip to content

Instantly share code, notes, and snippets.

@nshores
Created May 28, 2025 18:52
Show Gist options
  • Save nshores/888f61b37a9e538cd2d032186dd19172 to your computer and use it in GitHub Desktop.
Save nshores/888f61b37a9e538cd2d032186dd19172 to your computer and use it in GitHub Desktop.
generate_k8s_diagram.sh
#!/bin/sh
# Configuration
namespace="bitwarden"
label="app.kubernetes.io/name=self-host"
output_file="bitwarden-diagram.png"
# Generate YAML for all namespaced resources with the given label
# and pipe them into kube-diagrams for visualization.
{
for resource in $(kubectl api-resources --namespaced -o name); do
# Skip endpoints and endpointslices
if [ "$resource" = "endpoints" ] || [ "$resource" = "endpointslices" ] || [ "$resource" = "replicasets" ] || [ "$resource" = "replicaset" ] || [ "$resource" = "replicasets.apps" ] || [ "$resource" = "endpointslices.discovery.k8s.io" ]; then
continue
fi
count=$(kubectl get "$resource" -n "$namespace" -l "$label" -o json 2>/dev/null | jq '.items | length')
if [ "$count" -gt 0 ]; then
echo "# Resource: $resource" >&2
kubectl get "$resource" -n "$namespace" -l "$label" -o yaml
echo "---"
fi
done
} | /opt/homebrew/bin/kube-diagrams -o "$output_file" -
echo "✅ Diagram saved as $output_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment