Netflix platform interview: How would you build a Python Kubernetes watcher that detects CrashLoopBackOff pods and creates a structured incident event without missing updates or hammering the API server?
Quick Answer
Use the official Kubernetes Python client, load in-cluster or kubeconfig credentials, list with resourceVersion, then watch pod events with timeouts and reconnect logic. Filter pod container statuses for waiting.reason equals CrashLoopBackOff, deduplicate by namespace/name/container/restart count, and emit structured events instead of polling every few seconds.
Detailed Answer
A Kubernetes watcher is like a security desk watching badge events at a large office. Asking for the full list every five seconds is wasteful and still misses context. Subscribing to a stream and remembering the last seen position is cheaper and more accurate. That is the mental model interviewers want when they ask about Kubernetes automation in Python.
The official Kubernetes Python client exposes CoreV1Api and watch.Watch. A production watcher usually starts with a list_namespaced_pod or list_pod_for_all_namespaces call to get the current resourceVersion, then uses watch.stream against the same list function. The resourceVersion is important because Kubernetes objects are versioned; it lets the watcher resume from a known point instead of starting from scratch every time.
CrashLoopBackOff is not a pod phase. It appears in container status under state.waiting.reason, often alongside restartCount and lastState. Good code inspects initContainerStatuses and containerStatuses, records the exact container, and includes namespace, pod name, node, image, restart count, and last termination reason. It deduplicates so one broken pod does not create 500 Slack messages.
At scale, the watcher must respect API server limits. Use watch timeouts so the HTTP stream refreshes periodically. Handle 410 Gone by relisting because the resourceVersion is too old. Avoid one watcher per namespace unless needed; a single all-namespaces watch is usually simpler. Emit metrics for reconnects, event lag, and handler errors so the automation itself is observable.
The non-obvious gotcha is RBAC and blast radius. A watcher running in-cluster should use a ServiceAccount with list/watch on pods only, not cluster-admin. If it creates incident objects, write those to a narrow namespace or external queue, not back into every application namespace.