Skip to content

KeyError in kill-host-pods.py #4471

Open
@ianroberts

Description

@ianroberts

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions