69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216 | def classify_pod(pod: dict, events: list[dict]) -> list[DiagnosticFinding]:
"""Classify observed pod/container states and events; never inspect logs or secrets."""
name = pod["metadata"]["name"]
status = pod.get("status") or {}
findings: dict[tuple[str, str | None], DiagnosticFinding] = {}
def add(code, summary, evidence, remediation, container=None, severity="error"):
findings.setdefault(
(code, container),
DiagnosticFinding(
code,
severity,
name,
summary,
_evidence(evidence),
remediation,
container,
),
)
# The kubelet reports an elapsed activeDeadlineSeconds (max_runtime) on the pod, not on a container.
if status.get("reason") == "DeadlineExceeded":
add(
"deadline_exceeded",
"Pod exceeded its maximum runtime and was stopped by Kubernetes",
status.get("message") or "DeadlineExceeded",
"The pod's activeDeadlineSeconds (max_runtime) elapsed. Check driver logs for stalled or "
"slow work before raising max_runtime; outputs written before the stop may be partial.",
)
waiting_containers = []
for container in (status.get("init_container_statuses") or []) + (status.get("container_statuses") or []):
container_name = container.get("name")
current = container.get("state") or {}
waiting = current.get("waiting") or {}
reason = waiting.get("reason")
if reason and reason != "ContainerCreating":
waiting_containers.append(
f"container {container_name} waiting: {reason}"
+ (f" ({waiting['message']})" if waiting.get("message") else "")
)
if reason in {"ImagePullBackOff", "ErrImagePull", "InvalidImageName"}:
add(
"image_pull_failed",
"Container image is unavailable",
waiting.get("message") or reason,
"Check the image reference, registry reachability and imagePullSecrets in this namespace.",
container_name,
)
if reason == "CrashLoopBackOff":
add(
"crash_loop",
"Container repeatedly exits",
waiting.get("message") or reason,
"Inspect current and previous container logs and its exit code before changing "
"restart or resource settings.",
container_name,
)
for previous, state in ((False, current), (True, container.get("last_state") or {})):
terminated = state.get("terminated") or {}
if terminated.get("reason") == "OOMKilled":
add(
"oom_killed",
"A container attempt was killed for exceeding memory",
f"{'Previous' if previous else 'Current'} attempt: OOMKilled; "
f"exit code {terminated.get('exit_code')}",
"Compare memory usage with the container limit and Spark memory/overhead "
"settings; investigate before increasing limits.",
container_name,
"warning" if previous else "error",
)
elif not previous and terminated.get("exit_code") not in (None, 0):
add(
"container_failed",
"Container exited unsuccessfully",
f"{terminated.get('reason') or 'Terminated'}; exit code {terminated['exit_code']}",
"Inspect this container's logs for the application error. The exit code alone "
"does not identify the cause.",
container_name,
)
messages = [(event.get("reason", ""), event.get("message", "")) for event in events]
messages.extend(
(condition.get("reason", ""), condition.get("message", ""))
for condition in status.get("conditions") or []
if condition.get("type") == "PodScheduled" and condition.get("status") == "False"
)
for reason, message in messages:
message = message or ""
lower = message.lower()
if reason in {"FailedScheduling", "Unschedulable"}:
add(
"unschedulable",
"Kubernetes could not schedule the pod",
message or reason,
"Review the scheduler's stated constraints, resource requests, node selectors and affinity.",
)
if "insufficient " in lower:
add(
"insufficient_resources",
"Scheduler reports insufficient resources",
message,
"Compare requested resources with eligible node capacity; adjust requests or "
"add capacity.",
)
if "taint" in lower:
add(
"taint_mismatch",
"Scheduler reports a taint constraint",
message,
"Check whether the workload belongs on these nodes before adding a matching toleration.",
)
if "persistentvolumeclaim" in lower and ("not found" in lower or "unbound" in lower):
add(
"pvc_missing",
"A required persistent volume claim is unavailable",
message,
"Check the named PVC in this namespace, its binding state and storage class. Do "
"not create an empty replacement for required data.",
)
if "forbidden" in lower or reason == "Forbidden":
add(
"permission_denied",
"Kubernetes reported a denied operation",
message or reason,
"Identify the denied verb/resource and acting service account in the evidence; "
"ask an administrator for a namespace-scoped permission.",
)
if reason in {"FailedToRetrieveImagePullSecret"} or "failed to pull image" in lower:
add(
"image_pull_failed",
"Kubernetes could not access the container image",
message or reason,
"Check the image reference, registry reachability and imagePullSecrets in this namespace.",
)
if not findings and status.get("phase") in {"Pending", "Failed"}:
add(
"cause_unknown",
f"Pod is {status['phase']} without a specific observed cause",
# A waiting reason without a dedicated rule (CreateContainerConfigError for a missing
# Secret, ErrImageNeverPull) is still the most useful evidence available.
"; ".join(waiting_containers)
or status.get("message")
or status.get("reason")
or f"Pod phase: {status['phase']}",
"Inspect container logs and newer Kubernetes events. Absence of diagnostic evidence "
"does not establish a cause.",
severity="warning",
)
return list(findings.values())
|