From 4fa705d9f40d636b9ea022eed0fc22703e16f992 Mon Sep 17 00:00:00 2001 From: ms-hujia <48512395+ms-hujia@users.noreply.github.com> Date: Tue, 1 Aug 2023 08:02:31 +0800 Subject: [PATCH] Add support to skip provisioning of prerequisites for Azure Monitor K8s extensions (#234) --- .../partner_extensions/AzureMonitorMetrics.py | 37 +++++++++++++------ .../partner_extensions/ContainerInsights.py | 14 +++++-- .../azext_k8s_extension/utils.py | 14 +++++++ 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/k8s-extension/azext_k8s_extension/partner_extensions/AzureMonitorMetrics.py b/src/k8s-extension/azext_k8s_extension/partner_extensions/AzureMonitorMetrics.py index 9eb4a74dab0..dc89e2b6050 100644 --- a/src/k8s-extension/azext_k8s_extension/partner_extensions/AzureMonitorMetrics.py +++ b/src/k8s-extension/azext_k8s_extension/partner_extensions/AzureMonitorMetrics.py @@ -9,7 +9,7 @@ import json import re -from ..utils import get_cluster_rp_api_version +from ..utils import get_cluster_rp_api_version, is_skip_prerequisites_specified from knack.log import get_logger @@ -48,16 +48,19 @@ def Create(self, cmd, client, resource_group_name, cluster_name, name, cluster_t if release_train is None: release_train = 'stable' - cluster_subscription = get_subscription_id(cmd.cli_ctx) - ensure_azure_monitor_profile_prerequisites( - cmd, - cluster_rp, - cluster_subscription, - resource_group_name, - cluster_name, - configuration_settings, - cluster_type - ) + if not is_skip_prerequisites_specified(configuration_settings): + cluster_subscription = get_subscription_id(cmd.cli_ctx) + ensure_azure_monitor_profile_prerequisites( + cmd, + cluster_rp, + cluster_subscription, + resource_group_name, + cluster_name, + configuration_settings, + cluster_type + ) + else: + logger.info("Provisioning of prerequisites is skipped") create_identity = True extension = Extension( @@ -72,6 +75,16 @@ def Create(self, cmd, client, resource_group_name, cluster_name, name, cluster_t return extension, name, create_identity def Delete(self, cmd, client, resource_group_name, cluster_name, name, cluster_type, cluster_rp, yes): - # cluster_rp, _ = get_cluster_rp_api_version(cluster_type=cluster_type, cluster_rp=cluster_rp) + cluster_rp, _ = get_cluster_rp_api_version(cluster_type=cluster_type, cluster_rp=cluster_rp) + try: + extension = client.get(resource_group_name, cluster_rp, cluster_type, cluster_name, name) + except Exception: + pass # its OK to ignore the exception since MSI auth in preview + + if (extension is not None) and (extension.configuration_settings is not None): + if is_skip_prerequisites_specified(extension.configuration_settings): + logger.info("Deprovisioning of prerequisites is skipped") + return + cluster_subscription = get_subscription_id(cmd.cli_ctx) unlink_azure_monitor_profile_artifacts(cmd, cluster_subscription, resource_group_name, cluster_name) diff --git a/src/k8s-extension/azext_k8s_extension/partner_extensions/ContainerInsights.py b/src/k8s-extension/azext_k8s_extension/partner_extensions/ContainerInsights.py index 119f29421e9..9f8e5b067ae 100644 --- a/src/k8s-extension/azext_k8s_extension/partner_extensions/ContainerInsights.py +++ b/src/k8s-extension/azext_k8s_extension/partner_extensions/ContainerInsights.py @@ -9,7 +9,7 @@ import json import re -from ..utils import get_cluster_rp_api_version +from ..utils import get_cluster_rp_api_version, is_skip_prerequisites_specified from .. import consts from knack.log import get_logger @@ -60,8 +60,11 @@ def Create(self, cmd, client, resource_group_name, cluster_name, name, cluster_t 'only supports cluster scope and single instance of this extension.', extension_type) logger.warning("Defaulting to extension name '%s' and release-namespace '%s'", name, release_namespace) - _get_container_insights_settings(cmd, resource_group_name, cluster_rp, cluster_type, cluster_name, configuration_settings, - configuration_protected_settings, is_ci_extension_type) + if not is_skip_prerequisites_specified(configuration_settings): + _get_container_insights_settings(cmd, resource_group_name, cluster_rp, cluster_type, cluster_name, configuration_settings, + configuration_protected_settings, is_ci_extension_type) + else: + logger.info("Provisioning of prerequisites is skipped") # NOTE-2: Return a valid Extension object, Instance name and flag for Identity create_identity = True @@ -86,6 +89,11 @@ def Delete(self, cmd, client, resource_group_name, cluster_name, name, cluster_t except Exception: pass # its OK to ignore the exception since MSI auth in preview + if (extension is not None) and (extension.configuration_settings is not None): + if is_skip_prerequisites_specified(extension.configuration_settings): + logger.info("Deprovisioning of prerequisites is skipped") + return + subscription_id = get_subscription_id(cmd.cli_ctx) # handle cluster type here cluster_resource_id = '/subscriptions/{0}/resourceGroups/{1}/providers/{2}/{3}/{4}'.format(subscription_id, resource_group_name, cluster_rp, cluster_type, cluster_name) diff --git a/src/k8s-extension/azext_k8s_extension/utils.py b/src/k8s-extension/azext_k8s_extension/utils.py index 11112935c87..66740036efa 100644 --- a/src/k8s-extension/azext_k8s_extension/utils.py +++ b/src/k8s-extension/azext_k8s_extension/utils.py @@ -54,3 +54,17 @@ def is_dogfood_cluster(cmd): urlparse(cmd.cli_ctx.cloud.endpoints.resource_manager).hostname == consts.DF_RM_HOSTNAME ) + + +def is_skip_prerequisites_specified(configuration_settings): + # Determine if provisioning to prerequisites should be skipped by a configuration setting named skipPrerequisites. + SKIP_PREQUISITES = 'skipPrerequisites' + + has_skip_prerequisites_set = False + + if SKIP_PREQUISITES in configuration_settings: + skip_prerequisites_configuration_setting = configuration_settings[SKIP_PREQUISITES] + if (isinstance(skip_prerequisites_configuration_setting, str) and str(skip_prerequisites_configuration_setting).lower() == "true") or (isinstance(skip_prerequisites_configuration_setting, bool) and skip_prerequisites_configuration_setting): + has_skip_prerequisites_set = True + + return has_skip_prerequisites_set