Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{AKS} Fix snapshot not resolved according to the subscriptions field in the --snapshot-id option #5070

Merged
merged 6 commits into from
Jul 5, 2022
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: 6 additions & 1 deletion src/aks-preview/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ To release a new version, please select a new version number (usually plus 1 to
Pending
+++++++

0.5.87
++++++

* Fix snapshot not resolved according to the subscriptions field in the `--snapshot-id`` option.

0.5.86
+++++++
++++++

* Support network plugin mode for enabling Azure CNI Overlay preview feature.

Expand Down
4 changes: 2 additions & 2 deletions src/aks-preview/azext_aks_preview/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, cli_ctx=None):

def load_command_table(self, args):
super(ContainerServiceCommandsLoader, self).load_command_table(args)
from .commands import load_command_table
from azext_aks_preview.commands import load_command_table
load_command_table(self, args)
return self.command_table

Expand All @@ -44,7 +44,7 @@ def load_arguments(self, command):
else:
super().load_arguments(command)

from ._params import load_arguments
from azext_aks_preview._params import load_arguments
load_arguments(self, command)


Expand Down
44 changes: 23 additions & 21 deletions src/aks-preview/azext_aks_preview/_client_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
CUSTOM_MGMT_AKS_PREVIEW = CustomResourceType('azext_aks_preview.vendored_sdks.azure_mgmt_preview_aks',
'ContainerServiceClient')

# Note: cf_xxx, as the client_factory option value of a command group at command declaration, it should ignore
# parameters other than cli_ctx; get_xxx_client is used as the client of other services in the command implementation,
# and usually accepts subscription_id as a parameter to reconfigure the subscription when sending the request

def get_container_service_client(cli_ctx, **_):
return get_mgmt_service_client(cli_ctx, CUSTOM_MGMT_AKS_PREVIEW)


def cf_trustedaccess_role(cli_ctx, *_):
return get_container_service_client(cli_ctx).trusted_access_roles


def cf_trustedaccess_role_binding(cli_ctx, *_):
return get_container_service_client(cli_ctx).trusted_access_role_bindings
# container service clients
def get_container_service_client(cli_ctx, subscription_id=None):
return get_mgmt_service_client(cli_ctx, CUSTOM_MGMT_AKS_PREVIEW, subscription_id=subscription_id)


def cf_container_services(cli_ctx, *_):
Expand All @@ -46,40 +43,46 @@ def cf_nodepool_snapshots(cli_ctx, *_):
return get_container_service_client(cli_ctx).snapshots


# TODO: remove this
def cf_nodepool_snapshots_client(cli_ctx, subscription_id=None):
return get_mgmt_service_client(cli_ctx, CUSTOM_MGMT_AKS_PREVIEW, subscription_id=subscription_id).snapshots
def get_nodepool_snapshots_client(cli_ctx, subscription_id=None):
return get_container_service_client(cli_ctx, subscription_id=subscription_id).snapshots


def cf_mc_snapshots(cli_ctx, *_):
return get_container_service_client(cli_ctx).managed_cluster_snapshots


# TODO: remove this
def cf_mc_snapshots_client(cli_ctx, subscription_id=None):
return get_mgmt_service_client(cli_ctx, CUSTOM_MGMT_AKS_PREVIEW, subscription_id=subscription_id).managed_cluster_snapshots
def get_mc_snapshots_client(cli_ctx, subscription_id=None):
return get_container_service_client(cli_ctx, subscription_id=subscription_id).managed_cluster_snapshots


def cf_trustedaccess_role(cli_ctx, *_):
return get_container_service_client(cli_ctx).trusted_access_roles


def cf_trustedaccess_role_binding(cli_ctx, *_):
return get_container_service_client(cli_ctx).trusted_access_role_bindings


def cf_compute_service(cli_ctx, *_):
def get_compute_client(cli_ctx, *_):
return get_mgmt_service_client(cli_ctx, ResourceType.MGMT_COMPUTE)


def cf_resource_groups(cli_ctx, subscription_id=None):
def get_resource_groups_client(cli_ctx, subscription_id=None):
return get_mgmt_service_client(cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES,
subscription_id=subscription_id).resource_groups


def cf_resources(cli_ctx, subscription_id=None):
def get_resources_client(cli_ctx, subscription_id=None):
return get_mgmt_service_client(cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES,
subscription_id=subscription_id).resources


def cf_container_registry_service(cli_ctx, subscription_id=None):
def get_container_registry_client(cli_ctx, subscription_id=None):
return get_mgmt_service_client(cli_ctx, ResourceType.MGMT_CONTAINERREGISTRY,
subscription_id=subscription_id)


def cf_storage(cli_ctx, subscription_id=None):
def get_storage_client(cli_ctx, subscription_id=None):
return get_mgmt_service_client(cli_ctx, ResourceType.MGMT_STORAGE, subscription_id=subscription_id)


Expand Down Expand Up @@ -111,7 +114,6 @@ def get_graph_rbac_management_client(cli_ctx, **_):
return client


# pylint: disable=inconsistent-return-statements
def get_resource_by_name(cli_ctx, resource_name, resource_type):
"""Returns the ARM resource in the current subscription with resource_name.
:param str resource_name: The name of resource
Expand Down
12 changes: 6 additions & 6 deletions src/aks-preview/azext_aks_preview/_completers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def get_k8s_upgrades_completion_list(cmd, prefix, namespace, **kwargs): # pylin


def get_k8s_upgrades(cli_ctx, resource_group, name):
from ._client_factory import cf_managed_clusters
from azext_aks_preview._client_factory import cf_managed_clusters

results = cf_managed_clusters(cli_ctx).get_upgrade_profile(resource_group, name).as_dict()
return results['control_plane_profile']['upgrades']
Expand All @@ -33,7 +33,7 @@ def get_k8s_versions_completion_list(cmd, prefix, namespace, **kwargs): # pylin

def get_k8s_versions(cli_ctx, location):
"""Return a list of Kubernetes versions available for a new cluster."""
from ._client_factory import cf_container_services
from azext_aks_preview._client_factory import cf_container_services
from jmespath import search # pylint: disable=import-error

results = cf_container_services(cli_ctx).list_orchestrators(location, resource_type='managedClusters').as_dict()
Expand All @@ -57,9 +57,9 @@ def get_vm_size_completion_list(cmd, prefix, namespace, **kwargs): # pylint: di


def get_vm_sizes(cli_ctx, location):
from ._client_factory import cf_compute_service
from azext_aks_preview._client_factory import get_compute_client

return cf_compute_service(cli_ctx).virtual_machine_sizes.list(location)
return get_compute_client(cli_ctx).virtual_machine_sizes.list(location)


def _get_location(cli_ctx, namespace):
Expand All @@ -78,11 +78,11 @@ def _get_location(cli_ctx, namespace):


def _get_location_from_resource_group(cli_ctx, resource_group_name):
from ._client_factory import cf_resource_groups
from azext_aks_preview._client_factory import get_resource_groups_client
from msrestazure.azure_exceptions import CloudError

try:
rg = cf_resource_groups(cli_ctx).get(resource_group_name)
rg = get_resource_groups_client(cli_ctx).get(resource_group_name)
return rg.location
except CloudError as err:
# Print a warning if the user hit [TAB] but the `--resource-group` argument was incorrect.
Expand Down
16 changes: 9 additions & 7 deletions src/aks-preview/azext_aks_preview/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from knack.prompting import NoTTYException, prompt_y_n
from knack.util import CLIError

from azext_aks_preview._client_factory import cf_nodepool_snapshots, cf_mc_snapshots
from azext_aks_preview._client_factory import get_nodepool_snapshots_client, get_mc_snapshots_client

logger = get_logger(__name__)

Expand Down Expand Up @@ -216,14 +216,15 @@ def get_nodepool_snapshot_by_snapshot_id(cli_ctx, snapshot_id):
snapshot_id = snapshot_id.lower()
match = _re_snapshot_resource_id.search(snapshot_id)
if match:
subscription_id = match.group(1)
resource_group_name = match.group(2)
snapshot_name = match.group(3)
return get_nodepool_snapshot(cli_ctx, resource_group_name, snapshot_name)
return get_nodepool_snapshot(cli_ctx, subscription_id, resource_group_name, snapshot_name)
raise InvalidArgumentValueError("Cannot parse snapshot name from provided resource id '{}'.".format(snapshot_id))


def get_nodepool_snapshot(cli_ctx, resource_group_name, snapshot_name):
snapshot_client = cf_nodepool_snapshots(cli_ctx)
def get_nodepool_snapshot(cli_ctx, subscription_id, resource_group_name, snapshot_name):
snapshot_client = get_nodepool_snapshots_client(cli_ctx, subscription_id=subscription_id)
try:
snapshot = snapshot_client.get(resource_group_name, snapshot_name)
# track 2 sdk raise exception from azure.core.exceptions
Expand All @@ -242,15 +243,16 @@ def get_cluster_snapshot_by_snapshot_id(cli_ctx, snapshot_id):
snapshot_id = snapshot_id.lower()
match = _re_mc_snapshot_resource_id.search(snapshot_id)
if match:
subscription_id = match.group(1)
resource_group_name = match.group(2)
snapshot_name = match.group(3)
return get_cluster_snapshot(cli_ctx, resource_group_name, snapshot_name)
return get_cluster_snapshot(cli_ctx, subscription_id, resource_group_name, snapshot_name)
raise InvalidArgumentValueError(
"Cannot parse snapshot name from provided resource id {}.".format(snapshot_id))


def get_cluster_snapshot(cli_ctx, resource_group_name, snapshot_name):
snapshot_client = cf_mc_snapshots(cli_ctx)
def get_cluster_snapshot(cli_ctx, subscription_id, resource_group_name, snapshot_name):
snapshot_client = get_mc_snapshots_client(cli_ctx, subscription_id)
try:
snapshot = snapshot_client.get(resource_group_name, snapshot_name)
# track 2 sdk raise exception from azure.core.exceptions
Expand Down
4 changes: 2 additions & 2 deletions src/aks-preview/azext_aks_preview/_resourcegroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
# --------------------------------------------------------------------------------------------

from knack.util import CLIError
from azext_aks_preview._client_factory import cf_resource_groups
from azext_aks_preview._client_factory import get_resource_groups_client


def get_rg_location(ctx, resource_group_name, subscription_id=None):
groups = cf_resource_groups(ctx, subscription_id=subscription_id)
groups = get_resource_groups_client(ctx, subscription_id=subscription_id)
# Just do the get, we don't need the result, it will error out if the group doesn't exist.
rg = groups.get(resource_group_name)
if rg is None:
Expand Down
22 changes: 14 additions & 8 deletions src/aks-preview/azext_aks_preview/addonconfiguration.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from azure.cli.core.commands.client_factory import get_subscription_id
from azure.cli.core.util import sdk_no_wait
from azext_aks_preview._client_factory import CUSTOM_MGMT_AKS_PREVIEW
from azext_aks_preview._client_factory import cf_resources, cf_resource_groups
from azext_aks_preview._client_factory import get_resources_client, get_resource_groups_client
from azext_aks_preview._resourcegroup import get_rg_location
from azext_aks_preview._roleassignments import add_role_assignment
from azext_aks_preview._consts import (
Expand All @@ -35,10 +35,6 @@
CONST_ROTATION_POLL_INTERVAL,
CONST_KUBE_DASHBOARD_ADDON_NAME,
)
from .vendored_sdks.azure_mgmt_preview_aks.v2022_05_02_preview.models import (
ManagedClusterIngressProfile,
ManagedClusterIngressProfileWebAppRouting,
)

logger = get_logger(__name__)

Expand Down Expand Up @@ -177,6 +173,16 @@ def update_addons(cmd, # pylint: disable=too-many-branches,too-many-statements
resource_type=CUSTOM_MGMT_AKS_PREVIEW,
operation_group="managed_clusters",
)
ManagedClusterIngressProfile = cmd.get_models(
"ManagedClusterIngressProfile",
resource_type=CUSTOM_MGMT_AKS_PREVIEW,
operation_group="managed_clusters",
)
ManagedClusterIngressProfileWebAppRouting = cmd.get_models(
"ManagedClusterIngressProfileWebAppRouting",
resource_type=CUSTOM_MGMT_AKS_PREVIEW,
operation_group="managed_clusters",
)

# for each addons argument
for addon_arg in addon_args:
Expand Down Expand Up @@ -449,8 +455,8 @@ def ensure_default_log_analytics_workspace_for_monitoring(cmd, subscription_id,
default_workspace_resource_id = '/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.OperationalInsights' \
'/workspaces/{2}'.format(subscription_id,
default_workspace_resource_group, default_workspace_name)
resource_groups = cf_resource_groups(cmd.cli_ctx, subscription_id)
resources = cf_resources(cmd.cli_ctx, subscription_id)
resource_groups = get_resource_groups_client(cmd.cli_ctx, subscription_id)
resources = get_resources_client(cmd.cli_ctx, subscription_id)

from azure.cli.core.profiles import ResourceType
# check if default RG exists
Expand Down Expand Up @@ -546,7 +552,7 @@ def ensure_container_insights_for_monitoring(cmd,

# region of workspace can be different from region of RG so find the location of the workspace_resource_id
if not remove_monitoring:
resources = cf_resources(cmd.cli_ctx, subscription_id)
resources = get_resources_client(cmd.cli_ctx, subscription_id)
from azure.core.exceptions import HttpResponseError
try:
resource = resources.get_by_id(
Expand Down
4 changes: 2 additions & 2 deletions src/aks-preview/azext_aks_preview/aks_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from msrestazure.azure_exceptions import CloudError
from tabulate import tabulate

from azext_aks_preview._client_factory import cf_storage
from azext_aks_preview._client_factory import get_storage_client

from azext_aks_preview._consts import (
CONST_CONTAINER_NAME_MAX_LENGTH,
Expand Down Expand Up @@ -86,7 +86,7 @@ def aks_kollect_cmd(cmd, # pylint: disable=too-many-statements,too-many-local

readonly_sas_token = None
if sas_token is None:
storage_client = cf_storage(
storage_client = get_storage_client(
cmd.cli_ctx, parsed_storage_account['subscription'])
storage_account_keys = storage_client.storage_accounts.list_keys(parsed_storage_account['resource_group'],
storage_account_name)
Expand Down
42 changes: 24 additions & 18 deletions src/aks-preview/azext_aks_preview/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,30 @@

from azure.cli.core.commands import CliCommandType

from ._client_factory import cf_managed_clusters
from ._client_factory import cf_maintenance_configurations
from ._client_factory import cf_agent_pools
from ._client_factory import cf_nodepool_snapshots
from ._client_factory import cf_mc_snapshots
from ._client_factory import cf_trustedaccess_role
from ._client_factory import cf_trustedaccess_role_binding
from ._format import aks_show_table_format
from ._format import aks_addon_list_available_table_format, aks_addon_list_table_format, aks_addon_show_table_format
from ._format import aks_agentpool_show_table_format
from ._format import aks_agentpool_list_table_format
from ._format import aks_upgrades_table_format
from ._format import aks_pod_identities_table_format
from ._format import aks_pod_identity_exceptions_table_format
from ._format import aks_show_nodepool_snapshot_table_format
from ._format import aks_list_nodepool_snapshot_table_format
from ._format import aks_show_snapshot_table_format
from ._format import aks_list_snapshot_table_format
from azext_aks_preview._client_factory import (
cf_agent_pools,
cf_maintenance_configurations,
cf_managed_clusters,
cf_mc_snapshots,
cf_nodepool_snapshots,
cf_trustedaccess_role,
cf_trustedaccess_role_binding,
)
from azext_aks_preview._format import (
aks_addon_list_available_table_format,
aks_addon_list_table_format,
aks_addon_show_table_format,
aks_agentpool_list_table_format,
aks_agentpool_show_table_format,
aks_list_nodepool_snapshot_table_format,
aks_list_snapshot_table_format,
aks_pod_identities_table_format,
aks_pod_identity_exceptions_table_format,
aks_show_nodepool_snapshot_table_format,
aks_show_snapshot_table_format,
aks_show_table_format,
aks_upgrades_table_format,
)


def load_command_table(self, _):
Expand Down
Loading