Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ACR changes + bug fixes #77

Merged
merged 5 commits into from
Apr 26, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 45 additions & 14 deletions src/containerapp/azext_containerapp/_up_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ def __init__(self, cmd, name: str, location: str, exists: bool = None):
self.check_exists()

def create(self):
if not self.name:
self.name = get_randomized_name(get_profile_username())
g = create_resource_group(self.cmd, self.name, self.location)
self.exists = True
return g
Expand All @@ -90,6 +88,8 @@ def check_exists(self) -> bool:

def create_if_needed(self):
if not self.check_exists():
if not self.name:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Moved this because I was getting logger warning: "Creating resource group None"

self.name = get_randomized_name(get_profile_username())
logger.warning(f"Creating resoure group '{self.name}'")
self.create()
else:
Expand Down Expand Up @@ -155,23 +155,25 @@ def __init__(
super().__init__(cmd, name, resource_group, exists)
if is_valid_resource_id(name):
self.name = parse_resource_id(name)["name"]
rg = parse_resource_id(name)["resource_group"]
if resource_group.name != rg:
self.resource_group = ResourceGroup(cmd, rg, location)
if "resource_group" in parse_resource_id(name):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Bug fix part 1

rg = parse_resource_id(name)["resource_group"]
if resource_group.name != rg:
self.resource_group = ResourceGroup(cmd, rg, location)
self.location = _get_default_containerapps_location(cmd, location)
self.logs_key = logs_key
self.logs_customer_id = logs_customer_id

def set_name(self, name_or_rid):
if is_valid_resource_id(name_or_rid):
self.name = parse_resource_id(name_or_rid)["name"]
rg = parse_resource_id(name_or_rid)["resource_group"]
if self.resource_group.name != rg:
self.resource_group = ResourceGroup(
self.cmd,
rg,
_get_default_containerapps_location(self.cmd, self.location),
)
if "resource_group" in parse_resource_id(name_or_rid):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Bug fix part 2

rg = parse_resource_id(name_or_rid)["resource_group"]
if self.resource_group.name != rg:
self.resource_group = ResourceGroup(
self.cmd,
rg,
_get_default_containerapps_location(self.cmd, self.location),
)
else:
self.name = name_or_rid

Expand Down Expand Up @@ -274,12 +276,22 @@ def create(self, no_registry=False):
)

def create_acr_if_needed(self):
self.acr.name, self.acr.resource_group.name, acr_found = find_existing_acr(self.cmd, self.env.resource_group.name, self.env.name, self.acr.name, self.acr.resource_group.name, self)
if self.should_create_acr:
logger.warning(
f"Creating Azure Container Registry {self.acr.name} in resource group "
f"{self.acr.resource_group.name}"
)
self.create_acr()
if acr_found:
self.registry_user, self.registry_pass, _ = _get_acr_cred(
self.cmd.cli_ctx, self.acr.name
)
self.registry_server = self.acr.name + ".azurecr.io"
logger.warning(
f"Using Azure Container Registry {self.acr.name} in resource group "
f"{self.acr.resource_group.name}"
)

def create_acr(self):
registry_rg = self.resource_group
Expand Down Expand Up @@ -606,10 +618,10 @@ def _get_registry_details(cmd, app: "ContainerApp"):
else:
registry_rg = app.resource_group.name
user = get_profile_username()
registry_name = app.name.replace("-", "").lower()
registry_name = app.env.name.replace("-", "").lower()
registry_name = (
registry_name
+ str(hash((registry_rg, user, app.name)))
+ str(hash((app.env.resource_group.name, app.env.name)))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Change hash so it's more unique, as per @StrawnSC recommendation

Copy link
Collaborator

Choose a reason for hiding this comment

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

Second part of the change needed here is to reconstruct the hash when we are trying to find the ACR from the env

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@StrawnSC I think the hash uses object of string instead of characters, so it is impossible to reconstruct the hash, you'll need to test this

.replace("-", "")
.replace(".", "")[:10]
) # cap at 15 characters total
Expand Down Expand Up @@ -713,3 +725,22 @@ def up_output(app):
logger.warning(
f"See full output using: az containerapp show -n {app.name} -g {app.resource_group.name} \n"
)


def find_existing_acr(cmd, resource_group_name, env_name, default_name, default_rg, app: "ContainerApp"):
from azure.cli.command_modules.acr.custom import acr_list as list_acr
from azure.cli.command_modules.acr._client_factory import cf_acr_registries
client = cf_acr_registries(cmd.cli_ctx)
acr_list = list_acr(client, resource_group_name)
acr_list = list(acr_list)
acr = None
acr_list = [a for a in acr_list if env_name.lower().replace('-','') in a.name.lower()]

if acr_list:
acr = acr_list[0]

if acr:
app.should_create_acr = False
return acr.name, parse_resource_id(acr.id)["resource_group"], True
else:
return default_name, default_rg, False