Skip to content

Commit

Permalink
Containerapp - Add CLI changes for sticky sessions ingress (#5841)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdaroly authored Apr 6, 2023
1 parent f524a10 commit 3bc39be
Show file tree
Hide file tree
Showing 9 changed files with 2,758 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/containerapp/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Release History
===============
Upcoming
+++++++
* Add CLI support for containerapp ingress sticky-sessions'

0.3.27
++++++
* 'az containerapp secret set': add support for secrets from Key Vault
Expand Down
26 changes: 26 additions & 0 deletions src/containerapp/azext_containerapp/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,32 @@
az containerapp ingress access-restriction list -n MyContainerapp -g MyResourceGroup
"""

helps['containerapp ingress sticky-sessions'] = """
type: group
short-summary: Commands to set Sticky session affinity for a container app.
"""

helps['containerapp ingress sticky-sessions set'] = """
type: command
short-summary: Configure Sticky session for a container app.
examples:
- name: Set affinity to sticky for a container app.
text: |
az containerapp ingress sticky-sessions set -n MyContainerapp -g MyResourceGroup --affinity sticky
- name: Set affinity to none for a container app.
text: |
az containerapp ingress sticky-sessions set -n MyContainerapp -g MyResourceGroup --affinity none
"""

helps['containerapp ingress sticky-sessions show'] = """
type: command
short-summary: Show the Affinity for a container app.
examples:
- name: Show a container app's Sticky affinity configuration.
text: |
az containerapp ingress sticky-sessions show -n MyContainerapp -g MyResourceGroup
"""

# Registry Commands
helps['containerapp registry'] = """
type: group
Expand Down
3 changes: 2 additions & 1 deletion src/containerapp/azext_containerapp/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@
"exposedPort": None,
"traffic": None, # TrafficWeight
"customDomains": None, # [CustomDomain]
"ipSecurityRestrictions": None # [IPSecurityRestrictions]
"ipSecurityRestrictions": None, # [IPSecurityRestrictions]
"stickySessions": None # StickySessions
}

RegistryCredentials = {
Expand Down
3 changes: 3 additions & 0 deletions src/containerapp/azext_containerapp/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ def load_arguments(self, _):
c.argument('revision_weights', nargs='+', options_list=['--revision-weight', c.deprecate(target='--traffic-weight', redirect='--revision-weight')], help="A list of revision weight(s) for the container app. Space-separated values in 'revision_name=weight' format. For latest revision, use 'latest=weight'")
c.argument('label_weights', nargs='+', options_list=['--label-weight'], help="A list of label weight(s) for the container app. Space-separated values in 'label_name=weight' format.")

with self.argument_context('containerapp ingress sticky-sessions') as c:
c.argument('affinity', arg_type=get_enum_type(['sticky', 'none']), help='Whether the affinity for the container app is Sticky or None.')

with self.argument_context('containerapp secret') as c:
c.argument('secrets', nargs='+', options_list=['--secrets', '-s'], help="A list of secret(s) for the container app. Space-separated values in 'key=value' or 'key=keyvaultref:keyvaulturl,identityref:identity' format (where 'key' cannot be longer than 20 characters).")
c.argument('secret_name', help="The name of the secret to show.")
Expand Down
21 changes: 21 additions & 0 deletions src/containerapp/azext_containerapp/_sdk_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,23 @@ def __init__(self, **kwargs):
self.description = kwargs.get('description', None)


class StickySessions(Model):
"""Sticky Sessions of a Container App.
:param name: affinity
:type name: str
"""

_attribute_map = {
'affinity': {'key': 'affinity', 'type': 'str'},
}

def __init__(self, **kwargs):
super(StickySessions, self).__init__(**kwargs)
self.affinity = kwargs.get('affinity', None)


class CustomHostnameAnalysisResult(ProxyResource):
"""Custom domain analysis.
Expand Down Expand Up @@ -2096,6 +2113,8 @@ class Ingress(Model):
If set to false HTTP connections are automatically redirected to HTTPS
connections
:type allow_insecure: bool
:type sticky_sessions: sticky session affinity for Container App. Possible values include:
'none', 'Sticky'
"""

_validation = {
Expand All @@ -2114,6 +2133,7 @@ class Ingress(Model):
'ip_security_restrictions': {'key': 'ipSecurityRestrictions', 'type': '[IPSecurityRestrictions]'},
'client_certificate_mode': {'key': 'clientCertificateMode', 'type': 'str'},
'cors_policy': {'key': 'corsPolicy', 'type': 'CorsPolicy'},
'sticky_sessions': {'key': 'stickySessions', 'type': 'StickySessions'},
}

def __init__(self, **kwargs):
Expand All @@ -2129,6 +2149,7 @@ def __init__(self, **kwargs):
self.ipSecurityRestrictions = kwargs.get('ip_security_restrictions', None)
self.clientCertificateMode = kwargs.get('client_certificate_mode', None)
self.corsPolicy = kwargs.get('cors_policy', None)
self.stickySessions = kwargs.get('sticky_sessions', None)


class LegacyMicrosoftAccount(Model):
Expand Down
4 changes: 4 additions & 0 deletions src/containerapp/azext_containerapp/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ def load_command_table(self, _):
g.custom_command('set', 'set_ingress_traffic', exception_handler=ex_handler_factory())
g.custom_show_command('show', 'show_ingress_traffic')

with self.command_group('containerapp ingress sticky-sessions') as g:
g.custom_command('set', 'set_ingress_sticky_session', exception_handler=ex_handler_factory())
g.custom_show_command('show', 'show_ingress_sticky_session')

with self.command_group('containerapp ingress access-restriction') as g:
g.custom_command('set', 'set_ip_restriction', exception_handler=ex_handler_factory())
g.custom_command('remove', 'remove_ip_restriction')
Expand Down
40 changes: 40 additions & 0 deletions src/containerapp/azext_containerapp/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -2162,6 +2162,46 @@ def show_ip_restrictions(cmd, name, resource_group_name):
return []


def set_ingress_sticky_session(cmd, name, resource_group_name, affinity, no_wait=False):
_validate_subscription_registered(cmd, CONTAINER_APPS_RP)

containerapp_def = None
try:
containerapp_def = ContainerAppClient.show(cmd=cmd, resource_group_name=resource_group_name, name=name)
except:
pass

if not containerapp_def:
raise ResourceNotFoundError(f"The containerapp '{name}' does not exist in group '{resource_group_name}'")

containerapp_patch = {}
safe_set(containerapp_patch, "properties", "configuration", "ingress", "stickySessions", "affinity", value=affinity)
try:
r = ContainerAppClient.update(
cmd=cmd, resource_group_name=resource_group_name, name=name, container_app_envelope=containerapp_patch, no_wait=no_wait)
return r['properties']['configuration']['ingress']
except Exception as e:
handle_raw_exception(e)


def show_ingress_sticky_session(cmd, name, resource_group_name):
_validate_subscription_registered(cmd, CONTAINER_APPS_RP)

containerapp_def = None
try:
containerapp_def = ContainerAppClient.show(cmd=cmd, resource_group_name=resource_group_name, name=name)
except:
pass

if not containerapp_def:
raise ResourceNotFoundError("The containerapp '{}' does not exist".format(name))

try:
return containerapp_def["properties"]["configuration"]["ingress"]
except Exception as e:
raise ValidationError("Ingress must be enabled to enable sticky sessions. Try running `az containerapp ingress -h` for more info.") from e


def show_registry(cmd, name, resource_group_name, server):
_validate_subscription_registered(cmd, CONTAINER_APPS_RP)

Expand Down
Loading

0 comments on commit 3bc39be

Please sign in to comment.