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

Revision improvements #113

Merged
merged 14 commits into from
May 27, 2022
5 changes: 3 additions & 2 deletions src/containerapp/azext_containerapp/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ def load_arguments(self, _):
c.argument('revision', help='Name of the revision.')
c.argument('label', help='Name of the label.')
c.argument('yes', options_list=['--yes', '-y'], help='Do not prompt for confirmation.')
c.argument('label1', help='Name of the label to be swapped with label2.')
c.argument('label2', help='Name of the label to be swapped with label1.')

with self.argument_context('containerapp revision label') as c:
c.argument('labels', nargs='*', help='Labels to be swapped.')

with self.argument_context('containerapp ingress') as c:
c.argument('allow_insecure', help='Allow insecure connections for ingress traffic.')
Expand Down
14 changes: 14 additions & 0 deletions src/containerapp/azext_containerapp/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,20 @@ def _update_weights(containerapp_def, revision_weights, old_weight_sum):
weight["weight"] += 1


def _update_traffic_size(containerapp_def):
import sys
traffic_def = containerapp_def["properties"]["configuration"]["ingress"]["traffic"]
index = 0
while (sys.getsizeof(json.dumps(traffic_def)) - sys.getsizeof("")) > 4096 and index < len(traffic_def):
weight = traffic_def[index]
# need to check if latest revision?
if weight["weight"] == 0:
traffic_def.pop(index)
index += 1
if (sys.getsizeof(json.dumps(traffic_def)) - sys.getsizeof("")) > 4096:
raise ValidationError("Traffic size request cannot exceed 4096Bytes.")


def _validate_traffic_sum(revision_weights):
weight_sum = sum([int(w.split('=', 1)[1]) for w in revision_weights if len(w.split('=', 1)) == 2 and _validate_weight(w.split('=', 1)[1])])
if weight_sum > 100:
Expand Down
37 changes: 22 additions & 15 deletions src/containerapp/azext_containerapp/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
_update_revision_env_secretrefs, _get_acr_cred, safe_get, await_github_action, repo_url_to_name,
validate_container_app_name, _update_weights, get_vnet_location, register_provider_if_needed,
generate_randomized_cert_name, _get_name, load_cert_file, check_cert_name_availability,
validate_hostname, patch_new_custom_domain, get_custom_domains, _validate_revision_name)
validate_hostname, patch_new_custom_domain, get_custom_domains, _validate_revision_name, _update_traffic_size)


from ._ssh_utils import (SSH_DEFAULT_ENCODING, WebSocketConnection, read_ssh, get_stdin_writer, SSH_CTRL_C_MSG,
Expand Down Expand Up @@ -1395,8 +1395,8 @@ def add_revision_label(cmd, resource_group_name, revision, label, name=None, no_
if not containerapp_def:
raise ResourceNotFoundError(f"The containerapp '{name}' does not exist in group '{resource_group_name}'")

if "ingress" not in containerapp_def['properties']['configuration'] and "traffic" not in containerapp_def['properties']['configuration']['ingress']:
raise ValidationError("Ingress and traffic weights are required to set labels.")
if "ingress" not in containerapp_def['properties']['configuration'] or "traffic" not in containerapp_def['properties']['configuration']['ingress']:
raise ValidationError("Ingress and traffic weights are required to add labels.")

traffic_weight = containerapp_def['properties']['configuration']['ingress']['traffic'] if 'traffic' in containerapp_def['properties']['configuration']['ingress'] else {}

Expand Down Expand Up @@ -1444,10 +1444,13 @@ def add_revision_label(cmd, resource_group_name, revision, label, name=None, no_
handle_raw_exception(e)


def swap_revision_label(cmd, name, resource_group_name, label1, label2, no_wait=False):
def swap_revision_label(cmd, name, resource_group_name, labels, no_wait=False):
_validate_subscription_registered(cmd, CONTAINER_APPS_RP)

if label1 == label2:
if not labels or len(labels) != 2:
raise ArgumentUsageError("Usage error: --labels requires two labels to be swapped.")

if labels[0] == labels[1]:
raise ArgumentUsageError("Label names to be swapped must be different.")

containerapp_def = None
Expand All @@ -1459,8 +1462,8 @@ def swap_revision_label(cmd, name, resource_group_name, label1, label2, no_wait=
if not containerapp_def:
raise ResourceNotFoundError(f"The containerapp '{name}' does not exist in group '{resource_group_name}'")

if "ingress" not in containerapp_def['properties']['configuration'] and "traffic" not in containerapp_def['properties']['configuration']['ingress']:
raise ValidationError("Ingress and traffic weights are required to set labels.")
if "ingress" not in containerapp_def['properties']['configuration'] or "traffic" not in containerapp_def['properties']['configuration']['ingress']:
raise ValidationError("Ingress and traffic weights are required to swap labels.")

traffic_weight = containerapp_def['properties']['configuration']['ingress']['traffic'] if 'traffic' in containerapp_def['properties']['configuration']['ingress'] else {}

Expand All @@ -1469,18 +1472,20 @@ def swap_revision_label(cmd, name, resource_group_name, label1, label2, no_wait=
label2_found = False
for weight in traffic_weight:
if "label" in weight:
if weight["label"].lower() == label1.lower():
if weight["label"].lower() == labels[0].lower():
if not label1_found:
label1_found = True
weight["label"] = label2
elif weight["label"].lower() == label2.lower():
weight["label"] = labels[1]
elif weight["label"].lower() == labels[1].lower():
if not label2_found:
label2_found = True
weight["label"] = label1
weight["label"] = labels[0]
if not label1_found and not label2_found:
raise ArgumentUsageError(f"Could not find label '{labels[0]}' nor label '{labels[1]}' in traffic.")
if not label1_found:
raise ArgumentUsageError(f"Could not find label '{label1}' in traffic.")
raise ArgumentUsageError(f"Could not find label '{labels[0]}' in traffic.")
if not label2_found:
raise ArgumentUsageError(f"Could not find label '{label2}' in traffic.")
raise ArgumentUsageError(f"Could not find label '{labels[1]}' in traffic.")

containerapp_patch_def = {}
containerapp_patch_def['properties'] = {}
Expand Down Expand Up @@ -1509,8 +1514,8 @@ def remove_revision_label(cmd, resource_group_name, name, label, no_wait=False):
if not containerapp_def:
raise ResourceNotFoundError(f"The containerapp '{name}' does not exist in group '{resource_group_name}'")

if "ingress" not in containerapp_def['properties']['configuration'] and "traffic" not in containerapp_def['properties']['configuration']['ingress']:
raise ValidationError("Ingress and traffic weights are required to set labels.")
if "ingress" not in containerapp_def['properties']['configuration'] or "traffic" not in containerapp_def['properties']['configuration']['ingress']:
raise ValidationError("Ingress and traffic weights are required to remove labels.")

traffic_weight = containerapp_def['properties']['configuration']['ingress']['traffic']

Expand Down Expand Up @@ -1662,6 +1667,8 @@ def set_ingress_traffic(cmd, name, resource_group_name, label_weights=None, revi

_update_weights(containerapp_def, revision_weights, old_weight_sum)

# _update_traffic_size(containerapp_def)

containerapp_patch_def = {}
containerapp_patch_def['properties'] = {}
containerapp_patch_def['properties']['configuration'] = {}
Expand Down