Skip to content

Commit

Permalink
fix: do not create duplicate repo for rh push (#331)
Browse files Browse the repository at this point in the history
From the beginning, we would create a second
registry.access.redhat.com entry when releasing
to Red Hat registry. But it turns out we don't need
two separate entries. The traditional SP pipeline
only creates one as well.

Signed-off-by: Martin Malina <[email protected]>
  • Loading branch information
mmalina authored Dec 2, 2024
1 parent 7f7a156 commit daea8c0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 122 deletions.
45 changes: 22 additions & 23 deletions pyxis/create_container_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def create_container_image(args, parsed_data: Dict[str, Any]):

LOGGER.info("Creating new container image")

repositories = construct_repositories(args)
repository = construct_repository(args)

# sum_layer_size_bytes isn't accepted in the parsed_data payload to pyxis
sum_layer_size_bytes = parsed_data.pop("sum_layer_size_bytes", 0)
Expand All @@ -277,7 +277,7 @@ def create_container_image(args, parsed_data: Dict[str, Any]):
upload_url = urljoin(args.pyxis_url, "v1/images")

container_image_payload = {
"repositories": repositories,
"repositories": [repository],
"certified": json.loads(args.certified.lower()),
"image_id": args.architecture_digest,
"architecture": parsed_data["architecture"],
Expand Down Expand Up @@ -311,7 +311,7 @@ def add_container_image_repository(args: Dict[str, Any], image: Dict[str, Any]):
patch_url = urljoin(args.pyxis_url, f"v1/images/id/{identifier}")

payload = {"repositories": image["repositories"]}
payload["repositories"].extend(construct_repositories(args))
payload["repositories"].append(construct_repository(args))

rsp = pyxis.patch(patch_url, payload).json()

Expand All @@ -322,40 +322,39 @@ def add_container_image_repository(args: Dict[str, Any], image: Dict[str, Any]):
raise Exception("Image metadata was not successfully added to Pyxis.")


def construct_repositories(args):
def construct_repository(args):
image_name = args.name
image_registry = image_name.split("/")[0]
image_repo = image_name.split("/", 1)[1]

date_now = datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f+00:00")

repos = [
{
# For images released to registry.redhat.io we need a special repository item
# with published=true and registry and repository converted.
# E.g. if the name in the oras manifest result is
# "quay.io/redhat-prod/rhtas-tech-preview----cosign-rhel9",
# repository will be "rhtas-tech-preview/cosign-rhel9"
if args.rh_push == "true":
LOGGER.info("--rh-push is true. Associating registry.access.redhat.com repository.")
repo = {
"published": True,
"registry": "registry.access.redhat.com",
"repository": proxymap(image_name),
"push_date": date_now,
"tags": pyxis_tags(args, date_now),
}
else:
repo = {
"published": False,
"registry": image_registry,
"repository": image_repo,
"push_date": date_now,
"tags": pyxis_tags(args, date_now),
}
]

repos[0].update(repository_digest_values(args))

# For images released to registry.redhat.io we need a second repository item
# with published=true and registry and repository converted.
# E.g. if the name in the oras manifest result is
# "quay.io/redhat-prod/rhtas-tech-preview----cosign-rhel9",
# repository will be "rhtas-tech-preview/cosign-rhel9"
if not args.rh_push == "true":
LOGGER.info("--rh-push is not set. Skipping public registry association.")
else:
rh_repo = repos[0].copy()
rh_repo["published"] = True
rh_repo["registry"] = "registry.access.redhat.com"
rh_repo["repository"] = proxymap(image_name)
repos.append(rh_repo)
repo.update(repository_digest_values(args))

return repos
return repo


def main(): # pragma: no cover
Expand Down
144 changes: 45 additions & 99 deletions pyxis/test_create_container_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
repository_digest_values,
create_container_image,
add_container_image_repository,
construct_repositories,
construct_repository,
)


Expand Down Expand Up @@ -183,20 +183,6 @@ def test_add_container_image_repository(mock_datetime, mock_patch):
PYXIS_URL + "v1/images/id/some_id",
{
"repositories": [
{
"published": False,
"registry": "quay.io",
"repository": "redhat-pending/some_product----some_repo",
"push_date": "1970-10-10T10:10:10.000000+00:00",
"tags": [
{
"added_date": "1970-10-10T10:10:10.000000+00:00",
"name": "some_version",
}
],
# Note, no manifest_list_digest here. Single arch.
"manifest_schema2_digest": "arch specific digest",
},
{
"published": True,
"registry": "registry.access.redhat.com",
Expand Down Expand Up @@ -311,24 +297,6 @@ def test_create_container_image_rh_push_multiple_tags(mock_datetime, mock_post):
PYXIS_URL + "v1/images",
{
"repositories": [
{
"published": False,
"registry": "quay.io",
"repository": "redhat-pending/some-product----some-image",
"push_date": "1970-10-10T10:10:10.000000+00:00",
"tags": [
{
"added_date": "1970-10-10T10:10:10.000000+00:00",
"name": "tagprefix",
},
{
"added_date": "1970-10-10T10:10:10.000000+00:00",
"name": "tagprefix-timestamp",
},
],
"manifest_list_digest": "some_digest",
"manifest_schema2_digest": "arch specific digest",
},
{
"published": True,
"registry": "registry.access.redhat.com",
Expand Down Expand Up @@ -540,7 +508,7 @@ def test_repository_digest_values__multi_arch():


@patch("create_container_image.datetime")
def test_construct_repositories__rh_push_true(mock_datetime):
def test_construct_repository__rh_push_true(mock_datetime):
mock_datetime.now = MagicMock(return_value=datetime(1970, 10, 10, 10, 10, 10))
args = MagicMock()
args.media_type = "application/vnd.docker.distribution.manifest.list.v2+json"
Expand All @@ -551,50 +519,30 @@ def test_construct_repositories__rh_push_true(mock_datetime):
args.rh_push = "true"
args.name = "quay.io/redhat-pending/some-product----some-image"

repos = construct_repositories(args)
repo = construct_repository(args)

assert repos == [
{
"published": False,
"registry": "quay.io",
"repository": "redhat-pending/some-product----some-image",
"push_date": "1970-10-10T10:10:10.000000+00:00",
"tags": [
{
"added_date": "1970-10-10T10:10:10.000000+00:00",
"name": "tagprefix",
},
{
"added_date": "1970-10-10T10:10:10.000000+00:00",
"name": "tagprefix-timestamp",
},
],
"manifest_list_digest": "some_digest",
"manifest_schema2_digest": "arch specific digest",
},
{
"published": True,
"registry": "registry.access.redhat.com",
"repository": "some-product/some-image",
"push_date": "1970-10-10T10:10:10.000000+00:00",
"tags": [
{
"added_date": "1970-10-10T10:10:10.000000+00:00",
"name": "tagprefix",
},
{
"added_date": "1970-10-10T10:10:10.000000+00:00",
"name": "tagprefix-timestamp",
},
],
"manifest_list_digest": "some_digest",
"manifest_schema2_digest": "arch specific digest",
},
]
assert repo == {
"published": True,
"registry": "registry.access.redhat.com",
"repository": "some-product/some-image",
"push_date": "1970-10-10T10:10:10.000000+00:00",
"tags": [
{
"added_date": "1970-10-10T10:10:10.000000+00:00",
"name": "tagprefix",
},
{
"added_date": "1970-10-10T10:10:10.000000+00:00",
"name": "tagprefix-timestamp",
},
],
"manifest_list_digest": "some_digest",
"manifest_schema2_digest": "arch specific digest",
}


@patch("create_container_image.datetime")
def test_construct_repositories__rh_push_false(mock_datetime):
def test_construct_repository__rh_push_false(mock_datetime):
mock_datetime.now = MagicMock(return_value=datetime(1970, 10, 10, 10, 10, 10))
args = MagicMock()
args.media_type = "application/vnd.docker.distribution.manifest.list.v2+json"
Expand All @@ -605,29 +553,27 @@ def test_construct_repositories__rh_push_false(mock_datetime):
args.rh_push = "false"
args.name = "quay.io/some-org/some-image"

repos = construct_repositories(args)
repo = construct_repository(args)

assert repos == [
{
"published": False,
"registry": "quay.io",
"repository": "some-org/some-image",
"push_date": "1970-10-10T10:10:10.000000+00:00",
"tags": [
{
"added_date": "1970-10-10T10:10:10.000000+00:00",
"name": "tagprefix",
},
{
"added_date": "1970-10-10T10:10:10.000000+00:00",
"name": "tagprefix-timestamp",
},
{
"added_date": "1970-10-10T10:10:10.000000+00:00",
"name": "latest",
},
],
"manifest_list_digest": "some_digest",
"manifest_schema2_digest": "arch specific digest",
},
]
assert repo == {
"published": False,
"registry": "quay.io",
"repository": "some-org/some-image",
"push_date": "1970-10-10T10:10:10.000000+00:00",
"tags": [
{
"added_date": "1970-10-10T10:10:10.000000+00:00",
"name": "tagprefix",
},
{
"added_date": "1970-10-10T10:10:10.000000+00:00",
"name": "tagprefix-timestamp",
},
{
"added_date": "1970-10-10T10:10:10.000000+00:00",
"name": "latest",
},
],
"manifest_list_digest": "some_digest",
"manifest_schema2_digest": "arch specific digest",
}

0 comments on commit daea8c0

Please sign in to comment.