Skip to content

Commit

Permalink
Remove source image during cleanup
Browse files Browse the repository at this point in the history
STONEBLD-1643

Signed-off-by: Chenxiong Qi <[email protected]>
  • Loading branch information
tkdchen committed Feb 28, 2024
1 parent 6542216 commit 36e0206
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion config/registry_image_pruner/image_pruner/prune_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def delete_image_tag(quay_token: str, namespace: str, name: str, tag: str) -> No

def remove_tags(tags: Dict[str, Any], quay_token: str, namespace: str, name: str, dry_run: bool = False) -> None:
image_digests = [image["manifest_digest"] for image in tags.values()]
tag_regex = re.compile(r"^sha256-([0-9a-f]+)(\.sbom|\.att)$")
tag_regex = re.compile(r"^sha256-([0-9a-f]+)(\.sbom|\.att|\.src)$")
for tag in tags:
# attestation or sbom image
if (match := tag_regex.match(tag)) is not None:
Expand Down
26 changes: 18 additions & 8 deletions config/registry_image_pruner/image_pruner/test_prune_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_remove_orphan_tags_with_expected_suffixes(self, urlopen):
response.read.return_value = json.dumps({
"tags": {
"latest": {"name": "latest", "manifest_digest": "sha256:93a8743dc130"},
# image sha256:e45fad41f2ff has been removed
# image manifest sha256:03fabe17d4c5 does not exist
"sha256-03fabe17d4c5.sbom": {
"name": "sha256-03fabe17d4c5.sbom",
"manifest_digest": "sha256:e45fad41f2ff",
Expand All @@ -124,7 +124,11 @@ def test_remove_orphan_tags_with_expected_suffixes(self, urlopen):
"name": "sha256-03fabe17d4c5.att",
"manifest_digest": "sha256:e45fad41f2ff",
},
# image sha256:03fabe17d4c5 has been removed
"sha256-03fabe17d4c5.src": {
"name": "sha256-03fabe17d4c5.src",
"manifest_digest": "sha256:f490ad41f2cc",
},
# image manifest sha256:071c766795a0 does not exist
"sha256-071c766795a0.sbom": {
"name": "sha256-071c766795a0.sbom",
"manifest_digest": "sha256:961207f62413",
Expand All @@ -133,6 +137,10 @@ def test_remove_orphan_tags_with_expected_suffixes(self, urlopen):
"name": "sha256-071c766795a0.att",
"manifest_digest": "sha256:961207f62413",
},
"sha256-071c766795a0.src": {
"name": "sha256-071c766795a0.src",
"manifest_digest": "sha256:0ab207f62413",
},
},
}).encode()
get_repo_rv.__enter__.return_value = response
Expand All @@ -152,23 +160,25 @@ def test_remove_orphan_tags_with_expected_suffixes(self, urlopen):
delete_tag_rv,
delete_tag_rv,
delete_tag_rv,
delete_tag_rv,
delete_tag_rv,
]

main()

self.assertEqual(6, urlopen.call_count)
self.assertEqual(8, urlopen.call_count)

def _assert_deletion_request(request: Request, tag: str) -> None:
expected_url_path = f"{QUAY_API_URL}/repository/sample/hello-image/tag/{tag}"
self.assertEqual(expected_url_path, request.get_full_url())
self.assertEqual("DELETE", request.get_method())

test_pairs = zip(
# keep same order as above
("sha256-03fabe17d4c5.sbom", "sha256-03fabe17d4c5.att",
"sha256-071c766795a0.sbom", "sha256-071c766795a0.att"),
urlopen.mock_calls[-4:],
# keep same order as above
tags_to_remove = (
"sha256-03fabe17d4c5.sbom", "sha256-03fabe17d4c5.att", "sha256-03fabe17d4c5.src",
"sha256-071c766795a0.sbom", "sha256-071c766795a0.att", "sha256-071c766795a0.src",
)
test_pairs = zip(tags_to_remove, urlopen.mock_calls[-6:])
for tag, call in test_pairs:
_assert_deletion_request(call.args[0], tag)

Expand Down

0 comments on commit 36e0206

Please sign in to comment.