Skip to content

Commit 86a4e8f

Browse files
[AKS] aks machine update cli preview (#9346)
1 parent 2913f1b commit 86a4e8f

File tree

8 files changed

+2710
-0
lines changed

8 files changed

+2710
-0
lines changed

src/aks-preview/HISTORY.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ To release a new version, please select a new version number (usually plus 1 to
1111

1212
Pending
1313
+++++++
14+
* `az aks machine update`: Add support for updating machine tags, node taints and node labels.
1415
* Fix `az aks bastion` subshell defaulting to cmd on Windows when invoked from PowerShell by implementing grandparent process detection to identify the actual user shell.
1516

1617
19.0.0b8

src/aks-preview/azext_aks_preview/_help.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2620,6 +2620,30 @@
26202620
short-summary: The ipTags of the machine public IPs.
26212621
"""
26222622

2623+
helps['aks machine update'] = """
2624+
type: command
2625+
short-summary: Update the specified machine in an agentpool
2626+
parameters:
2627+
- name: --cluster-name
2628+
type: string
2629+
short-summary: Name of the managed cluster.
2630+
- name: --nodepool-name
2631+
type: string
2632+
short-summary: Name of the agentpool of a managed cluster.
2633+
- name: --machine-name
2634+
type: string
2635+
short-summary: Host name of the machine.
2636+
- name: --tags
2637+
type: string
2638+
short-summary: The tags of the machine.
2639+
- name: --labels
2640+
type: string
2641+
short-summary: The labels of the machine.
2642+
- name: --node-taints
2643+
type: string
2644+
short-summary: The taints of the machine.
2645+
"""
2646+
26232647
helps['aks machine list'] = """
26242648
type: command
26252649
short-summary: List the details for all machines in an agentpool

src/aks-preview/azext_aks_preview/_params.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2248,6 +2248,14 @@ def load_arguments(self, _):
22482248
help="Version of Kubernetes to use for the machine.",
22492249
)
22502250

2251+
with self.argument_context("aks machine update") as c:
2252+
c.argument(
2253+
"machine_name", help="The machine name."
2254+
)
2255+
c.argument("tags", tags_type, help="The tags to set on the machine.")
2256+
c.argument("node_taints", validator=validate_nodepool_taints)
2257+
c.argument("labels", nargs="*", help="Labels to set on the machine.")
2258+
22512259
with self.argument_context("aks operation") as c:
22522260
c.argument(
22532261
"nodepool_name",

src/aks-preview/azext_aks_preview/commands.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ def load_command_table(self, _):
301301
"show", "aks_machine_show", table_transformer=aks_machine_show_table_format
302302
)
303303
g.custom_command("add", "aks_machine_add", supports_no_wait=True)
304+
g.custom_command("update", "aks_machine_update", supports_no_wait=True)
304305

305306
with self.command_group(
306307
"aks operation", operations_sdk, client_factory=cf_operations

src/aks-preview/azext_aks_preview/custom.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
)
121121
from azext_aks_preview.machine import (
122122
add_machine,
123+
update_machine,
123124
)
124125
from azext_aks_preview.jwtauthenticator import (
125126
aks_jwtauthenticator_add_internal,
@@ -2611,6 +2612,33 @@ def aks_machine_add(
26112612
return add_machine(cmd, client, raw_parameters, no_wait)
26122613

26132614

2615+
# pylint: disable=unused-argument
2616+
def aks_machine_update(
2617+
cmd,
2618+
client,
2619+
resource_group_name,
2620+
cluster_name,
2621+
nodepool_name,
2622+
machine_name=None,
2623+
tags=None,
2624+
node_taints=None,
2625+
labels=None,
2626+
no_wait=False,
2627+
):
2628+
existedMachine = None
2629+
try:
2630+
existedMachine = client.get(resource_group_name, cluster_name, nodepool_name, machine_name)
2631+
except ResourceNotFoundError:
2632+
raise ClientRequestError(
2633+
f"Machine '{machine_name}' does not exist. Please use 'az aks machine list' to get current list of machines."
2634+
)
2635+
2636+
if existedMachine:
2637+
# DO NOT MOVE: get all the original parameters and save them as a dictionary
2638+
raw_parameters = locals()
2639+
return update_machine(client, raw_parameters, existedMachine, no_wait)
2640+
2641+
26142642
def aks_addon_list_available():
26152643
available_addons = []
26162644
for k, v in ADDONS.items():

src/aks-preview/azext_aks_preview/machine.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111
from azure.cli.core.util import sdk_no_wait
1212

1313

14+
def parse_key_value_list(pairs):
15+
result = {}
16+
if pairs is None:
17+
return result
18+
for pair in pairs:
19+
if "=" not in pair:
20+
raise ValueError(f"Invalid format '{pair}'. Expected format key=value.")
21+
key, value = pair.split("=", 1)
22+
result[key.strip()] = value.strip()
23+
return result
24+
25+
1426
def add_machine(cmd, client, raw_parameters, no_wait):
1527
resource_group_name = raw_parameters.get("resource_group_name")
1628
cluster_name = raw_parameters.get("cluster_name")
@@ -30,6 +42,35 @@ def add_machine(cmd, client, raw_parameters, no_wait):
3042
)
3143

3244

45+
def update_machine(client, raw_parameters, existedMachine, no_wait):
46+
resource_group_name = raw_parameters.get("resource_group_name")
47+
cluster_name = raw_parameters.get("cluster_name")
48+
nodepool_name = raw_parameters.get("nodepool_name")
49+
machine_name = raw_parameters.get("machine_name")
50+
51+
updated_machine = updateMachine(raw_parameters, existedMachine)
52+
53+
return sdk_no_wait(
54+
no_wait,
55+
client.begin_create_or_update,
56+
resource_group_name,
57+
cluster_name,
58+
nodepool_name,
59+
machine_name,
60+
updated_machine,
61+
)
62+
63+
64+
def updateMachine(raw_parameters, existedMachine):
65+
existedMachine = update_machine_tags(raw_parameters, existedMachine)
66+
existedMachine.properties.kubernetes = update_machine_kubernetes_profile_taints_labels(
67+
raw_parameters,
68+
existedMachine
69+
)
70+
71+
return existedMachine
72+
73+
3374
def constructMachine(cmd, raw_parameters, machine_name):
3475
machine_name = raw_parameters.get("machine_name")
3576
if machine_name is None:
@@ -115,6 +156,27 @@ def set_machine_kubernetes_profile(cmd, raw_parameters):
115156
return machineKubernetesProfile
116157

117158

159+
def update_machine_tags(raw_parameters, existedMachine):
160+
tags = raw_parameters.get("tags")
161+
if tags is not None and len(tags) != 0:
162+
existedMachine.properties.tags = tags
163+
return existedMachine
164+
165+
166+
def update_machine_kubernetes_profile_taints_labels(raw_parameters, existedMachine):
167+
taints_raw = raw_parameters.get("node_taints")
168+
if taints_raw is not None:
169+
node_taints = [x.strip() for x in (taints_raw.split(",") if taints_raw else [])]
170+
existedMachine.properties.kubernetes.node_taints = node_taints
171+
172+
labels_raw = raw_parameters.get("labels")
173+
labels = parse_key_value_list(labels_raw)
174+
if labels is not None and len(labels) != 0:
175+
existedMachine.properties.kubernetes.node_labels = labels
176+
177+
return existedMachine.properties.kubernetes
178+
179+
118180
def set_machine_os_profile(cmd, raw_parameters):
119181
MachineOSProfile = cmd.get_models(
120182
"MachineOSProfile",

0 commit comments

Comments
 (0)