Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skip not found error during image pruner #102

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions config/registry_image_pruner/image_pruner/prune_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from collections.abc import Iterator
from http.client import HTTPResponse
from typing import Any, Dict, List
from urllib.error import HTTPError
from urllib.parse import urlencode
from urllib.request import Request, urlopen

Expand All @@ -29,10 +30,18 @@ def get_quay_repo(quay_token: str, namespace: str, name: str) -> ImageRepo:
"Authorization": f"Bearer {quay_token}",
})
resp: HTTPResponse
with urlopen(request) as resp:
if resp.status != 200:
raise RuntimeError(resp.reason)
return json.loads(resp.read())
try:
with urlopen(request) as resp:
if resp.status != 200:
raise RuntimeError(resp.reason)
return json.loads(resp.read())

except HTTPError as ex:
# ignore if not found
if ex.status != 404:
raise(ex)
else:
return {}


def delete_image_tag(quay_token: str, namespace: str, name: str, tag: str) -> None:
Expand All @@ -41,9 +50,15 @@ def delete_image_tag(quay_token: str, namespace: str, name: str, tag: str) -> No
"Authorization": f"Bearer {quay_token}",
})
resp: HTTPResponse
with urlopen(request) as resp:
if resp.status != 200 and resp.status != 204:
raise RuntimeError(resp.reason)
try:
with urlopen(request) as resp:
if resp.status != 200 and resp.status != 204:
raise RuntimeError(resp.reason)

except HTTPError as ex:
# ignore if not found
if ex.status != 404:
raise(ex)


def remove_tags(tags: Dict[str, Any], quay_token: str, namespace: str, name: str, dry_run: bool = False) -> None:
Expand All @@ -68,6 +83,10 @@ def process_repositories(repos: List[ImageRepo], quay_token: str, dry_run: bool
name = repo["name"]
LOGGER.info("Processing repository %s: %s/%s", next(processed_repos_counter), namespace, name)
repo_info = get_quay_repo(quay_token, namespace, name)

if not repo_info:
continue

if (tags := repo_info.get("tags")) is not None:
remove_tags(tags, quay_token, namespace, name, dry_run=dry_run)

Expand Down
Loading