Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/azure-cli/azure/cli/command_modules/acs/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,13 @@
helps["aks delete"] = """
type: command
short-summary: Delete a managed Kubernetes cluster.
parameters:
- name: --if-match
type: string
short-summary: The value provided will be compared to the ETag of the managed cluster, if it matches the operation will proceed. If it does not match, the request will be rejected to prevent accidental overwrites.
- name: --if-none-match
type: string
short-summary: Set to '*' to allow deleting a cluster only if it exists. Other values will be ignored.
examples:
- name: Delete a managed Kubernetes cluster. (autogenerated)
text: az aks delete --name MyManagedCluster --resource-group MyResourceGroup
Expand Down
25 changes: 18 additions & 7 deletions src/azure-cli/azure/cli/command_modules/acs/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import random
import re
import semver
from typing import Any, List, TypeVar
from typing import Any, Dict, List, TypeVar

from azure.cli.command_modules.acs._client_factory import get_snapshots_client, get_msi_client
from azure.cli.core.azclierror import (
Expand Down Expand Up @@ -66,6 +66,22 @@ def safe_lower(obj: Any) -> Any:
return obj


def build_etag_kwargs(if_match=None, if_none_match=None) -> Dict[str, Any]:
"""Convert if_match/if_none_match to etag/match_condition kwargs for SDK v41+."""
from azure.core import MatchConditions
kwargs: Dict[str, Any] = {}
if if_match is not None:
kwargs["etag"] = if_match
kwargs["match_condition"] = MatchConditions.IfNotModified
elif if_none_match is not None:
if if_none_match == "*":
kwargs["match_condition"] = MatchConditions.IfMissing
else:
kwargs["etag"] = if_none_match
kwargs["match_condition"] = MatchConditions.IfModified
return kwargs


def get_property_from_dict_or_object(obj, property_name) -> Any:
"""Get the value corresponding to the property name from a dictionary or object.

Expand Down Expand Up @@ -103,15 +119,10 @@ def check_is_private_cluster(mc: ManagedCluster) -> bool:
def check_is_apiserver_vnet_integration_cluster(mc: ManagedCluster) -> bool:
"""Check `mc` object to determine whether apiserver vnet integration is enabled.

Note: enableVnetIntegration is still in preview api so we use additional_properties here

:return: bool
"""
if mc and mc.api_server_access_profile:
additional_properties = mc.api_server_access_profile.additional_properties
if 'enableVnetIntegration' in additional_properties:
return additional_properties['enableVnetIntegration']
return False
return bool(mc.api_server_access_profile.enable_vnet_integration)
return False


Expand Down
4 changes: 4 additions & 0 deletions src/azure-cli/azure/cli/command_modules/acs/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,10 @@ def load_arguments(self, _):
'by that action.'
)
)
with self.argument_context('aks delete') as c:
c.argument("if_match")
c.argument("if_none_match")

with self.argument_context('aks disable-addons', resource_type=ResourceType.MGMT_CONTAINERSERVICE, operation_group='managed_clusters') as c:
c.argument('addons', options_list=['--addons', '-a'])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
DecoratorEarlyExitException,
DecoratorMode,
)
from azure.cli.command_modules.acs._helpers import get_snapshot_by_snapshot_id, safe_list_get, process_dns_overrides
from azure.cli.command_modules.acs._helpers import (
get_snapshot_by_snapshot_id, safe_list_get, process_dns_overrides, build_etag_kwargs
)
from azure.cli.command_modules.acs._validators import extract_comma_separated_string
from azure.cli.command_modules.acs.base_decorator import BaseAKSContext, BaseAKSModels, BaseAKSParamDict
from azure.cli.core import AzCommandsLoader
Expand Down Expand Up @@ -1249,8 +1251,9 @@ def get_vm_set_type(self) -> str:
if self.agentpool and self.agentpool.type is not None:
vm_set_type = self.agentpool.type
else:
if self.agentpool and self.agentpool.type_properties_type is not None:
vm_set_type = self.agentpool.type_properties_type
if (self.agentpool and self.agentpool.properties and
self.agentpool.properties.type_properties_type is not None):
vm_set_type = self.agentpool.properties.type_properties_type

# normalize
if vm_set_type.lower() == CONST_VIRTUAL_MACHINE_SCALE_SETS.lower():
Expand Down Expand Up @@ -2350,8 +2353,7 @@ def add_agentpool(self, agentpool: AgentPool) -> AgentPool:
# validated in "init_agentpool", skip to avoid duplicate api calls
self.context._get_nodepool_name(enable_validation=False),
agentpool,
if_match=self.context.get_if_match(),
if_none_match=self.context.get_if_none_match(),
**build_etag_kwargs(self.context.get_if_match(), self.context.get_if_none_match()),
headers=self.context.get_aks_custom_headers(),
)

Expand Down Expand Up @@ -2697,8 +2699,7 @@ def update_agentpool(self, agentpool: AgentPool) -> AgentPool:
self.context.get_cluster_name(),
self.context.get_nodepool_name(),
agentpool,
if_match=self.context.get_if_match(),
if_none_match=self.context.get_if_none_match(),
**build_etag_kwargs(self.context.get_if_match(), self.context.get_if_none_match()),
headers=self.context.get_aks_custom_headers(),
)

Expand Down
20 changes: 10 additions & 10 deletions src/azure-cli/azure/cli/command_modules/acs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ def load_command_table(self, _):

managed_clusters_sdk = CliCommandType(
operations_tmpl='azure.mgmt.containerservice.operations.'
'_managed_clusters_operations#ManagedClustersOperations.{}',
'_operations#ManagedClustersOperations.{}',
operation_group='managed_clusters',
resource_type=ResourceType.MGMT_CONTAINERSERVICE,
client_factory=cf_managed_clusters
)

agent_pools_sdk = CliCommandType(
operations_tmpl='azure.mgmt.containerservice.operations.'
'_agent_pools_operations#AgentPoolsOperations.{}',
'_operations#AgentPoolsOperations.{}',
operation_group='agent_pools',
resource_type=ResourceType.MGMT_CONTAINERSERVICE,
client_factory=cf_managed_clusters
Expand All @@ -60,40 +60,40 @@ def load_command_table(self, _):
)

maintenance_configuration_sdk = CliCommandType(
operations_tmpl='aazure.mgmt.containerservice.operations.'
'_maintenance_configurations_operations#MaintenanceConfigurationsOperations.{}',
operations_tmpl='azure.mgmt.containerservice.operations.'
'_operations#MaintenanceConfigurationsOperations.{}',
operation_group='maintenance_configurations',
resource_type=ResourceType.MGMT_CONTAINERSERVICE,
client_factory=cf_maintenance_configurations
)

managed_namespaces_sdk = CliCommandType(
operations_tmpl='azure.mgmt.containerservice.operations.'
'_managed_namespaces_operations#ManagedNamespacesOperations.{}',
'_operations#ManagedNamespacesOperations.{}',
operation_group='managed_namespaces',
resource_type=ResourceType.MGMT_CONTAINERSERVICE,
client_factory=cf_managed_namespaces,
)

snapshot_sdk = CliCommandType(
operations_tmpl='azure.mgmt.containerservice.operations.'
'_snapshots_operations#SnapshotsOperations.{}',
'_operations#SnapshotsOperations.{}',
operation_group='snapshots',
resource_type=ResourceType.MGMT_CONTAINERSERVICE,
client_factory=cf_snapshots
)

trustedaccess_role_sdk = CliCommandType(
operations_tmpl='azure.mgmt.containerservice.operations.'
'_trusted_access_roles_operations#TrustedAccessRolesOperations.{}',
'_operations#TrustedAccessRolesOperations.{}',
operation_group='trustedaccess_role',
resource_type=ResourceType.MGMT_CONTAINERSERVICE,
client_factory=cf_trustedaccess_role
)

