Skip to content

Commit 36c6b10

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 477596c commit 36c6b10

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
@@ -181,14 +181,17 @@ def delete(self):
181181
If the container has an ID, it calls the `destroy_container` function to delete the container.
182182
After deletion, it sets the ID and status of the container to None.
183183
184+
Note: Delete is called with "force", which depends on the following zun policy:
185+
`container:delete_force: rule:admin_or_owner`
186+
184187
Args:
185188
None
186189
187190
Returns:
188191
None
189192
"""
190193
if self.id:
191-
destroy_container(self.id)
194+
destroy_container(self.id, force=True)
192195
self.id = None
193196
self._status = None
194197

@@ -478,7 +481,7 @@ def snapshot_container(
478481
return zun().containers.commit(container_ref, repository, tag=tag)["uuid"]
479482

480483

481-
def destroy_container(container_ref: "str"):
484+
def destroy_container(container_ref: "str", stop=False, force=False):
482485
"""
483486
.. deprecated:: 1.0
484487
@@ -489,7 +492,7 @@ def destroy_container(container_ref: "str"):
489492
Args:
490493
container_ref (str): The name or ID of the container.
491494
"""
492-
return zun().containers.delete(container_ref, stop=True)
495+
return zun().containers.delete(container_ref, stop=stop, force=force)
493496

494497

495498
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)