Skip to content

Commit

Permalink
replace functions in aks-preview with the ones in azure-cli (#6612)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinForReal authored Aug 14, 2023
1 parent ff6cb60 commit cb18200
Show file tree
Hide file tree
Showing 15 changed files with 953 additions and 1,332 deletions.
4 changes: 3 additions & 1 deletion src/aks-preview/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ If there is no rush to release a new version, please just add a description of t

To release a new version, please select a new version number (usually plus 1 to last patch version, X.Y.Z -> Major.Minor.Patch, more details in `\doc <https://semver.org/>`_), and then add a new section named as the new version number in this file, the content should include the new modifications and everything from the *Pending* section. Finally, update the `VERSION` variable in `setup.py` with this new version number.

Pending
0.5.152
++++++
* move loadbalancer/natgateway util functions to azure-cli and update reference in aks-preview project.
* bump azure-cli to 2.49

0.5.151
+++++++
Expand Down
6 changes: 4 additions & 2 deletions src/aks-preview/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ Dependency between aks-preview and azure-cli/acs (azure-cli-core)
- >= `\2.38.0 <https://github.com/Azure/azure-cli/releases/tag/azure-cli-2.38.0>`_, 2022/07/05
* - 0.5.119 ~ 0.5.124
- >= `\2.43.0 <https://github.com/Azure/azure-cli/releases/tag/azure-cli-2.43.0>`_, 2022/12/06
* - 0.5.125 ~ latest
- >= `\2.44.0 <https://github.com/Azure/azure-cli/releases/tag/azure-cli-2.44.0>`_, 2023/01/10
* - 0.5.125 ~ 0.5.150
- >= `\2.44.0 <https://github.com/Azure/azure-cli/releases/tag/azure-cli-2.44.0>`_, 2023/01/10
* - 0.5.152 ~ latest
- >= `\2.49.0 <https://github.com/Azure/azure-cli/releases/tag/azure-cli-2.44.0>`_, 2023/05/23
5 changes: 0 additions & 5 deletions src/aks-preview/azext_aks_preview/_consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@
CONST_MANAGED_CLUSTER_SKU_TIER_FREE = "free"
CONST_MANAGED_CLUSTER_SKU_TIER_STANDARD = "standard"

# outbound type
CONST_OUTBOUND_TYPE_LOAD_BALANCER = "loadBalancer"
CONST_OUTBOUND_TYPE_USER_DEFINED_ROUTING = "userDefinedRouting"
CONST_OUTBOUND_TYPE_MANAGED_NAT_GATEWAY = "managedNATGateway"
CONST_OUTBOUND_TYPE_USER_ASSIGNED_NAT_GATEWAY = "userAssignedNATGateway"
CONST_OUTBOUND_MIGRATION_MULTIZONE_TO_NATGATEWAY_MSG = "Warning: this AKS cluster has multi-zonal nodepools, but NAT Gateway is not currently zone redundant. Migrating outbound connectivity to NAT Gateway could lead to a reduction in zone redundancy for this cluster. Continue?"
# load balancer backend pool type
CONST_LOAD_BALANCER_BACKEND_POOL_TYPE_NODE_IP = "nodeIP"
Expand Down
123 changes: 11 additions & 112 deletions src/aks-preview/azext_aks_preview/_loadbalancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,22 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from distutils.version import StrictVersion
from types import SimpleNamespace

from knack.log import get_logger

from azure.cli.command_modules.acs._loadbalancer import (
is_load_balancer_profile_provided as _is_load_balancer_profile_provided,
configure_load_balancer_profile as _configure_load_balancer_profile,
)
logger = get_logger(__name__)


def set_load_balancer_sku(sku, kubernetes_version):
if sku:
return sku
if kubernetes_version and StrictVersion(kubernetes_version) < StrictVersion("1.13.0"):
logger.warning('Setting load_balancer_sku to basic as it is not specified and kubernetes'
'version(%s) less than 1.13.0 only supports basic load balancer SKU\n',
kubernetes_version)
return "basic"
return "standard"


def update_load_balancer_profile(managed_outbound_ip_count, managed_outbound_ipv6_count, outbound_ips,
outbound_ip_prefixes, outbound_ports, idle_timeout, backend_pool_type, profile, models):
"""parse and update an existing load balancer profile"""
if not is_load_balancer_profile_provided(managed_outbound_ip_count, managed_outbound_ipv6_count, outbound_ips,
outbound_ip_prefixes, outbound_ports, backend_pool_type, idle_timeout):
if not (_is_load_balancer_profile_provided(managed_outbound_ip_count, managed_outbound_ipv6_count, outbound_ips,
outbound_ip_prefixes, outbound_ports, idle_timeout) or backend_pool_type):
return profile
if not profile:
if profile is None:
if isinstance(models, SimpleNamespace):
ManagedClusterLoadBalancerProfile = models.ManagedClusterLoadBalancerProfile
else:
Expand All @@ -41,8 +31,8 @@ def update_load_balancer_profile(managed_outbound_ip_count, managed_outbound_ipv
def create_load_balancer_profile(managed_outbound_ip_count, managed_outbound_ipv6_count, outbound_ips,
outbound_ip_prefixes, outbound_ports, idle_timeout, backend_pool_type, models):
"""parse and build load balancer profile"""
if not is_load_balancer_profile_provided(managed_outbound_ip_count, managed_outbound_ipv6_count, outbound_ips,
outbound_ip_prefixes, outbound_ports, backend_pool_type, idle_timeout):
if not (_is_load_balancer_profile_provided(managed_outbound_ip_count, managed_outbound_ipv6_count, outbound_ips,
outbound_ip_prefixes, outbound_ports, idle_timeout) or backend_pool_type):
return None

if isinstance(models, SimpleNamespace):
Expand All @@ -58,99 +48,8 @@ def configure_load_balancer_profile(managed_outbound_ip_count, managed_outbound_
outbound_ip_prefixes, outbound_ports, idle_timeout, backend_pool_type, profile, models):
"""configure a load balancer with customer supplied values"""

outbound_ip_resources = _get_load_balancer_outbound_ips(outbound_ips, models)
outbound_ip_prefix_resources = _get_load_balancer_outbound_ip_prefixes(outbound_ip_prefixes, models)

if (
managed_outbound_ip_count or
managed_outbound_ipv6_count or
outbound_ip_resources or
outbound_ip_prefix_resources
):
# ips -> i_ps due to track 2 naming issue
profile.outbound_i_ps = None
profile.outbound_ip_prefixes = None
profile.managed_outbound_i_ps = None

if managed_outbound_ip_count or managed_outbound_ipv6_count:
if isinstance(models, SimpleNamespace):
ManagedClusterLoadBalancerProfileManagedOutboundIPs = (
models.ManagedClusterLoadBalancerProfileManagedOutboundIPs
)
else:
ManagedClusterLoadBalancerProfileManagedOutboundIPs = models.get(
"ManagedClusterLoadBalancerProfileManagedOutboundIPs"
)
profile.managed_outbound_i_ps = ManagedClusterLoadBalancerProfileManagedOutboundIPs()
if managed_outbound_ip_count:
profile.managed_outbound_i_ps.count = managed_outbound_ip_count
if managed_outbound_ipv6_count:
profile.managed_outbound_i_ps.count_ipv6 = managed_outbound_ipv6_count
if outbound_ip_resources:
if isinstance(models, SimpleNamespace):
ManagedClusterLoadBalancerProfileOutboundIPs = models.ManagedClusterLoadBalancerProfileOutboundIPs
else:
ManagedClusterLoadBalancerProfileOutboundIPs = models.get(
"ManagedClusterLoadBalancerProfileOutboundIPs"
)
# ips -> i_ps due to track 2 naming issue
profile.outbound_i_ps = ManagedClusterLoadBalancerProfileOutboundIPs(
public_i_ps=outbound_ip_resources
)
if outbound_ip_prefix_resources:
if isinstance(models, SimpleNamespace):
ManagedClusterLoadBalancerProfileOutboundIPPrefixes = (
models.ManagedClusterLoadBalancerProfileOutboundIPPrefixes
)
else:
ManagedClusterLoadBalancerProfileOutboundIPPrefixes = models.get(
"ManagedClusterLoadBalancerProfileOutboundIPPrefixes"
)
profile.outbound_ip_prefixes = ManagedClusterLoadBalancerProfileOutboundIPPrefixes(
public_ip_prefixes=outbound_ip_prefix_resources
)
if outbound_ports:
profile.allocated_outbound_ports = outbound_ports
if idle_timeout:
profile.idle_timeout_in_minutes = idle_timeout
profile = _configure_load_balancer_profile(managed_outbound_ip_count, managed_outbound_ipv6_count, outbound_ips,
outbound_ip_prefixes, outbound_ports, idle_timeout, profile, models)
if backend_pool_type:
profile.backend_pool_type = backend_pool_type
return profile


def is_load_balancer_profile_provided(managed_outbound_ip_count, managed_outbound_ipv6_count, outbound_ips,
ip_prefixes, outbound_ports, backend_pool_type, idle_timeout):
return any([managed_outbound_ip_count,
managed_outbound_ipv6_count,
outbound_ips,
ip_prefixes,
outbound_ports,
idle_timeout,
backend_pool_type])


def _get_load_balancer_outbound_ips(load_balancer_outbound_ips, models):
"""parse load balancer profile outbound IP ids and return an array of references to the outbound IP resources"""
load_balancer_outbound_ip_resources = None
if isinstance(models, SimpleNamespace):
ResourceReference = models.ResourceReference
else:
ResourceReference = models.get("ResourceReference")
if load_balancer_outbound_ips:
load_balancer_outbound_ip_resources = \
[ResourceReference(id=x.strip()) for x in load_balancer_outbound_ips.split(',')]
return load_balancer_outbound_ip_resources


def _get_load_balancer_outbound_ip_prefixes(load_balancer_outbound_ip_prefixes, models):
"""parse load balancer profile outbound IP prefix ids and return an array \
of references to the outbound IP prefix resources"""
load_balancer_outbound_ip_prefix_resources = None
if isinstance(models, SimpleNamespace):
ResourceReference = models.ResourceReference
else:
ResourceReference = models.get("ResourceReference")
if load_balancer_outbound_ip_prefixes:
load_balancer_outbound_ip_prefix_resources = \
[ResourceReference(id=x.strip()) for x in load_balancer_outbound_ip_prefixes.split(',')]
return load_balancer_outbound_ip_prefix_resources
42 changes: 0 additions & 42 deletions src/aks-preview/azext_aks_preview/_natgateway.py

This file was deleted.

24 changes: 14 additions & 10 deletions src/aks-preview/azext_aks_preview/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
get_k8s_versions_completion_list,
get_vm_size_completion_list,
)
from azure.cli.command_modules.acs._consts import (
CONST_OUTBOUND_TYPE_LOAD_BALANCER,
CONST_OUTBOUND_TYPE_MANAGED_NAT_GATEWAY,
CONST_OUTBOUND_TYPE_USER_ASSIGNED_NAT_GATEWAY,
CONST_OUTBOUND_TYPE_USER_DEFINED_ROUTING,
)
from azure.cli.command_modules.acs._validators import (
validate_load_balancer_idle_timeout,
validate_load_balancer_outbound_ip_prefixes,
validate_load_balancer_outbound_ips,
validate_load_balancer_outbound_ports,
validate_nat_gateway_idle_timeout,
validate_nat_gateway_managed_outbound_ip_count,
)
from azext_aks_preview._consts import (
CONST_ABSOLUTEMONTHLY_MAINTENANCE_SCHEDULE,
CONST_AZURE_KEYVAULT_NETWORK_ACCESS_PRIVATE,
Expand Down Expand Up @@ -58,10 +72,6 @@
CONST_OS_SKU_UBUNTU,
CONST_OS_SKU_WINDOWS2019,
CONST_OS_SKU_WINDOWS2022,
CONST_OUTBOUND_TYPE_LOAD_BALANCER,
CONST_OUTBOUND_TYPE_MANAGED_NAT_GATEWAY,
CONST_OUTBOUND_TYPE_USER_ASSIGNED_NAT_GATEWAY,
CONST_OUTBOUND_TYPE_USER_DEFINED_ROUTING,
CONST_PATCH_UPGRADE_CHANNEL,
CONST_RAPID_UPGRADE_CHANNEL,
CONST_RELATIVEMONTHLY_MAINTENANCE_SCHEDULE,
Expand Down Expand Up @@ -117,15 +127,9 @@
validate_k8s_version,
validate_linux_host_name,
validate_load_balancer_backend_pool_type,
validate_load_balancer_idle_timeout,
validate_load_balancer_outbound_ip_prefixes,
validate_load_balancer_outbound_ips,
validate_load_balancer_outbound_ports,
validate_load_balancer_sku,
validate_max_surge,
validate_message_of_the_day,
validate_nat_gateway_idle_timeout,
validate_nat_gateway_managed_outbound_ip_count,
validate_node_public_ip_tags,
validate_nodepool_id,
validate_nodepool_labels,
Expand Down
56 changes: 0 additions & 56 deletions src/aks-preview/azext_aks_preview/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,6 @@ def validate_load_balancer_sku(namespace):
raise CLIError("--load-balancer-sku can only be standard or basic")


def validate_load_balancer_outbound_ips(namespace):
"""validate load balancer profile outbound IP ids"""
if namespace.load_balancer_outbound_ips is not None:
ip_id_list = [x.strip()
for x in namespace.load_balancer_outbound_ips.split(',')]
if not all(ip_id_list):
raise CLIError(
"--load-balancer-outbound-ips cannot contain whitespace")


def validate_sku_tier(namespace):
"""Validates the sku tier string."""
if namespace.tier is not None:
Expand All @@ -220,19 +210,8 @@ def validate_sku_tier(namespace):
raise InvalidArgumentValueError("--tier can only be free or standard")


def validate_load_balancer_outbound_ip_prefixes(namespace):
"""validate load balancer profile outbound IP prefix ids"""
if namespace.load_balancer_outbound_ip_prefixes is not None:
ip_prefix_id_list = [
x.strip() for x in namespace.load_balancer_outbound_ip_prefixes.split(',')]
if not all(ip_prefix_id_list):
raise CLIError(
"--load-balancer-outbound-ip-prefixes cannot contain whitespace")


def validate_nodepool_taints(namespace):
"""Validates that provided node taints is a valid format"""

if hasattr(namespace, 'nodepool_taints'):
taintsStr = namespace.nodepool_taints
else:
Expand Down Expand Up @@ -335,25 +314,6 @@ def _validate_subnet_id(subnet_id, name):
raise CLIError(name + " is not a valid Azure resource ID.")


def validate_load_balancer_outbound_ports(namespace):
"""validate load balancer profile outbound allocated ports"""
if namespace.load_balancer_outbound_ports is not None:
if namespace.load_balancer_outbound_ports % 8 != 0:
raise CLIError(
"--load-balancer-allocated-ports must be a multiple of 8")
if namespace.load_balancer_outbound_ports < 0 or namespace.load_balancer_outbound_ports > 64000:
raise CLIError(
"--load-balancer-allocated-ports must be in the range [0,64000]")


def validate_load_balancer_idle_timeout(namespace):
"""validate load balancer profile idle timeout"""
if namespace.load_balancer_idle_timeout is not None:
if namespace.load_balancer_idle_timeout < 4 or namespace.load_balancer_idle_timeout > 100:
raise CLIError(
"--load-balancer-idle-timeout must be in the range [4,100]")


def validate_load_balancer_backend_pool_type(namespace):
"""validate load balancer backend pool type"""
if namespace.load_balancer_backend_pool_type is not None:
Expand All @@ -363,22 +323,6 @@ def validate_load_balancer_backend_pool_type(namespace):
f"Invalid Load Balancer Backend Pool Type {namespace.load_balancer_backend_pool_type}, supported values are nodeIP and nodeIPConfiguration")


def validate_nat_gateway_managed_outbound_ip_count(namespace):
"""validate NAT gateway profile managed outbound IP count"""
if namespace.nat_gateway_managed_outbound_ip_count is not None:
if namespace.nat_gateway_managed_outbound_ip_count < 1 or namespace.nat_gateway_managed_outbound_ip_count > 16:
raise InvalidArgumentValueError(
"--nat-gateway-managed-outbound-ip-count must be in the range [1,16]")


def validate_nat_gateway_idle_timeout(namespace):
"""validate NAT gateway profile idle timeout"""
if namespace.nat_gateway_idle_timeout is not None:
if namespace.nat_gateway_idle_timeout < 4 or namespace.nat_gateway_idle_timeout > 120:
raise InvalidArgumentValueError(
"--nat-gateway-idle-timeout must be in the range [4,120]")


def validate_nodepool_tags(ns):
""" Extracts multiple space-separated tags in key[=value] format """
if isinstance(ns.nodepool_tags, list):
Expand Down
2 changes: 1 addition & 1 deletion src/aks-preview/azext_aks_preview/azext_metadata.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"azext.minCliCoreVersion": "2.44.0",
"azext.minCliCoreVersion": "2.49.0",
"azext.isPreview": true
}
Loading

0 comments on commit cb18200

Please sign in to comment.