microk8s icon indicating copy to clipboard operation
microk8s copied to clipboard

KeyError in kill-host-pods.py

Open ianroberts opened this issue 10 months ago • 0 comments

https://github.com/canonical/microk8s/blob/f9eb294558bf1103a3109504452077af059af384/scripts/kill-host-pods.py#L24

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 avatar Mar 28 '24 15:03 ianroberts