Skip to content

Commit e7ba3dc

Browse files
committed
fix(az aks list-vm-skus): filter out vm skus with AZ restrictions by default
1 parent 6b81746 commit e7ba3dc

File tree

4 files changed

+19
-26
lines changed

4 files changed

+19
-26
lines changed

src/aks-preview/azext_aks_preview/custom.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3450,7 +3450,7 @@ def aks_list_vm_skus(cmd, client, location, size=None, zone=None, show_all=None)
34503450
result = list(client.list(location))
34513451

34523452
if not show_all:
3453-
result = [sku for sku in result if _aks_is_vm_sku_available(sku, zone)]
3453+
result = [sku for sku in result if _aks_is_vm_sku_available(sku)]
34543454

34553455
if size:
34563456
result = [sku for sku in result if sku.name and size.lower() in sku.name.lower()]

src/aks-preview/azext_aks_preview/tests/latest/test_aks_commands.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22721,7 +22721,6 @@ def test_aks_list_vm_skus(self):
2272122721
).get_output_in_json()
2272222722
assert len(zonal) > 0, "expected zone-capable SKUs in eastus"
2272322723
for sku in zonal:
22724-
zones = (
22725-
(sku.get("locationInfo") or [{}])[0].get("zones") or []
22726-
)
22724+
loc_info = sku.get("locationInfo", [])
22725+
zones = loc_info[0].get("zones", []) if loc_info else []
2272722726
assert len(zones) > 0, f"SKU '{sku['name']}' has no zones despite --zone filter"

src/aks-preview/azext_aks_preview/tests/latest/test_vm_skus.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,76 +56,69 @@ class TestAksIsVmSkuAvailable(unittest.TestCase):
5656

5757
def test_no_restrictions_returns_true(self):
5858
sku = _make_sku("Standard_D4s_v3", restrictions=[])
59-
self.assertTrue(_aks_is_vm_sku_available(sku, zone=False))
59+
self.assertTrue(_aks_is_vm_sku_available(sku))
6060

6161
def test_none_restrictions_returns_true(self):
6262
sku = _make_sku("Standard_D4s_v3")
6363
sku.restrictions = None
64-
self.assertTrue(_aks_is_vm_sku_available(sku, zone=False))
64+
self.assertTrue(_aks_is_vm_sku_available(sku))
6565

6666
def test_irrelevant_reason_code_is_ignored(self):
6767
# A restriction with a different reason_code should not affect availability.
6868
restriction = _make_restriction("Location", reason_code="ManuallyExcluded",
6969
locations=["eastus"])
7070
sku = _make_sku("Standard_D4s_v3", location="eastus",
7171
restrictions=[restriction])
72-
self.assertTrue(_aks_is_vm_sku_available(sku, zone=False))
72+
self.assertTrue(_aks_is_vm_sku_available(sku))
7373

7474
def test_location_restriction_matching_location_returns_false(self):
7575
restriction = _make_restriction("Location", locations=["eastus"])
7676
sku = _make_sku("Standard_D4s_v3", location="eastus",
7777
restrictions=[restriction])
78-
self.assertFalse(_aks_is_vm_sku_available(sku, zone=False))
78+
self.assertFalse(_aks_is_vm_sku_available(sku))
7979

8080
def test_location_restriction_non_matching_location_returns_true(self):
8181
restriction = _make_restriction("Location", locations=["westus"])
8282
sku = _make_sku("Standard_D4s_v3", location="eastus",
8383
restrictions=[restriction])
84-
self.assertTrue(_aks_is_vm_sku_available(sku, zone=False))
84+
self.assertTrue(_aks_is_vm_sku_available(sku))
8585

86-
def test_zone_restriction_all_zones_restricted_with_zone_flag_returns_false(self):
87-
# All three zones are restricted and caller requests zone-aware filtering.
86+
def test_zone_restriction_all_zones_restricted_returns_false(self):
87+
# All three zones are restricted — SKU is unavailable.
8888
restriction = _make_restriction("Zone", zones=["1", "2", "3"])
8989
sku = _make_sku("Standard_D4s_v3", zones=["1", "2", "3"],
9090
restrictions=[restriction])
91-
self.assertFalse(_aks_is_vm_sku_available(sku, zone=True))
91+
self.assertFalse(_aks_is_vm_sku_available(sku))
9292

