Skip to content

Commit fe875eb

Browse files
authored
[AKS] az aks update: Fix --outbound-type validation for UDR and userAssignedNATGateway (#33694)
1 parent dd351b1 commit fe875eb

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

src/azure-cli/azure/cli/command_modules/acs/managed_cluster_decorator.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2491,6 +2491,19 @@ def _get_outbound_type(
24912491
CONST_OUTBOUND_TYPE_USER_ASSIGNED_NAT_GATEWAY,
24922492
]:
24932493
if not read_from_mc and self.get_vnet_subnet_id() in ["", None] and not byo_subnets_configured:
2494+
if self.decorator_mode == DecoratorMode.UPDATE:
2495+
# --vnet-subnet-id is not registered for 'aks update'. For BYO VNet clusters the subnet
2496+
# is already known from the agentpool, so validation passes above. Reaching here in update
2497+
# mode means the cluster uses a managed VNet, which cannot be migrated to UDR/userAssignedNATGateway.
2498+
raise InvalidArgumentValueError(
2499+
"Updating outbound type to {outbound_type} is only supported for "
2500+
"clusters using a custom (BYO) virtual network. Managed VNet clusters "
2501+
"cannot be updated to {outbound_type}. Please refer to "
2502+
"https://learn.microsoft.com/en-us/azure/aks/egress-outboundtype"
2503+
"#updating-outboundtype-after-cluster-creation for supported migration paths.".format(
2504+
outbound_type=outbound_type
2505+
)
2506+
)
24942507
self._raise_missing_vnet_subnet_for_outbound_type(outbound_type, skuName)
24952508
if outbound_type == CONST_OUTBOUND_TYPE_MANAGED_NAT_GATEWAY:
24962509
if self.get_vnet_subnet_id() not in ["", None] or byo_subnets_set:

src/azure-cli/azure/cli/command_modules/acs/tests/latest/test_managed_cluster_decorator.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,6 +2391,71 @@ def test_get_outbound_type(self):
23912391
with self.assertRaises(InvalidArgumentValueError):
23922392
ctx_17.get_outbound_type()
23932393

2394+
# update to UDR on a BYO VNet cluster (subnet known from agentpool) should succeed
2395+
ctx_18 = AKSManagedClusterContext(
2396+
self.cmd,
2397+
AKSManagedClusterParamDict(
2398+
{"outbound_type": CONST_OUTBOUND_TYPE_USER_DEFINED_ROUTING}
2399+
),
2400+
self.models,
2401+
DecoratorMode.UPDATE,
2402+
)
2403+
ctx_18.agentpool_context = mock.MagicMock()
2404+
ctx_18.agentpool_context.get_vnet_subnet_id.return_value = "test_vnet_subnet_id"
2405+
self.assertEqual(
2406+
ctx_18.get_outbound_type(), CONST_OUTBOUND_TYPE_USER_DEFINED_ROUTING
2407+
)
2408+
2409+
# update to userAssignedNATGateway on a BYO VNet cluster (subnet known from agentpool) should succeed
2410+
ctx_18_1 = AKSManagedClusterContext(
2411+
self.cmd,
2412+
AKSManagedClusterParamDict(
2413+
{"outbound_type": CONST_OUTBOUND_TYPE_USER_ASSIGNED_NAT_GATEWAY}
2414+
),
2415+
self.models,
2416+
DecoratorMode.UPDATE,
2417+
)
2418+
ctx_18_1.agentpool_context = mock.MagicMock()
2419+
ctx_18_1.agentpool_context.get_vnet_subnet_id.return_value = "test_vnet_subnet_id"
2420+
self.assertEqual(
2421+
ctx_18_1.get_outbound_type(), CONST_OUTBOUND_TYPE_USER_ASSIGNED_NAT_GATEWAY
2422+
)
2423+
2424+
# update to UDR on a managed VNet cluster (no subnet) should fail with a clear error,
2425+
# not ask for --vnet-subnet-id (which is not registered for 'aks update')
2426+
ctx_19 = AKSManagedClusterContext(
2427+
self.cmd,
2428+
AKSManagedClusterParamDict(
2429+
{"outbound_type": CONST_OUTBOUND_TYPE_USER_DEFINED_ROUTING}
2430+
),
2431+
self.models,
2432+
DecoratorMode.UPDATE,
2433+
)
2434+
ctx_19.agentpool_context = mock.MagicMock()
2435+
ctx_19.agentpool_context.get_vnet_subnet_id.return_value = None
2436+
with self.assertRaisesRegex(
2437+
InvalidArgumentValueError,
2438+
"only supported for clusters using a custom",
2439+
):
2440+
ctx_19.get_outbound_type()
2441+
2442+
# update to userAssignedNATGateway on a managed VNet cluster (no subnet) should fail with a clear error
2443+
ctx_20 = AKSManagedClusterContext(
2444+
self.cmd,
2445+
AKSManagedClusterParamDict(
2446+
{"outbound_type": CONST_OUTBOUND_TYPE_USER_ASSIGNED_NAT_GATEWAY}
2447+
),
2448+
self.models,
2449+
DecoratorMode.UPDATE,
2450+
)
2451+
ctx_20.agentpool_context = mock.MagicMock()
2452+
ctx_20.agentpool_context.get_vnet_subnet_id.return_value = None
2453+
with self.assertRaisesRegex(
2454+
InvalidArgumentValueError,
2455+
"only supported for clusters using a custom",
2456+
):
2457+
ctx_20.get_outbound_type()
2458+
23942459
def test_get_network_plugin_mode(self):
23952460
# default
23962461
ctx_1 = AKSManagedClusterContext(

0 commit comments

Comments
 (0)