Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError in kill-host-pods.py #4471

Open
ianroberts opened this issue Mar 28, 2024 · 0 comments · May be fixed by #4473
Open

KeyError in kill-host-pods.py #4471

ianroberts opened this issue Mar 28, 2024 · 0 comments · May be fixed by #4473

Comments

@ianroberts
Copy link

for container in pod["status"]["containerStatuses"] or []:

It is possible for a pod not to have a containerStatus field, e.g. when it is in "Pending" state because no compatible node is available for it to be scheduled onto. If kill-host-pods.py runs when there are one or more pods in this condition, it fails with the following traceback:

Traceback (most recent call last):
  File "/snap/microk8s/6364/scripts/kill-host-pods.py", line 104, in <module>
    main()
  File "/snap/microk8s/6364/usr/lib/python3/dist-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/snap/microk8s/6364/usr/lib/python3/dist-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/snap/microk8s/6364/usr/lib/python3/dist-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/snap/microk8s/6364/usr/lib/python3/dist-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/snap/microk8s/6364/scripts/kill-host-pods.py", line 88, in main
    if not post_filter_has_known_containers(pod, containers):
  File "/snap/microk8s/6364/scripts/kill-host-pods.py", line 24, in post_filter_has_known_containers
    for container in pod["status"]["containerStatuses"] or []:
KeyError: 'containerStatuses'

Two possible fixes:

  1. use .get instead of []
  2. add another layer of try/except around the whole for loop
ianroberts added a commit to ianroberts/microk8s that referenced this issue Mar 29, 2024
In certain circumstances pod["status"] may not have a containerStatuses child property at all (e.g. if the pod is Pending and not yet scheduled to any node).  Guard against this by using .get (returns None) instead of [] (raises KeyError).

Closes canonical#4471
ianroberts added a commit to ianroberts/microk8s that referenced this issue Mar 29, 2024
In certain circumstances pod["status"] may not have a containerStatuses child property at all (e.g. if the pod is Pending and not yet scheduled to any node).  Guard against this by using .get (returns None) instead of [] (raises KeyError).
@ianroberts ianroberts linked a pull request Mar 29, 2024 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant