Skip to content

Commit ba28ea5

Browse files
author
Jianping Zeng
committed
aks machine update cli preview
1 parent 22c3785 commit ba28ea5

File tree

9 files changed

+2704
-1
lines changed

9 files changed

+2704
-1
lines changed

src/aks-preview/HISTORY.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ To release a new version, please select a new version number (usually plus 1 to
1212
Pending
1313
+++++++
1414

15+
19.0.0b7
16+
+++++++
17+
* `az aks machine update`: Add support for updating machine tags, node taints and node labels.
18+
1519
19.0.0b6
1620
+++++++
1721
* Update the minimum required cli core version to `2.73.0` (actually since `18.0.0b35`).

src/aks-preview/azext_aks_preview/_help.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2611,6 +2611,30 @@
26112611
short-summary: The ipTags of the machine public IPs.
26122612
"""
26132613

2614+
helps['aks machine update'] = """
2615+
type: command
2616+
short-summary: update the specified machine in an agentpool
2617+
parameters:
2618+
- name: --cluster-name
2619+
type: string
2620+
short-summary: Name of the managed cluster.
2621+
- name: --nodepool-name
2622+
type: string
2623+
short-summary: Name of the agentpool of a managed cluster.
2624+
- name: --machine-name
2625+
type: string
2626+
short-summary: Host name of the machine.
2627+
- name: --tags
2628+
type: string
2629+
short-summary: The tags of the machine.
2630+
- name: --labels
2631+
type: string
2632+
short-summary: The labels of the machine.
2633+
- name: --node-taints
2634+
type: string
2635+
short-summary: The taints of the machine.
2636+
"""
2637+
26142638
helps['aks machine list'] = """
26152639
type: command
26162640
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
@@ -2219,6 +2219,14 @@ def load_arguments(self, _):
22192219
help="Version of Kubernetes to use for the machine.",
22202220
)
22212221

2222+
with self.argument_context("aks machine update") as c:
2223+
c.argument(
2224+
"machine_name", help="The machine name."
2225+
)
2226+
c.argument("tags", tags_type, help="The tags to set to the machine")
2227+
c.argument("node_taints", validator=validate_nodepool_taints)
2228+
c.argument("labels", nargs="*", help="Labels set to the managed namespace")
2229+
22222230
with self.argument_context("aks operation") as c:
22232231
c.argument(
22242232
"nodepool_name",

src/aks-preview/azext_aks_preview/commands.py

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

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

src/aks-preview/azext_aks_preview/custom.py

Lines changed: 27 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,
@@ -2607,6 +2608,32 @@ def aks_machine_add(
26072608
raw_parameters = locals()
26082609
return add_machine(cmd, client, raw_parameters, no_wait)
26092610

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

26112638
def aks_addon_list_available():
26122639
available_addons = []

src/aks-preview/azext_aks_preview/machine.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010

1111
from azure.cli.core.util import sdk_no_wait
1212

13+
def parse_key_value_list(pairs):
14+
result = {}
15+
if pairs is None:
16+
return result
17+
for pair in pairs:
18+
if "=" not in pair:
19+
raise ValueError(f"Invalid format '{pair}'. Expected format key=value.")
20+
key, value = pair.split("=", 1)
21+
result[key.strip()] = value.strip()
22+
return result
1323

1424
def add_machine(cmd, client, raw_parameters, no_wait):
1525
resource_group_name = raw_parameters.get("resource_group_name")
@@ -29,6 +39,30 @@ def add_machine(cmd, client, raw_parameters, no_wait):
2939
machine,
3040
)
3141

42+
def update_machine(cmd, client, raw_parameters, existedMachine, no_wait):
43+
resource_group_name = raw_parameters.get("resource_group_name")
44+
cluster_name = raw_parameters.get("cluster_name")
45+
nodepool_name = raw_parameters.get("nodepool_name")
46+
machine_name = raw_parameters.get("machine_name")
47+
48+
updated_machine = updateMachine(cmd, raw_parameters, existedMachine)
49+
50+
return sdk_no_wait(
51+
no_wait,
52+
client.begin_create_or_update,
53+
resource_group_name,
54+
cluster_name,
55+
nodepool_name,
56+
machine_name,
57+
updated_machine,
58+
)
59+
60+
def updateMachine(cmd, raw_parameters, existedMachine):
61+
existedMachine = update_machine_tags(cmd, raw_parameters, existedMachine)
62+
existedMachine.properties.kubernetes = update_machine_kubernetes_profile_taints_labels(cmd, raw_parameters, existedMachine)
63+
64+
return existedMachine
65+
3266

3367
def constructMachine(cmd, raw_parameters, machine_name):
3468
machine_name = raw_parameters.get("machine_name")
@@ -114,6 +148,25 @@ def set_machine_kubernetes_profile(cmd, raw_parameters):
114148
)
115149
return machineKubernetesProfile
116150

151+
def update_machine_tags(cmd, raw_parameters, existedMachine):
152+
tags = raw_parameters.get("tags")
153+
if tags is not None and len(tags) != 0:
154+
existedMachine.properties.tags = tags
155+
return existedMachine
156+
157+
def update_machine_kubernetes_profile_taints_labels(cmd, raw_parameters, existedMachine):
158+
taints_raw = raw_parameters.get("node_taints")
159+
if taints_raw is not None:
160+
node_taints = [x.strip() for x in (taints_raw.split(",") if taints_raw else [])]
161+
existedMachine.properties.kubernetes.node_taints = node_taints
162+
163+
labels_raw = raw_parameters.get("labels")
164+
labels = parse_key_value_list(labels_raw)
165+
if labels is not None and len(labels) != 0:
166+
existedMachine.properties.kubernetes.node_labels = labels
167+
168+
return existedMachine.properties.kubernetes
169+
117170

118171
def set_machine_os_profile(cmd, raw_parameters):
119172
MachineOSProfile = cmd.get_models(

0 commit comments

Comments
 (0)