trustedaccess_role_binding_sdk = CliCommandType(
operations_tmpl='azure.mgmt.containerservice.operations.'
'_trusted_access_role_bindings_operations#TrustedAccessRoleBindingsOperations.{}',
'_operations#TrustedAccessRoleBindingsOperations.{}',
operation_group='trustedaccess_role_binding',
resource_type=ResourceType.MGMT_CONTAINERSERVICE,
client_factory=cf_trustedaccess_role_binding
Expand All @@ -109,8 +109,8 @@ def load_command_table(self, _):
table_transformer=aks_upgrades_table_format)
g.custom_command('upgrade', 'aks_upgrade', supports_no_wait=True)
g.custom_command('scale', 'aks_scale', supports_no_wait=True)
g.command('delete', 'begin_delete',
supports_no_wait=True, confirmation=True)
g.custom_command('delete', 'aks_delete',
supports_no_wait=True, confirmation=True)
g.custom_show_command('show', 'aks_show',
table_transformer=aks_show_table_format)
g.custom_command('list', 'aks_list',
Expand Down
37 changes: 21 additions & 16 deletions src/azure-cli/azure/cli/command_modules/acs/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
CONST_VIRTUAL_MACHINES,
)
from azure.cli.command_modules.acs._polling import RunCommandLocationPolling
from azure.cli.command_modules.acs._helpers import get_snapshot_by_snapshot_id, check_is_private_link_cluster
from azure.cli.command_modules.acs._helpers import get_snapshot_by_snapshot_id, check_is_private_link_cluster, build_etag_kwargs
from azure.cli.command_modules.acs._resourcegroup import get_rg_location
from azure.cli.command_modules.acs.managednamespace import aks_managed_namespace_add, aks_managed_namespace_update
from azure.cli.command_modules.acs._validators import extract_comma_separated_string
Expand Down Expand Up @@ -1357,7 +1357,7 @@ def aks_upgrade(cmd,
if active_cloud.profile != "latest":
return sdk_no_wait(no_wait, client.begin_create_or_update, resource_group_name, name, instance)

return sdk_no_wait(no_wait, client.begin_create_or_update, resource_group_name, name, instance, if_match=if_match, if_none_match=if_none_match)
return sdk_no_wait(no_wait, client.begin_create_or_update, resource_group_name, name, instance, **build_etag_kwargs(if_match, if_none_match))


def _update_upgrade_settings(cmd, instance,
Expand Down Expand Up @@ -1453,6 +1453,11 @@ def aks_scale(cmd, client, resource_group_name, name, node_count, nodepool_name=
raise CLIError('The nodepool "{}" was not found.'.format(nodepool_name))


def aks_delete(cmd, client, resource_group_name, name, no_wait=False, if_match=None, if_none_match=None):
return sdk_no_wait(no_wait, client.begin_delete, resource_group_name, name,
**build_etag_kwargs(if_match, if_none_match))


def aks_show(cmd, client, resource_group_name, name):
mc = client.get(resource_group_name, name)
return _remove_nulls([mc])[0]
Expand Down Expand Up @@ -1490,15 +1495,16 @@ def _remove_nulls(managed_clusters):
for managed_cluster in managed_clusters:
for attr in attrs:
if getattr(managed_cluster, attr, None) is None:
delattr(managed_cluster, attr)
managed_cluster.pop(attr, None)
if managed_cluster.agent_pool_profiles is not None:
for ap_profile in managed_cluster.agent_pool_profiles:
for attr in ap_attrs:
if getattr(ap_profile, attr, None) is None:
delattr(ap_profile, attr)
for attr in sp_attrs:
if getattr(managed_cluster.service_principal_profile, attr, None) is None:
delattr(managed_cluster.service_principal_profile, attr)
ap_profile.pop(attr, None)
if managed_cluster.service_principal_profile is not None:
for attr in sp_attrs:
if getattr(managed_cluster.service_principal_profile, attr, None) is None:
managed_cluster.service_principal_profile.pop(attr, None)
return managed_clusters


Expand Down Expand Up @@ -1835,14 +1841,14 @@ def aks_get_credentials(cmd, client, resource_group_name, name, admin=False,
if admin:
if cmd.cli_ctx.cloud.profile == "latest":
credentialResults = client.list_cluster_admin_credentials(
resource_group_name, name, serverType)
resource_group_name, name, server_fqdn=serverType)
else:
credentialResults = client.list_cluster_admin_credentials(
resource_group_name, name)
else:
if cmd.cli_ctx.cloud.profile == "latest":
credentialResults = client.list_cluster_user_credentials(
resource_group_name, name, serverType, credential_format)
resource_group_name, name, server_fqdn=serverType, format=credential_format)
else:
credentialResults = client.list_cluster_user_credentials(
resource_group_name, name)
Expand Down Expand Up @@ -3124,8 +3130,7 @@ def aks_agentpool_upgrade(cmd, client, resource_group_name, cluster_name,
nodepool_name,
instance,
headers=aks_custom_headers,
if_match=if_match,
if_none_match=if_none_match,
**build_etag_kwargs(if_match, if_none_match),
)


Expand All @@ -3140,7 +3145,7 @@ def aks_agentpool_scale(cmd, client, resource_group_name, cluster_name,
if new_node_count == instance.count:
raise CLIError(
"The new node count is the same as the current node count.")
if instance.type_properties_type == CONST_VIRTUAL_MACHINES:
if instance.properties.type_properties_type == CONST_VIRTUAL_MACHINES:
if len(instance.virtual_machines_profile.scale.manual) == 1:
instance.virtual_machines_profile.scale.manual[0].count = new_node_count
else:
Expand Down Expand Up @@ -3222,7 +3227,7 @@ def aks_agentpool_delete(cmd, client, resource_group_name, cluster_name,
if cmd.cli_ctx.cloud.profile != "latest":
return sdk_no_wait(no_wait, client.begin_delete, resource_group_name, cluster_name, nodepool_name)

return sdk_no_wait(no_wait, client.begin_delete, resource_group_name, cluster_name, nodepool_name, if_match=if_match, ignore_pod_disruption_budget=ignore_pdb)
return sdk_no_wait(no_wait, client.begin_delete, resource_group_name, cluster_name, nodepool_name, **build_etag_kwargs(if_match), ignore_pod_disruption_budget=ignore_pdb)


def aks_agentpool_operation_abort(cmd,
Expand Down Expand Up @@ -3324,7 +3329,7 @@ def aks_agentpool_manual_scale_add(cmd,
node_count,
no_wait=False):
instance = client.get(resource_group_name, cluster_name, nodepool_name)
if instance.type_properties_type != CONST_VIRTUAL_MACHINES:
if instance.properties.type_properties_type != CONST_VIRTUAL_MACHINES:
raise ClientRequestError("Cannot add manual to a non-virtualmachines node pool.")

ManualScaleProfile = cmd.get_models(
Expand Down Expand Up @@ -3361,7 +3366,7 @@ def aks_agentpool_manual_scale_update(cmd, # pylint: disable=unused-argument
raise RequiredArgumentMissingError("specify --vm-sizes or --node-count or both.")

instance = client.get(resource_group_name, cluster_name, nodepool_name)
if instance.type_properties_type != CONST_VIRTUAL_MACHINES:
if instance.properties.type_properties_type != CONST_VIRTUAL_MACHINES:
raise ClientRequestError("Cannot update manual in a non-virtualmachines node pool.")

_current_vm_sizes = [x.strip().lower() for x in current_vm_sizes.split(",")]
Expand Down Expand Up @@ -3402,7 +3407,7 @@ def aks_agentpool_manual_scale_delete(cmd, # pylint: disable=unused-argument
current_vm_sizes,
no_wait=False):
instance = client.get(resource_group_name, cluster_name, nodepool_name)
if instance.type_properties_type != CONST_VIRTUAL_MACHINES:
if instance.properties.type_properties_type != CONST_VIRTUAL_MACHINES:
raise CLIError("Cannot delete manual in a non-virtualmachines node pool.")

_current_vm_sizes = [x.strip().lower() for x in current_vm_sizes.split(",")]
Expand Down
Loading
Loading