Skip to content

Commit e880ef4

Browse files
committed
use "force" when deleting containers
By default, zun will raise a 409 when deleting a container in either a running, or transitional state. Calling with "stop=True" fixes it for running containers, but still not for transitional ones, such as containers stuck "creating" or "error". Update python-chi to call with force=true to work in call cases. Note: this depends on the following zun policy being set, otherwise the force argument is admin-only. `container:delete_force: rule:admin_or_owner`
1 parent d6b5877 commit e880ef4

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

chi/container.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,17 @@ def delete(self):
171171
If the container has an ID, it calls the `destroy_container` function to delete the container.
172172
After deletion, it sets the ID and status of the container to None.
173173
174+
Note: Delete is called with "force", which depends on the following zun policy:
175+
`container:delete_force: rule:admin_or_owner`
176+
174177
Args:
175178
None
176179
177180
Returns:
178181
None
179182
"""
180183
if self.id:
181-
destroy_container(self.id)
184+
destroy_container(self.id, force=True)
182185
self.id = None
183186
self._status = None
184187

@@ -468,7 +471,7 @@ def snapshot_container(
468471
return zun().containers.commit(container_ref, repository, tag=tag)["uuid"]
469472

470473

471-
def destroy_container(container_ref: "str"):
474+
def destroy_container(container_ref: "str", stop=False, force=False):
472475
"""
473476
.. deprecated:: 1.0
474477
@@ -479,7 +482,7 @@ def destroy_container(container_ref: "str"):
479482
Args:
480483
container_ref (str): The name or ID of the container.
481484
"""
482-
return zun().containers.delete(container_ref, stop=True)
485+
return zun().containers.delete(container_ref, stop=stop, force=force)
483486

484487

485488
def get_logs(container_ref: "str", stdout=True, stderr=True):

tests/test_container.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ def test_download_extracts_tar_and_writes_file(mocker):
113113
assert f.read() == file_content
114114

115115

116+
def test_delete_calls_force(mocker):
117+
destroy_mock = mocker.patch("chi.container.destroy_container")
118+
container = Container(name="test", image_ref="img")
119+
container.id = "fake-id"
120+
121+
container.delete()
122+
123+
destroy_mock.assert_called_once_with("fake-id", force=True)
124+
assert container.id is None
125+
assert container._status is None
126+
127+
116128
def test_submit_idempotent_returns_existing_without_create(mocker):
117129
# idempotent=true, wait=true
118130
chi_container = Container(name="dup-name", image_ref="img")

0 commit comments

Comments
 (0)