Skip to content

Commit

Permalink
Add tag/untag commands for container repositories
Browse files Browse the repository at this point in the history
fixes: #423
  • Loading branch information
gerrod3 committed Jan 20, 2022
1 parent 99689c1 commit 2e75eb4
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/423.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added tag/untag commands to add and remove tags from images in container repositories.
7 changes: 7 additions & 0 deletions pulpcore/cli/container/context.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import ClassVar

from pulpcore.cli.common.context import (
EntityDefinition,
PluginRequirement,
Expand Down Expand Up @@ -101,10 +103,13 @@ class PulpContainerRepositoryContext(PulpRepositoryContext):
UPDATE_ID = "repositories_container_container_partial_update"
DELETE_ID = "repositories_container_container_delete"
SYNC_ID = "repositories_container_container_sync"
TAG_ID: ClassVar[str] = "repositories_container_container_tag"
UNTAG_ID: ClassVar[str] = "repositories_container_container_untag"
VERSION_CONTEXT = PulpContainerRepositoryVersionContext
CAPABILITIES = {
"sync": [PluginRequirement("container")],
"pulpexport": [PluginRequirement("container", "2.8.0.dev")],
"tag": [PluginRequirement("container", "2.3.0")]
}


Expand All @@ -115,6 +120,8 @@ class PulpContainerPushRepositoryContext(PulpRepositoryContext):
# CREATE_ID = "repositories_container_container_push_create"
# UPDATE_ID = "repositories_container_container_push_update"
# DELETE_ID = "repositories_container_container_push_delete"
TAG_ID: ClassVar[str] = "repositories_container_push_tag"
UNTAG_ID: ClassVar[str] = "repositories_container_push_untag"
VERSION_CONTEXT = PulpContainerPushRepositoryVersionContext


Expand Down
49 changes: 49 additions & 0 deletions pulpcore/cli/container/repository.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any, Dict

import click
import re

from pulpcore.cli.common.context import (
EntityFieldDefinition,
Expand Down Expand Up @@ -37,8 +38,17 @@

translation = get_translation(__name__)
_ = translation.gettext
VALID_TAG_REGEX = r"^[A-Za-z0-9][A-Za-z0-9._-]*$"


def _tag_callback(ctx: click.Context, param: click.Parameter, value: Any) -> None:
if len(value) == 0:
raise click.ClickException("Please pass a non empty tag name.")
if re.match(VALID_TAG_REGEX, value) is None:
raise click.ClickException("Please pass a valid tag.")

return value

remote_option = resource_option(
"--remote",
default_plugin="container",
Expand Down Expand Up @@ -122,3 +132,42 @@ def sync(
href=repository_href,
body=body,
)


@repository.command(name="tag")
@name_option
@href_option
@click.option("--tag", help=_("Name to tag an image with"), required=True, callback=_tag_callback)
@click.option("--digest", help=_("SHA256 digest of the Manifest file"), required=True)
@pass_repository_context
def add_tag(repository_ctx: PulpRepositoryContext, digest: str, tag: str,) -> None:
if not repository_ctx.capable("tag"):
raise click.ClickException(_("pulp_container 2.3.0 is required to tag images"))

digest = digest.strip()
if not digest.startswith("sha256:"):
digest = f"sha256:{digest}"
if len(digest) != 71: # len("sha256:") + 64
raise click.ClickException("Improper SHA256, please provide a valid 64 digit digest.")

repository_ctx.pulp_ctx.call(
repository_ctx.TAG_ID,
parameters={repository_ctx.HREF: repository_ctx.pulp_href},
body={"tag": tag, "digest": digest},
)


@repository.command(name="untag")
@name_option
@href_option
@click.option("--tag", help=_("Name of tag to remove"), required=True, callback=_tag_callback)
@pass_repository_context
def remove_tag(repository_ctx: PulpRepositoryContext, tag: str) -> None:
if not repository_ctx.capable("tag"):
raise click.ClickException(_("pulp_container 2.3.0 is required to untag images"))

repository_ctx.pulp_ctx.call(
repository_ctx.UNTAG_ID,
parameters={repository_ctx.HREF: repository_ctx.pulp_href},
body={"tag": tag},
)
28 changes: 28 additions & 0 deletions tests/scripts/pulp_container/test_tag.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

# shellcheck source=tests/scripts/config.source
. "$(dirname "$(dirname "$(realpath "$0")")")"/config.source

pulp debug has-plugin --name "container" || exit 3

cleanup() {
pulp container repository destroy --name "cli_test_container_repo" || true
pulp container remote destroy --name "cli_test_container_remote" || true
pulp orphan cleanup || true
}
trap cleanup EXIT

# Prepare
pulp container remote create --name "cli_test_container_remote" --url "$CONTAINER_REMOTE_URL" --upstream-name "$CONTAINER_IMAGE"
pulp container repository create --name "cli_test_container_repository"
pulp container repository sync --name "cli_test_container_repository" --remote "cli_test_container_remote"
pulp container content -t "manifest" list
manifest_digest="$(echo "$OUTPUT" | tr '\r\n' ' ' | jq -r .[0].digest)"

expect_succ pulp container repository tag --name "cli_test_container_repository" --tag "test_tag" --digest "$manifest_digest"
expect_succ pulp container repository version show --repository "cli_test_container_repository" --version "2"
test "$(echo "$OUTPUT" | jq -r '.content_summary.added["container.tag"].count')" -eq "1"

expect_succ pulp container repository untag --name "cli_test_container_repository" --tag "test_tag"
expect_succ pulp container repository version show --repository "cli_test_container_repository" --version "3"
test "$(echo "$OUTPUT" | jq -r '.content_summary.removed["container.tag"].count')" -eq "1"

0 comments on commit 2e75eb4

Please sign in to comment.