Skip to content

Commit

Permalink
Disable stage check when removing arches [CLOUDDST-24444] (#281)
Browse files Browse the repository at this point in the history
Do stage check only when removing tags from prod. It should allow
removing arches from a prod tag if the tag exists in stage.
  • Loading branch information
emilyzheng authored Jan 8, 2025
1 parent ad5f4f3 commit ee2a125
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
63 changes: 45 additions & 18 deletions src/pubtools/_quay/tag_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ def check_input_validity(self) -> None:
The constraints are following:
1. If adding tags to prod target, these tags must already exist in stage target.
2. If removing tags from prod target, these tags must already not exist in stage target.
"""
if "propagated_from" in self.target_settings:
full_repo_schema = "{host}/{namespace}/{repo}"
Expand Down Expand Up @@ -180,23 +179,50 @@ def check_input_validity(self) -> None:
else:
raise

# all to-be-removed tags must already be removed from stage
for tag in item.metadata["remove_tags"]:
stage_image = "{0}:{1}".format(stage_repo, tag)
try:
stage_quay_client.get_manifest(stage_image)
except requests.exceptions.HTTPError as e:
if e.response.status_code == 404 or e.response.status_code == 401:
# 404/401 -> all good
pass
else:
raise
else:
raise BadPushItem(
"To-be-removed tag {0} must already be removed from stage repo".format(
tag
)
)
def check_input_validity_remove(self, push_item: Any, tag: str) -> None:
"""
Check if input data satisfies tag-docker specific constraints.
The constraints are following:
1. If removing tags from prod target, these tags must already not exist in stage target.
Args:
push_item (ContainerPushItem):
Push item to perform the check with.
tag (str):
Tag to perform the check with.
"""
if "propagated_from" in self.target_settings:
full_repo_schema = "{host}/{namespace}/{repo}"
stage_target_info = self.hub.worker.get_target_info(
self.target_settings["propagated_from"]
)
stage_namespace = stage_target_info["settings"]["quay_namespace"]
stage_quay_client = QuayClient(
stage_target_info["settings"]["dest_quay_user"],
stage_target_info["settings"]["dest_quay_password"],
self.quay_host,
)

internal_repo = get_internal_container_repo_name(list(push_item.repos.keys())[0])
stage_repo = full_repo_schema.format(
host=self.quay_host, namespace=stage_namespace, repo=internal_repo
)

# all to-be-removed tags must already be removed from stage
stage_image = "{0}:{1}".format(stage_repo, tag)
try:
stage_quay_client.get_manifest(stage_image)
except requests.exceptions.HTTPError as e:
if e.response.status_code == 404 or e.response.status_code == 401:
# 404/401 -> all good
pass
else:
raise
else:
raise BadPushItem(
"To-be-removed tag {0} must already be removed from stage repo".format(tag)
)

def get_image_details(self, reference: str, executor: Executor) -> Optional[ImageDetails]:
"""
Expand Down Expand Up @@ -944,6 +970,7 @@ def run(self) -> None:
continue
# If no archs will remain after removal, just perform untagging
elif not keep_archs:
self.check_input_validity_remove(item, tag)
self.untag_image(item, tag)
# if some archs will be removed and some will remain, create new manifest list
else:
Expand Down
8 changes: 5 additions & 3 deletions tests/test_tag_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,11 @@ def test_check_input_validity_remove_tag_still_in_stage(
"some-target",
target_settings,
)
tag_docker_instance.check_input_validity()
tag_docker_instance.check_input_validity_remove(tag_docker_push_item_remove_no_src, "v1.8")
tag_docker_instance.check_input_validity_remove(tag_docker_push_item_remove_no_src, "v1.9")

mock_get_target_info.assert_called_once_with("quay-stage-target")
assert mock_get_target_info.call_count == 2
assert mock_get_target_info.call_args_list[0] == mock.call("quay-stage-target")
assert mock_get_manifest.call_count == 2
assert mock_get_manifest.call_args_list[0] == mock.call(
"quay.io/stage-namespace/namespace----test_repo2:v1.8"
Expand Down Expand Up @@ -401,7 +403,7 @@ def test_check_input_validity_remove_tag_server_error(
"some-target",
target_settings,
)
tag_docker_instance.check_input_validity()
tag_docker_instance.check_input_validity_remove(tag_docker_push_item_remove_no_src, "v1.8")


@mock.patch("pubtools._quay.tag_docker.LocalExecutor")
Expand Down

0 comments on commit ee2a125

Please sign in to comment.