Skip to content

Commit 8a0c87c

Browse files
Clean up local buildah images and manifests after push:
Use a temporary local manifest name to avoid collisions on repeated publishes, and remove local manifests and intermediate images in a finally block after push completes. Signed-off-by: Jacob Weinstock <jakobweinstock@gmail.com>
1 parent 82fd40c commit 8a0c87c

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

captain/buildah.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,14 @@ def manifest_push(
162162
_log = logger or _default_log
163163
_log.log(f"buildah manifest push → {dest}")
164164
run(["buildah", "manifest", "push", "--all", manifest, f"docker://{dest}"])
165+
166+
167+
def rmi(
168+
image: str,
169+
*,
170+
logger: StageLogger | None = None,
171+
) -> None:
172+
"""Remove a local image or manifest list."""
173+
_log = logger or _default_log
174+
_log.log(f"buildah rmi {image}")
175+
run(["buildah", "rmi", image])

captain/oci.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import tarfile
3434
from datetime import datetime, timezone
3535
from pathlib import Path
36+
from uuid import uuid4
3637

3738
from captain import artifacts, buildah, skopeo
3839
from captain.config import Config
@@ -206,6 +207,37 @@ def _build_platform_image(
206207
return current
207208

208209

210+
def _create_push_cleanup(
211+
image_ids: list[str],
212+
dest_ref: str,
213+
logger: StageLogger,
214+
) -> None:
215+
"""Create a manifest list from *image_ids*, push it to *dest_ref*, and clean up.
216+
217+
Uses a temporary local manifest name to avoid collisions on repeated
218+
publishes. After a successful (or failed) push, the local manifest
219+
and all *image_ids* are removed on a best-effort basis.
220+
"""
221+
temp_name = f"captain-local-{uuid4().hex[:12]}"
222+
manifest_id: str | None = None
223+
try:
224+
manifest_id = buildah.manifest_create(temp_name, logger=logger)
225+
for image_id in image_ids:
226+
buildah.manifest_add(manifest_id, image_id, logger=logger)
227+
buildah.manifest_push(manifest_id, dest_ref, logger=logger)
228+
finally:
229+
if manifest_id is not None:
230+
try:
231+
buildah.rmi(manifest_id, logger=logger)
232+
except Exception:
233+
pass
234+
for image_id in image_ids:
235+
try:
236+
buildah.rmi(image_id, logger=logger)
237+
except Exception:
238+
pass
239+
240+
209241
def _publish_single_arch(
210242
*,
211243
layer_tars: list[Path],
@@ -236,10 +268,7 @@ def _publish_single_arch(
236268
)
237269
image_ids.append(image_id)
238270

239-
manifest_id = buildah.manifest_create(ref, logger=logger)
240-
for image_id in image_ids:
241-
buildah.manifest_add(manifest_id, image_id, logger=logger)
242-
buildah.manifest_push(manifest_id, ref, logger=logger)
271+
_create_push_cleanup(image_ids, ref, logger)
243272

244273

245274
def _publish_combined(
@@ -314,10 +343,7 @@ def _publish_combined(
314343
)
315344
image_ids.append(image_id)
316345

317-
manifest_id = buildah.manifest_create(combined_ref, logger=logger)
318-
for image_id in image_ids:
319-
buildah.manifest_add(manifest_id, image_id, logger=logger)
320-
buildah.manifest_push(manifest_id, combined_ref, logger=logger)
346+
_create_push_cleanup(image_ids, combined_ref, logger)
321347
return True
322348

323349

0 commit comments

Comments
 (0)