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

[Container App]: Remove quota check for 'az containerapps env create' #5548

Merged
merged 9 commits into from
Nov 22, 2022
Merged
4 changes: 4 additions & 0 deletions src/containerapp/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
Release History
===============

0.3.16
++++++
* Remove quota check for 'az containerapp up' and 'az containerapp env create'.

0.3.15
++++++
* Add 'az containerapp containerapp ingress ip-restriction' command group to manage IP restrictions on the ingress of a container app.
Expand Down
2 changes: 0 additions & 2 deletions src/containerapp/azext_containerapp/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
LOG_ANALYTICS_RP = "Microsoft.OperationalInsights"
CONTAINER_APPS_RP = "Microsoft.App"

MAX_ENV_PER_LOCATION = 5

MICROSOFT_SECRET_SETTING_NAME = "microsoft-provider-authentication-secret"
FACEBOOK_SECRET_SETTING_NAME = "facebook-provider-authentication-secret"
GITHUB_SECRET_SETTING_NAME = "github-provider-authentication-secret"
Expand Down
54 changes: 41 additions & 13 deletions src/containerapp/azext_containerapp/_up_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
trigger_workflow,
_ensure_location_allowed,
register_provider_if_needed,
validate_environment_location
validate_environment_location,
list_environment_locations
)

from ._constants import (MAXIMUM_SECRET_LENGTH,
Expand Down Expand Up @@ -203,19 +204,46 @@ def create_if_needed(self, app_name):
) # TODO use .info()

def create(self):
self.location = validate_environment_location(self.cmd, self.location)
register_provider_if_needed(self.cmd, LOG_ANALYTICS_RP)
env = create_managed_environment(
self.cmd,
self.name,
location=self.location,
resource_group_name=self.resource_group.name,
logs_key=self.logs_key,
logs_customer_id=self.logs_customer_id,
disable_warnings=True,
)
self.exists = True
return env

if self.location:
self.location = validate_environment_location(self.cmd, self.location)

env = create_managed_environment(
self.cmd,
self.name,
location=self.location,
resource_group_name=self.resource_group.name,
logs_key=self.logs_key,
logs_customer_id=self.logs_customer_id,
disable_warnings=True,
)
self.exists = True

return env
else:
res_locations = list_environment_locations(self.cmd)
for loc in res_locations:
try:
env = create_managed_environment(
self.cmd,
self.name,
location=loc,
resource_group_name=self.resource_group.name,
logs_key=self.logs_key,
logs_customer_id=self.logs_customer_id,
disable_warnings=True,
)

self.exists = True
self.location = loc

return env
except Exception as ex:
logger.info(
f"Failed to create ManagedEnvironment in {loc} due to {ex}"
)
raise ValidationError("Can not find a region with quota to create ManagedEnvironment")

def get_rid(self):
rid = self.name
Expand Down
29 changes: 3 additions & 26 deletions src/containerapp/azext_containerapp/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1506,42 +1506,19 @@ def is_registry_msi_system(identity):


def validate_environment_location(cmd, location):
from ._constants import MAX_ENV_PER_LOCATION
runefa marked this conversation as resolved.
Show resolved Hide resolved
from .custom import list_managed_environments
env_list = list_managed_environments(cmd)

locations = [loc["location"] for loc in env_list]
locations = list(set(locations)) # remove duplicates

location_count = {}
for loc in locations:
location_count[loc] = len([e for e in env_list if e["location"] == loc]) # pylint: disable=used-before-assignment

disallowed_locations = []
for _, value in enumerate(location_count):
if location_count[value] > MAX_ENV_PER_LOCATION - 1:
disallowed_locations.append(value)

res_locations = list_environment_locations(cmd)
res_locations = [loc for loc in res_locations if loc not in disallowed_locations]

allowed_locs = ", ".join(res_locations)

if location:
try:
_ensure_location_allowed(cmd, location, CONTAINER_APPS_RP, "managedEnvironments")

return location
except Exception as e: # pylint: disable=broad-except
raise ValidationError("You cannot create a Containerapp environment in location {}. List of eligible locations: {}.".format(location, allowed_locs)) from e

if len(res_locations) > 0:
if not location:
logger.warning("Creating environment on location %s.", res_locations[0])
return res_locations[0]
if location in disallowed_locations:
raise ValidationError("You have more than {} environments in location {}. List of eligible locations: {}.".format(MAX_ENV_PER_LOCATION, location, allowed_locs))
Zijian-Ju marked this conversation as resolved.
Show resolved Hide resolved
return location
else:
raise ValidationError("You cannot create any more environments. Environments are limited to {} per location in a subscription. Please specify an existing environment using --environment.".format(MAX_ENV_PER_LOCATION))
return res_locations[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

add null check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

res_location will not be empty after the change as we don't filter by remaining quota anymore



def list_environment_locations(cmd):
Expand Down
2 changes: 1 addition & 1 deletion src/containerapp/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# TODO: Confirm this is the right version number you want and it matches your
# HISTORY.rst entry.

VERSION = '0.3.15'
VERSION = '0.3.16'

# The full list of classifiers is available at
# https://pypi.python.org/pypi?%3Aaction=list_classifiers
Expand Down