Skip to content

Commit 0cc81bc

Browse files
mdegat01bdraco
andauthored
feat: add support for build/prune API (#1007)
* feat: add support for build/prune API * Fix mypy failure * Apply suggestion from @bdraco * Apply suggestion from @bdraco * Remove unused image_name fixture --------- Co-authored-by: J. Nick Koston <nick+github@koston.org>
1 parent 624e655 commit 0cc81bc

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

CHANGES/1007.feature.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Introduce support in the library for the `build/prune` API endpoint.

aiodocker/images.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,53 @@ async def prune(
282282
response = await self.docker._query_json("images/prune", "POST", params=params)
283283
return response
284284

285+
async def prune_builds(
286+
self,
287+
*,
288+
reserved_space: Optional[int] = None,
289+
max_used_space: Optional[int] = None,
290+
min_free_space: Optional[int] = None,
291+
all_builds: Optional[bool] = None,
292+
filters: Optional[Mapping[str, Any]] = None,
293+
) -> Dict[str, Any]:
294+
"""
295+
Delete builder cache
296+
297+
Args:
298+
reserved_space: Amount of disk space in bytes to keep for cache.
299+
max_used_space: Maximum amount of disk space allowed to keep for cache.
300+
min_free_space: Target amount of free disk space after pruning.
301+
all_builds: When true, consider all unused build cache objects for pruning.
302+
When false, only consider dangling build cache objects for pruning.
303+
filters: Filter expressions to limit what types of build cache objects are pruned.
304+
Available filters:
305+
- until: Only remove build cache objects created before given timestamp.
306+
- id: id=<id>
307+
- parent: parent=<id>
308+
- type: type=<string>
309+
- description: description=<string>
310+
- inuse
311+
- shared
312+
- private
313+
314+
Returns:
315+
Dictionary containing information about deleted caches and space reclaimed
316+
"""
317+
params: Dict[str, Any] = {}
318+
if reserved_space is not None:
319+
params["reserved-space"] = reserved_space
320+
if max_used_space is not None:
321+
params["max-used-space"] = max_used_space
322+
if min_free_space is not None:
323+
params["min-free-space"] = min_free_space
324+
if all_builds is not None:
325+
params["all"] = all_builds
326+
if filters is not None:
327+
params["filters"] = clean_filters(filters)
328+
329+
response = await self.docker._query_json("build/prune", "POST", params=params)
330+
return response
331+
285332
@staticmethod
286333
async def _stream(fileobj: SupportsRead[bytes]) -> AsyncIterator[bytes]:
287334
chunk = fileobj.read(io.DEFAULT_BUFFER_SIZE)

tests/test_images.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,35 @@ async def test_prune_images_without_filters(docker: Docker) -> None:
399399
assert "SpaceReclaimed" in result
400400
assert result["ImagesDeleted"] is None
401401
assert isinstance(result["SpaceReclaimed"], int)
402+
403+
404+
@pytest.mark.asyncio
405+
async def test_prune_builds_with_options(docker: Docker, random_name: str) -> None:
406+
"""Test builds prune with options set."""
407+
result = await docker.images.prune_builds(
408+
reserved_space=0,
409+
max_used_space=10**9,
410+
min_free_space=0,
411+
all_builds=True,
412+
filters={"id": random_name},
413+
)
414+
415+
# Verify the response structure
416+
assert isinstance(result, dict)
417+
assert "CachesDeleted" in result
418+
assert "SpaceReclaimed" in result
419+
assert result["CachesDeleted"] is None
420+
assert isinstance(result["SpaceReclaimed"], int)
421+
422+
423+
@pytest.mark.asyncio
424+
async def test_prune_builds_default_options(docker: Docker) -> None:
425+
"""Test builds prune using default options."""
426+
result = await docker.images.prune_builds()
427+
428+
# Verify the response structure
429+
assert isinstance(result, dict)
430+
assert "CachesDeleted" in result
431+
assert "SpaceReclaimed" in result
432+
assert result["CachesDeleted"] is None
433+
assert isinstance(result["SpaceReclaimed"], int)

0 commit comments

Comments
 (0)