Skip to content

Image import handles more scenarios#152

Merged
NatalieGong merged 17 commits into
devfrom
natalie/image-import-advanced
May 16, 2018
Merged

Image import handles more scenarios#152
NatalieGong merged 17 commits into
devfrom
natalie/image-import-advanced

Conversation

@NatalieGong
Copy link
Copy Markdown


This checklist is used to make sure that common guidelines for a pull request are followed.

  • The PR has modified HISTORY.rst describing any customer-facing, functional changes. Note that this does not include changes only to help content. (see Modifying change log).

  • I adhere to the Command Guidelines.

djyou and others added 2 commits May 10, 2018 10:47
Merge changes from upstream
* Add support for image import

* Enable image import commands

* Enable Image Import Commands

* Error handling

* Rewrite acr_image_import + Help messages

* Image import without support of docker hub and cross-subscription registires

* Listify tags and repository arguments for image import

* Support import image cross subscription

* Help message for arguments
@NatalieGong NatalieGong requested a review from djyou as a code owner May 15, 2018 03:13
@NatalieGong NatalieGong force-pushed the natalie/image-import-advanced branch from ee791f7 to c409f36 Compare May 15, 2018 03:26
nataliegong added 2 commits May 14, 2018 20:41
c.argument('source_image', help='A fully qualified image identifier.')
c.argument('tags', nargs='+', options_list=['--image', '-t'], help="Space-separated list of images in the 'repository:tag' format where tag is optional.")
c.argument('repository', nargs='+', help='Space-separared list of image repository without tag to do a manifest only copy.')
c.argument('target', options_list=['--image', '-t'], help="The image repository and optionally a tag in the 'repository:tag' format where tag is optional.", action='append')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just use target_tags from the SDK since there is an options_list to override it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The help doesn't read right. Can you reuse the same help from build-task --image?


IMPORT_NOT_SUPPORTED = "Image imports are only supported for managed registries."
INVALID_SOURCE_IMAGE = "Please specify source image in the form of 'regsitry.azurecr.io/repository[:tag]'."
INVALID_SOURCE_IMAGE = "Please specify source image in the form of 'registry.azurecr.io/repository[:tag]"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing an ending single quote.

IMPORT_NOT_SUPPORTED = "Image imports are only supported for managed registries."
INVALID_SOURCE_IMAGE = "Please specify source image in the form of 'regsitry.azurecr.io/repository[:tag]'."
INVALID_SOURCE_IMAGE = "Please specify source image in the form of 'registry.azurecr.io/repository[:tag]"
SOURCE_REGISTRY_NOT_FOUND = "Source registry cannot be found in the current subscription. Please specify the full resource id for it:"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resource ID

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: add a space after :.

def acr_image_import(cmd,
client,
registry_name,
registry_name,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing whitespace.

try:
resource_id = get_resource_id_by_registry_name(cmd.cli_ctx, source_registry)
except CLIError:
resource_id = input(SOURCE_REGISTRY_NOT_FOUND)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle non-tty

resource_id = get_resource_id_by_registry_name(cmd.cli_ctx, source_registry)
try:
resource_id = get_resource_id_by_registry_name(cmd.cli_ctx, source_registry)
except CLIError:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you only want to catch when registry couldn't be found, but throw other errors out.

c.argument('tags', nargs='+', options_list=['--image', '-t'], help="Space-separated list of images in the 'repository:tag' format where tag is optional.")
c.argument('repository', nargs='+', help='Space-separared list of image repository without tag to do a manifest only copy.')
c.argument('target_tags', options_list=['--image', '-t'], help="The image repository and optionally a tag in the 'repository:tag' format.", action='append')
c.argument('repository', help='The repository names to do a manifest only copy.', action='append')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repository name?

result = list(result)

result = todict(result, AzCliCommandInvoker.remove_additional_prop_layer)
result = todict(result)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to modify core?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert this change, rebase from dev branch, create a new venv, run dev setup again, this problem should go away.

text: >
az acr image import -n yugongTest --resource-id /subscriptions/dfb63c8c-7c89-4ef8-af13-75c1d873c895/resourceGroups/yugong/providers/Microsoft.ContainerRegistry/registries/yugongeast --source-image yugongeast.azurecr.io/builder:latest -t test:test .
az acr image import -n targetRegistry --resource-id /subscriptions/dfb63c8c-7c89-4ef8-af13-75c1d873c895/resourceGroups/yugong/providers/Microsoft.ContainerRegistry/registries/yugongeast --source-image yugongeast.azurecr.io/builder:latest
-t test:test
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace your names with some common strings, also the sub id with empty guid, see other examples.

resource_id = get_resource_id_by_registry_name(cmd.cli_ctx, source_registry)
source_image = source_image[slash + 1 :]
resource_id = get_resource_id_by_registry_name(cmd.cli_ctx, source_registry, SOURCE_REGISTRY_NOT_FOUND)
registry_name_from_resource_id = get_registry_name_by_resource_id(
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we already have registry name from source_registry?

if tags is None and repository is None:
tags = [source_image]
image_source = ImportSource(
resource_id=resource_id, source_image=source_image)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert this line break. Line length max is 120.

resource_id = get_resource_id_by_registry_name(cmd.cli_ctx, source_registry, SOURCE_REGISTRY_NOT_FOUND)
registry_name_from_resource_id = get_registry_name_by_resource_id(
resource_id)
if source_registry != registry_name_from_resource_id:
Copy link
Copy Markdown

@djyou djyou May 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not getting this condition. resource_id is obtained from source_registry, registry_name_from_resource_id is obtained from resource_id, shouldn't registry_name_from_resource_id always match source_registry?


if not elements or len(elements) != 1:
try:
resource_id = prompt(msg)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method would be confusing here, at least it shouldn't be in _utils as it won't be used by any other commands due to this special handling.

I would suggest that you do a get_resource_id_by_registry_name but instead of throwing, returning an empty result so you know the registry doesn't exist in the subscription.

"""Returns the resource ID for the container registry.
:param str login_server: The login server of the container registry.
"""
from .custom import acr_list
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to import. Call client.list directly here.

"""
from .custom import acr_list
registry_list = acr_list(client)
elements = [item for item in registry_list if item.login_server.lower() == login_server.lower()]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a var = login_server.lower() and reuse it, rather than calling lower() every time.

elements = [item for item in registry_list if item.login_server.lower() == login_server.lower()]

if not elements or len(elements) != 1:
return ""
Copy link
Copy Markdown

@djyou djyou May 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

edit: rename this method to get_registry_by_login_server and return the Registry object if it exists in the current sub, None otherwise, so that this method can be generic enough to be used by others.

@@ -1,54 +1,69 @@
from knack.util import CLIError
from knack.prompting import prompt, NoTTYException
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this line to where they are used for better perf.


from azure.mgmt.containerregistry.v2017_10_01.models import SkuName, Sku

#from .custom import acr_list
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove.


if resource_id and resource_id_from_login_server and resource_id_from_login_server != resource_id:
raise CLIError("Registry mismatch. Please check either source-image or resource ID " \
"to make sure that they are referring to the same registry and give another try.")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: and try again.

@NatalieGong NatalieGong merged commit 9309805 into dev May 16, 2018
@NatalieGong NatalieGong deleted the natalie/image-import-advanced branch May 16, 2018 03:15
NatalieGong pushed a commit that referenced this pull request May 30, 2018
* Enable Image Import Commands (#148)

* Add support for image import

* Enable image import commands

* Enable Image Import Commands

* Error handling

* Rewrite acr_image_import + Help messages

* Image import without support of docker hub and cross-subscription registires

* Listify tags and repository arguments for image import

* Support import image cross subscription

* Help message for arguments

* Enable Image Import Commands with Prompt for resource_id

* Add help message

* .

* Move tty to _utils

* Enable Image Import Commands with Prompt for resource_id

* Add help message

* .

* Move tty to _utils

* Move prompt out of _util

* .

* Validate using login server

* .

* .
NatalieGong pushed a commit that referenced this pull request May 30, 2018
* Enable Image Import Commands (#148)

* Add support for image import

* Enable image import commands

* Enable Image Import Commands

* Error handling

* Rewrite acr_image_import + Help messages

* Image import without support of docker hub and cross-subscription registires

* Listify tags and repository arguments for image import

* Support import image cross subscription

* Help message for arguments

* Enable Image Import Commands with Prompt for resource_id

* Add help message

* .

* Move tty to _utils

* Enable Image Import Commands with Prompt for resource_id

* Add help message

* .

* Move tty to _utils

* Move prompt out of _util

* .

* Validate using login server

* .

* .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants