Skip to content

Commit

Permalink
bug fix where commands fail if providing registry creds
Browse files Browse the repository at this point in the history
  • Loading branch information
StrawnSC committed Apr 22, 2022
1 parent 78b1c50 commit 0ba7185
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/containerapp/azext_containerapp/.flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[flake8]
ignore =
W503 # line break before binary operator, not compliant with PEP 8
E203 # whitespace before ':', not compliant with PEP 8
35 changes: 25 additions & 10 deletions src/containerapp/azext_containerapp/_up_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
MutuallyExclusiveArgumentError)
from azure.cli.core.commands.client_factory import get_subscription_id
from azure.cli.command_modules.appservice._create_util import check_resource_group_exists
from azure.cli.command_modules.acr.custom import acr_show
from azure.cli.core.commands.client_factory import get_mgmt_service_client
from azure.mgmt.containerregistry import ContainerRegistryManagementClient
from knack.log import get_logger

from msrestazure.tools import parse_resource_id, is_valid_resource_id, resource_id
Expand Down Expand Up @@ -382,16 +385,19 @@ def _get_env_and_group_from_log_analytics(cmd, resource_group_name, env:'Contain

def _get_acr_from_image(cmd, app):
if app.image is not None and "azurecr.io" in app.image:
app.registry_server = app.image.split('/')[0] # TODO what if this conflicts with registry_server param?
parsed = urlparse(app.image)
registry_name = (parsed.netloc if parsed.scheme else parsed.path).split('.')[0]
if app.registry_user is None or app.registry_pass is None:
logger.info('No credential was provided to access Azure Container Registry. Trying to look up...')
app.registry_server = app.image.split('/')[0] # TODO what if this conflicts with registry_server param?
parsed = urlparse(app.image)
registry_name = (parsed.netloc if parsed.scheme else parsed.path).split('.')[0]
try:
app.registry_user, app.registry_pass, registry_rg = _get_acr_cred(cmd.cli_ctx, registry_name)
app.acr = AzureContainerRegistry(registry_name, ResourceGroup(cmd, registry_rg, None, None))
except Exception as ex:
raise RequiredArgumentMissingError('Failed to retrieve credentials for container registry. Please provide the registry username and password') from ex
try:
app.registry_user, app.registry_pass, registry_rg = _get_acr_cred(cmd.cli_ctx, registry_name)
app.acr = AzureContainerRegistry(registry_name, ResourceGroup(cmd, registry_rg, None, None))
except Exception as ex:
raise RequiredArgumentMissingError('Failed to retrieve credentials for container registry. Please provide the registry username and password') from ex
else:
acr_rg = _get_acr_rg(app)
app.acr = AzureContainerRegistry(name=registry_name, resource_group=ResourceGroup(app.cmd, acr_rg, None, None))


def _get_registry_from_app(app):
Expand All @@ -401,20 +407,27 @@ def _get_registry_from_app(app):
app.registry_server = containerapp_def["properties"]["configuration"]["registries"][0]["server"]


def _get_acr_rg(app):
registry_name = app.registry_server[:app.registry_server.rindex(".azurecr.io")]
client = get_mgmt_service_client(app.cmd.cli_ctx, ContainerRegistryManagementClient).registries
return parse_resource_id(acr_show(app.cmd, client, registry_name).id)["resource_group"]

def _get_registry_details(cmd, app: 'ContainerApp'):
registry_rg = None
registry_name = None
if app.registry_server:
if "azurecr.io" not in app.registry_server:
raise ValidationError("Cannot supply non-Azure registry when using --source.")
parsed = urlparse(app.registry_server)
registry_name = (parsed.netloc if parsed.scheme else parsed.path).split('.')[0]
if app.registry_user is None or app.registry_pass is None:
logger.info('No credential was provided to access Azure Container Registry. Trying to look up...')
parsed = urlparse(app.registry_server)
registry_name = (parsed.netloc if parsed.scheme else parsed.path).split('.')[0]
try:
app.registry_user, app.registry_pass, registry_rg = _get_acr_cred(cmd.cli_ctx, registry_name)
except Exception as ex:
raise RequiredArgumentMissingError('Failed to retrieve credentials for container registry. Please provide the registry username and password') from ex
else:
registry_rg = _get_acr_rg(app)
else:
registry_rg = app.resource_group.name
user = get_profile_username()
Expand All @@ -423,6 +436,7 @@ def _get_registry_details(cmd, app: 'ContainerApp'):
registry_name = f"ca{registry_name}acr" # ACR names must start + end in a letter
app.registry_server = registry_name + ".azurecr.io"
app.should_create_acr = True

app.acr = AzureContainerRegistry(registry_name, ResourceGroup(cmd, registry_rg, None, None))


Expand All @@ -438,6 +452,7 @@ def _set_up_defaults(cmd, name, resource_group_name, logs_customer_id, location,
# get ACR details from --image, if possible
_get_acr_from_image(cmd, app)


def _create_github_action(app:'ContainerApp',
env:'ContainerAppEnvironment',
service_principal_client_id, service_principal_client_secret, service_principal_tenant_id,
Expand Down
4 changes: 3 additions & 1 deletion src/containerapp/azext_containerapp/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -2007,7 +2007,7 @@ def containerapp_up(cmd,
service_principal_tenant_id=None):
from ._up_utils import (_validate_up_args, _reformat_image, _get_dockerfile_content, _get_ingress_and_target_port,
ResourceGroup, ContainerAppEnvironment, ContainerApp, _get_registry_from_app,
_get_registry_details, _create_github_action, _set_up_defaults, up_output)
_get_registry_details, _create_github_action, _set_up_defaults, up_output, AzureContainerRegistry)

dockerfile="Dockerfile" # for now the dockerfile name must be "Dockerfile" (until GH actions API is updated)

Expand All @@ -2033,6 +2033,8 @@ def containerapp_up(cmd,
env.create_if_needed(name)
app.create_acr_if_needed()



if source:
app.run_acr_build(dockerfile, source)

Expand Down

0 comments on commit 0ba7185

Please sign in to comment.