Skip to content

Commit

Permalink
{AKS} Add new command: az aks nodepool delete-machines (#7248)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuexu6666 authored Feb 8, 2024
1 parent e6bfbc6 commit e71b504
Show file tree
Hide file tree
Showing 7 changed files with 1,861 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/aks-preview/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Pending
+++++++
* Vendor new SDK and bump API version to 2023-11-02-preview.
* Implicitly enable istio when ingress or egress gateway is enabled for Azure Service Mesh.
* Add `az aks nodepool delete-machines` command.

1.0.0b5
+++++++
Expand Down
12 changes: 12 additions & 0 deletions src/aks-preview/azext_aks_preview/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -1904,6 +1904,18 @@
text: az aks nodepool operation-abort -g myResourceGroup --nodepool-name nodepool1 --cluster-name myAKSCluster
"""

helps['aks nodepool delete-machines'] = """
type: command
short-summary: Delete specific machines in an agentpool for a managed cluster.
parameters:
- name: --machine-names
type: string array
short-summary: Space-separated list of machine names from the agent pool to be deleted.
examples:
- name: Delete specific machines in an agent pool
text: az aks nodepool delete-machines -g myResourceGroup --nodepool-name nodepool1 --cluster-name myAKSCluster --machine-names machine1
"""

helps['aks machine'] = """
type: group
short-summary: Get information about machines in a nodepool of a managed clusters
Expand Down
8 changes: 8 additions & 0 deletions src/aks-preview/azext_aks_preview/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,14 @@ def load_arguments(self, _):
help="delete an AKS nodepool by ignoring PodDisruptionBudget setting",
)

with self.argument_context("aks nodepool delete-machines") as c:
c.argument(
"machine_names",
nargs="+",
required=True,
help="Space-separated machine names to delete.",
)

with self.argument_context("aks machine") as c:
c.argument("cluster_name", help="The cluster name.")
c.argument(
Expand Down
3 changes: 3 additions & 0 deletions src/aks-preview/azext_aks_preview/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ def load_command_table(self, _):
g.custom_command(
"operation-abort", "aks_agentpool_operation_abort", supports_no_wait=True
)
g.custom_command(
"delete-machines", "aks_agentpool_delete_machines", supports_no_wait=True
)

with self.command_group(
"aks machine", machines_sdk, client_factory=cf_machines
Expand Down
44 changes: 44 additions & 0 deletions src/aks-preview/azext_aks_preview/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
ClientRequestError,
InvalidArgumentValueError,
MutuallyExclusiveArgumentError,
RequiredArgumentMissingError,
)
from azure.cli.core.commands import LongRunningOperation
from azure.cli.core.commands.client_factory import get_subscription_id
Expand Down Expand Up @@ -1550,6 +1551,49 @@ def aks_agentpool_operation_abort(cmd, # pylint: disable=unused-argument
)


def aks_agentpool_delete_machines(cmd, # pylint: disable=unused-argument
client,
resource_group_name,
cluster_name,
nodepool_name,
machine_names,
no_wait=False):
agentpool_exists = False
instances = client.list(resource_group_name, cluster_name)
for agentpool_profile in instances:
if agentpool_profile.name.lower() == nodepool_name.lower():
agentpool_exists = True
break

if not agentpool_exists:
raise ResourceNotFoundError(
f"Node pool {nodepool_name} doesn't exist, "
"use 'az aks nodepool list' to get current node pool list"
)

if len(machine_names) == 0:
raise RequiredArgumentMissingError(
"--machine-names doesn't provide, "
"use 'az aks machine list' to get current machine list"
)

AgentPoolDeleteMachinesParameter = cmd.get_models(
"AgentPoolDeleteMachinesParameter",
resource_type=CUSTOM_MGMT_AKS_PREVIEW,
operation_group="agent_pools",
)

machines = AgentPoolDeleteMachinesParameter(machine_names=machine_names)
return sdk_no_wait(
no_wait,
client.begin_delete_machines,
resource_group_name,
cluster_name,
nodepool_name,
machines,
)


def aks_operation_abort(cmd, # pylint: disable=unused-argument
client,
resource_group_name,
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3379,6 +3379,69 @@ def test_aks_nodepool_snapshot(self, resource_group, resource_group_location):
delete_snapshot_cmd = "aks nodepool snapshot delete --resource-group {resource_group} --name {snapshot_name} --yes --no-wait"
self.cmd(delete_snapshot_cmd, checks=[self.is_empty()])

@AllowLargeResponse()
@AKSCustomResourceGroupPreparer(
random_name_length=17, name_prefix="clitest", location="westus2"
)
def test_aks_nodepool_delete_machines(self, resource_group, resource_group_location):
aks_name = self.create_random_name("cliakstest", 16)
nodepool_name = self.create_random_name("c", 6)
self.kwargs.update(
{
"resource_group": resource_group,
"location": resource_group_location,
"name": aks_name,
"nodepool_name": nodepool_name,
"ssh_key_value": self.generate_ssh_keys(),
}
)

# create aks cluster
create_cmd = "aks create --resource-group={resource_group} --name={name} --ssh-key-value={ssh_key_value}"
self.cmd(
create_cmd,
checks=[
self.check("provisioningState", "Succeeded"),
],
)
# add nodepool
self.cmd(
"aks nodepool add --resource-group={resource_group} --cluster-name={name} --name={nodepool_name} --node-count=4",
checks=[self.check("provisioningState", "Succeeded")],
)
# list machines
list_cmd = 'aks machine list ' \
' --resource-group={resource_group} ' \
' --cluster-name={name} --nodepool-name={nodepool_name} -o json'
machine_list = self.cmd(list_cmd).get_output_in_json()
assert len(machine_list) == 4
aks_machine_list_table_format(machine_list)
# delete machines
machine_name1 = machine_list[0]["name"]
machine_name2 = machine_list[2]["name"]
self.kwargs.update(
{
"resource_group": resource_group,
"location": resource_group_location,
"name": aks_name,
"nodepool_name": nodepool_name,
"ssh_key_value": self.generate_ssh_keys(),
"machine_name1": machine_name1,
"machine_name2": machine_name2,
}
)
self.cmd(
"aks nodepool delete-machines --resource-group={resource_group} --cluster-name={name} --nodepool-name={nodepool_name} --machine-names {machine_name1} {machine_name2}"
)
# list machines after deletion
machine_list_after = self.cmd(list_cmd).get_output_in_json()
assert len(machine_list_after) == 2
# delete AKS cluster
self.cmd(
"aks delete -g {resource_group} -n {name} --yes --no-wait",
checks=[self.is_empty()],
)

@AllowLargeResponse()
@AKSCustomResourceGroupPreparer(
random_name_length=17,
Expand Down

0 comments on commit e71b504

Please sign in to comment.