File size: 14,658 Bytes
c8f3b98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
"""Task: Kubernetes Pod Failures — MEDIUM.

Agent fixes common pod failure scenarios:
OOMKilled, ImagePullBackOff, wrong command, missing ConfigMap, liveness probe.
"""

from server.models import TaskDifficulty
from server.tasks.base import BaseTask


class K8sPodTask(BaseTask):
    NAME = "Kubernetes Pod Failures"
    DESCRIPTION = "Fix Kubernetes pod failures including CrashLoopBackOff, ImagePullBackOff, and resource issues"
    DIFFICULTY = TaskDifficulty.MEDIUM
    AVAILABLE_SECRETS = []

    SCENARIOS = [
        # Scenario 1: CrashLoopBackOff — OOMKilled (memory limit too low)
        {
            "id": "oom_killed",
            "files": [
                {
                    "path": "k8s/deployment.yaml",
                    "type": "kubernetes",
                    "content": (
                        "apiVersion: apps/v1\n"
                        "kind: Deployment\n"
                        "metadata:\n"
                        "  name: api-server\n"
                        "spec:\n"
                        "  replicas: 3\n"
                        "  selector:\n"
                        "    matchLabels:\n"
                        "      app: api\n"
                        "  template:\n"
                        "    metadata:\n"
                        "      labels:\n"
                        "        app: api\n"
                        "    spec:\n"
                        "      containers:\n"
                        "      - name: api\n"
                        '        image: myapp:v1.2.3\n'
                        "        resources:\n"
                        "          limits:\n"
                        '            memory: "64Mi"\n'
                        '            cpu: "100m"\n'
                        "        ports:\n"
                        "        - containerPort: 8080\n"
                    ),
                }
            ],
            "error": {
                "phase": "k8s_runtime",
                "message": (
                    "$ kubectl get pods\n"
                    "NAME                          READY   STATUS             RESTARTS   AGE\n"
                    "api-server-7d4b8c9f5-x2k9m   0/1     CrashLoopBackOff   5          3m\n"
                    "\n"
                    "$ kubectl describe pod api-server-7d4b8c9f5-x2k9m\n"
                    "...\n"
                    "State:          Waiting\n"
                    "  Reason:       CrashLoopBackOff\n"
                    "Last State:     Terminated\n"
                    "  Reason:       OOMKilled\n"
                    "  Exit Code:    137\n"
                    "...\n"
                    "Events:\n"
                    "  Warning  OOMKilling  3m  kubelet  Memory limit 64Mi exceeded"
                ),
            },
            "expected_fixes": [
                {
                    "file": "k8s/deployment.yaml",
                    "type": "contains",
                    "expected": 'memory: "256Mi"',
                    "hint": "Container is OOMKilled with 64Mi limit. The app needs at least 256Mi.",
                }
            ],
        },

        # Scenario 2: ImagePullBackOff — image tag typo
        {
            "id": "image_pull_backoff",
            "files": [
                {
                    "path": "k8s/deployment.yaml",
                    "type": "kubernetes",
                    "content": (
                        "apiVersion: apps/v1\n"
                        "kind: Deployment\n"
                        "metadata:\n"
                        "  name: web-app\n"
                        "spec:\n"
                        "  replicas: 2\n"
                        "  selector:\n"
                        "    matchLabels:\n"
                        "      app: web\n"
                        "  template:\n"
                        "    metadata:\n"
                        "      labels:\n"
                        "        app: web\n"
                        "    spec:\n"
                        "      containers:\n"
                        "      - name: web\n"
                        "        image: nginx:latset\n"
                        "        ports:\n"
                        "        - containerPort: 80\n"
                    ),
                }
            ],
            "error": {
                "phase": "k8s_runtime",
                "message": (
                    "$ kubectl get pods\n"
                    "NAME                       READY   STATUS             RESTARTS   AGE\n"
                    "web-app-5f8d7b6c4-abc12    0/1     ImagePullBackOff   0          2m\n"
                    "\n"
                    "$ kubectl describe pod web-app-5f8d7b6c4-abc12\n"
                    "...\n"
                    "Events:\n"
                    '  Warning  Failed   2m   kubelet  Failed to pull image "nginx:latset": '
                    "rpc error: code = NotFound desc = failed to pull and unpack image: "
                    "reference not found\n"
                    "  Warning  Failed   2m   kubelet  Error: ImagePullBackOff\n"
                    "..."
                ),
            },
            "expected_fixes": [
                {
                    "file": "k8s/deployment.yaml",
                    "type": "contains",
                    "expected": "image: nginx:latest",
                    "hint": "Image tag has a typo: 'latset' should be 'latest'",
                }
            ],
        },

        # Scenario 3: CrashLoopBackOff — wrong command
        {
            "id": "wrong_command",
            "files": [
                {
                    "path": "k8s/deployment.yaml",
                    "type": "kubernetes",
                    "content": (
                        "apiVersion: apps/v1\n"
                        "kind: Deployment\n"
                        "metadata:\n"
                        "  name: worker\n"
                        "spec:\n"
                        "  replicas: 1\n"
                        "  selector:\n"
                        "    matchLabels:\n"
                        "      app: worker\n"
                        "  template:\n"
                        "    metadata:\n"
                        "      labels:\n"
                        "        app: worker\n"
                        "    spec:\n"
                        "      containers:\n"
                        "      - name: worker\n"
                        "        image: python:3.11-slim\n"
                        "        command: [\"python\", \"workers.py\"]\n"
                        "        resources:\n"
                        "          limits:\n"
                        '            memory: "512Mi"\n'
                        '            cpu: "500m"\n'
                    ),
                },
                {
                    "path": "app/worker.py",
                    "type": "other",
                    "content": (
                        "import time\n"
                        "\n"
                        "def main():\n"
                        "    while True:\n"
                        "        print('Processing...')\n"
                        "        time.sleep(5)\n"
                        "\n"
                        "if __name__ == '__main__':\n"
                        "    main()\n"
                    ),
                },
            ],
            "error": {
                "phase": "k8s_runtime",
                "message": (
                    "$ kubectl get pods\n"
                    "NAME                      READY   STATUS             RESTARTS   AGE\n"
                    "worker-6b8f9d7c4-kj3m2    0/1     CrashLoopBackOff   4          2m\n"
                    "\n"
                    "$ kubectl logs worker-6b8f9d7c4-kj3m2\n"
                    "python: can't open file '/workers.py': [Errno 2] No such file or directory\n"
                    "\n"
                    "$ kubectl describe pod worker-6b8f9d7c4-kj3m2\n"
                    "...\n"
                    "State:          Waiting\n"
                    "  Reason:       CrashLoopBackOff\n"
                    "Last State:     Terminated\n"
                    "  Reason:       Error\n"
                    "  Exit Code:    2\n"
                    "..."
                ),
            },
            "expected_fixes": [
                {
                    "file": "k8s/deployment.yaml",
                    "type": "contains",
                    "expected": 'command: ["python", "worker.py"]',
                    "hint": "The command references 'workers.py' but the file is named 'worker.py' (no 's')",
                }
            ],
        },

        # Scenario 4: CreateContainerConfigError — missing ConfigMap
        {
            "id": "missing_configmap",
            "files": [
                {
                    "path": "k8s/deployment.yaml",
                    "type": "kubernetes",
                    "content": (
                        "apiVersion: apps/v1\n"
                        "kind: Deployment\n"
                        "metadata:\n"
                        "  name: backend\n"
                        "spec:\n"
                        "  replicas: 2\n"
                        "  selector:\n"
                        "    matchLabels:\n"
                        "      app: backend\n"
                        "  template:\n"
                        "    metadata:\n"
                        "      labels:\n"
                        "        app: backend\n"
                        "    spec:\n"
                        "      containers:\n"
                        "      - name: backend\n"
                        "        image: mybackend:v2.0\n"
                        "        ports:\n"
                        "        - containerPort: 8080\n"
                        "        envFrom:\n"
                        "        - configMapRef:\n"
                        "            name: app-config\n"
                        "        resources:\n"
                        "          limits:\n"
                        '            memory: "512Mi"\n'
                        '            cpu: "500m"\n'
                    ),
                },
            ],
            "error": {
                "phase": "k8s_runtime",
                "message": (
                    "$ kubectl get pods\n"
                    "NAME                       READY   STATUS                       RESTARTS   AGE\n"
                    "backend-5c9d8f7b6-lm4n5    0/1     CreateContainerConfigError   0          1m\n"
                    "\n"
                    "$ kubectl describe pod backend-5c9d8f7b6-lm4n5\n"
                    "...\n"
                    "Events:\n"
                    '  Warning  Failed  1m  kubelet  Error: configmap "app-config" not found\n'
                    "..."
                ),
            },
            "expected_fixes": [
                {
                    "file": "k8s/configmap.yaml",
                    "type": "contains",
                    "expected": "name: app-config",
                    "hint": "The ConfigMap 'app-config' is referenced but doesn't exist. Create a ConfigMap manifest.",
                }
            ],
        },

        # Scenario 5: Pod not ready — liveness probe failing
        {
            "id": "liveness_probe_failing",
            "files": [
                {
                    "path": "k8s/deployment.yaml",
                    "type": "kubernetes",
                    "content": (
                        "apiVersion: apps/v1\n"
                        "kind: Deployment\n"
                        "metadata:\n"
                        "  name: api\n"
                        "spec:\n"
                        "  replicas: 2\n"
                        "  selector:\n"
                        "    matchLabels:\n"
                        "      app: api\n"
                        "  template:\n"
                        "    metadata:\n"
                        "      labels:\n"
                        "        app: api\n"
                        "    spec:\n"
                        "      containers:\n"
                        "      - name: api\n"
                        "        image: myapi:v3.1\n"
                        "        ports:\n"
                        "        - containerPort: 8080\n"
                        "        livenessProbe:\n"
                        "          httpGet:\n"
                        "            path: /healthz\n"
                        "            port: 3000\n"
                        "          initialDelaySeconds: 5\n"
                        "          periodSeconds: 10\n"
                        "        readinessProbe:\n"
                        "          httpGet:\n"
                        "            path: /ready\n"
                        "            port: 8080\n"
                        "          initialDelaySeconds: 5\n"
                        "          periodSeconds: 10\n"
                        "        resources:\n"
                        "          limits:\n"
                        '            memory: "512Mi"\n'
                        '            cpu: "500m"\n'
                    ),
                },
            ],
            "error": {
                "phase": "k8s_runtime",
                "message": (
                    "$ kubectl get pods\n"
                    "NAME                   READY   STATUS    RESTARTS      AGE\n"
                    "api-7f8d9c6b5-gh7j8    0/1     Running   3 (30s ago)  2m\n"
                    "\n"
                    "$ kubectl describe pod api-7f8d9c6b5-gh7j8\n"
                    "...\n"
                    "Events:\n"
                    "  Warning  Unhealthy  90s  kubelet  Liveness probe failed: "
                    "Get \"http://10.244.0.5:3000/healthz\": dial tcp 10.244.0.5:3000: "
                    "connect: connection refused\n"
                    "  Normal   Killing    90s  kubelet  Container api failed liveness probe, "
                    "will be restarted\n"
                    "...\n"
                    "\n"
                    "Note: The application listens on port 8080, not 3000."
                ),
            },
            "expected_fixes": [
                {
                    "file": "k8s/deployment.yaml",
                    "type": "contains",
                    "expected": "port: 8080\n          initialDelaySeconds: 5\n          periodSeconds: 10\n        readinessProbe:",
                    "hint": "The liveness probe port (3000) doesn't match the container port (8080). Change liveness probe port to 8080.",
                }
            ],
        },
    ]