50 lines
2.0 KiB
Python
50 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Extract bounded pod status and events without dumping pod specs or secrets."""
|
|
import json
|
|
import re
|
|
import sys
|
|
|
|
SECRET = re.compile(r"(?i)(bearer\s+|(?:token|password|secret|api[_-]?key)\s*[=:]\s*)([^\s|,]+)")
|
|
|
|
|
|
def clean(value):
|
|
return SECRET.sub(r"\1[REDACTED]", str(value)).replace("\n", " ").replace("\t", " ")[:500]
|
|
|
|
|
|
def main():
|
|
data = json.load(sys.stdin)
|
|
items = data.get("items")
|
|
if not isinstance(items, list):
|
|
raise ValueError("missing items")
|
|
mode = sys.argv[1]
|
|
if mode == "names":
|
|
for pod in items[:5]:
|
|
name = (pod.get("metadata") or {}).get("name", "")
|
|
if re.fullmatch(r"[a-z0-9]([-a-z0-9.]*[a-z0-9])?", name):
|
|
print(name)
|
|
elif mode == "pods":
|
|
if not items:
|
|
print("No pods matched the deployment selector.")
|
|
for pod in items[:5]:
|
|
status = pod.get("status") or {}
|
|
print("pod", clean((pod.get("metadata") or {}).get("name", "unknown")), "phase=" + clean(status.get("phase", "unknown")))
|
|
for container in (status.get("initContainerStatuses") or []) + (status.get("containerStatuses") or []):
|
|
state = container.get("state") or {}
|
|
current = state.get("waiting") or state.get("terminated") or {}
|
|
if current:
|
|
print("container", clean(container.get("name", "unknown")), "reason=" + clean(current.get("reason", "unknown")),
|
|
"message=" + clean(current.get("message", "")))
|
|
elif mode == "events":
|
|
for event in items[-8:]:
|
|
print("event", clean(event.get("type", "unknown")), clean(event.get("reason", "unknown")), clean(event.get("message", "")))
|
|
else:
|
|
raise ValueError("unknown mode")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except (ValueError, TypeError, KeyError, IndexError, AttributeError) as exc:
|
|
print(f"Pod evidence unavailable: {type(exc).__name__}", file=sys.stderr)
|
|
sys.exit(1)
|