9393
def test_zone_restriction_partial_zones_restricted_returns_true(self):
9494
# Only zones 1 and 2 are restricted, but zone 3 is still available.
9595
restriction = _make_restriction("Zone", zones=["1", "2"])
9696
sku = _make_sku("Standard_D4s_v3", zones=["1", "2", "3"],
9797
restrictions=[restriction])
98-
self.assertTrue(_aks_is_vm_sku_available(sku, zone=True))
99-
100-
def test_zone_restriction_ignored_when_zone_flag_false(self):
101-
# Even if all zones are restricted, without --zone the SKU is available.
102-
restriction = _make_restriction("Zone", zones=["1", "2", "3"])
103-
sku = _make_sku("Standard_D4s_v3", zones=["1", "2", "3"],
104-
restrictions=[restriction])
105-
self.assertTrue(_aks_is_vm_sku_available(sku, zone=False))
98+
self.assertTrue(_aks_is_vm_sku_available(sku))
10699

107100
def test_location_restriction_with_empty_locations_list_returns_true(self):
108101
# restriction_info.locations is present but empty – current region is not listed.
109102
restriction = _make_restriction("Location", locations=[])
110103
sku = _make_sku("Standard_D4s_v3", location="eastus",
111104
restrictions=[restriction])
112-
self.assertTrue(_aks_is_vm_sku_available(sku, zone=False))
105+
self.assertTrue(_aks_is_vm_sku_available(sku))
113106

114107
def test_multiple_restrictions_one_location_match_returns_false(self):
115108
# One restriction is unrelated; the second blocks the current location.
116109
r1 = _make_restriction("Zone", zones=["1"])
117110
r2 = _make_restriction("Location", locations=["eastus"])
118111
sku = _make_sku("Standard_D4s_v3", location="eastus",
119112
zones=["1", "2", "3"], restrictions=[r1, r2])
120-
self.assertFalse(_aks_is_vm_sku_available(sku, zone=False))
113+
self.assertFalse(_aks_is_vm_sku_available(sku))
121114

122115
def test_none_restriction_info_is_handled_gracefully(self):
123116
restriction = _make_restriction("Location", locations=["eastus"])
124117
restriction.restriction_info = None
125118
sku = _make_sku("Standard_D4s_v3", location="eastus",
126119
restrictions=[restriction])
127120
# With restriction_info=None the locations list defaults to [], so no match.
128-
self.assertTrue(_aks_is_vm_sku_available(sku, zone=False))
121+
self.assertTrue(_aks_is_vm_sku_available(sku))
129122

130123

131124
if __name__ == '__main__':

src/aks-preview/azext_aks_preview/vm_skus_util.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
"""Helpers for the `az aks list-vm-skus` command."""
66

77

8-
def _aks_is_vm_sku_available(sku, zone):
8+
def _aks_is_vm_sku_available(sku):
99
"""Return True if the SKU is available for the current subscription.
1010
1111
A SKU is considered unavailable when:
1212
1. It has a Location restriction that covers this region, or
13-
2. The --zone flag is set AND all availability zones in the region are restricted.
13+
2. All availability zones in the region are restricted.
1414
"""
1515
if not sku.restrictions:
1616
return True
@@ -22,13 +22,14 @@ def _aks_is_vm_sku_available(sku, zone):
2222
restriction_type = restriction.type
2323
restriction_info = restriction.restriction_info
2424

25+
# NOTE: Should already filtered at the API level
2526
if restriction_type == "Location":
2627
restricted_locations = (restriction_info.locations or []) if restriction_info else []
2728
location_info = sku.location_info[0] if sku.location_info else None
2829
if location_info and location_info.location in restricted_locations:
2930
return False
3031

31-
if restriction_type == "Zone" and zone:
32+
if restriction_type == "Zone":
3233
location_info = sku.location_info[0] if sku.location_info else None
3334
if location_info:
3435
available_zones = set(location_info.zones or [])

0 commit comments

Comments
 (0)