Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
xuexu6666 committed Feb 2, 2024
1 parent f311e02 commit d924628
Show file tree
Hide file tree
Showing 6 changed files with 114 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 @@ -12,6 +12,7 @@ To release a new version, please select a new version number (usually plus 1 to
Pending
+++++++
* Deprecate the alias "-r" of parameter --source-resource-id in `az aks trustedaccess rolebinding create`
* Add `az aks nodepool delete-machines` command.

1.0.0b2
+++++++
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 @@ -1895,6 +1895,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 machine2
"""

helps['aks machine'] = """
type: group
short-summary: Get information about machines in a nodepool of a managed clusters
Expand Down
3 changes: 3 additions & 0 deletions src/aks-preview/azext_aks_preview/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,9 @@ 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", help="machines 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
45 changes: 45 additions & 0 deletions src/aks-preview/azext_aks_preview/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,51 @@ def aks_agentpool_operation_abort(cmd, # pylint: disable=unused-argument
headers=headers,
)

def aks_agentpool_delete_machines(cmd, # pylint: disable=unused-argument
client,
resource_group_name,
cluster_name,
nodepool_name,
machine_names,
aks_custom_headers=None,
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 CLIError(
f"Node pool {nodepool_name} doesnt exist, "
"use 'aks nodepool list' to get current node pool list"
)

if len(machine_names) == 0:
raise CLIError(
f"Machine names doesn't provide, "
"use '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)
headers = get_aks_custom_headers(aks_custom_headers)
return sdk_no_wait(
no_wait,
client.begin_delete_machines,
resource_group_name,
cluster_name,
nodepool_name,
machines,
headers=headers,
)


def aks_operation_abort(cmd, # pylint: disable=unused-argument
client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3376,6 +3376,56 @@ 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.cmd(
"aks nodepool delete-machines --resource-group={resource_group} --cluster-name={name} --nodepool-name={nodepool_name} --machine-names {machine_name1} {machine_name2}",
checks=[self.check("provisioningState", "Succeeded")],
)
# 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 d924628

Please sign in to comment.