From c3b736a9c7440f76f92ec99f3ec915cee65d2121 Mon Sep 17 00:00:00 2001 From: Huiren Jiang Date: Tue, 18 Jul 2023 19:56:53 +0800 Subject: [PATCH 1/2] Use virtual switch in configuration settings --- .../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..7dd3145edd8 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 to 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 to 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..6fe1fc17715 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 to 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 to 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 From 246ce455caae06da566a39d9fa54afd9b473e6ef Mon Sep 17 00:00:00 2001 From: Huiren Jiang Date: Wed, 19 Jul 2023 07:22:00 +0800 Subject: [PATCH 2/2] Fix PR comments --- .../partner_extensions/AzureMonitorMetrics.py | 4 ++-- .../partner_extensions/ContainerInsights.py | 4 ++-- 2 files changed, 4 insertions(+), 4 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 7dd3145edd8..dc89e2b6050 100644 --- a/src/k8s-extension/azext_k8s_extension/partner_extensions/AzureMonitorMetrics.py +++ b/src/k8s-extension/azext_k8s_extension/partner_extensions/AzureMonitorMetrics.py @@ -60,7 +60,7 @@ def Create(self, cmd, client, resource_group_name, cluster_name, name, cluster_t cluster_type ) else: - logger.info("Provisioning to prerequisites is skipped") + logger.info("Provisioning of prerequisites is skipped") create_identity = True extension = Extension( @@ -83,7 +83,7 @@ def Delete(self, cmd, client, resource_group_name, cluster_name, name, cluster_t if (extension is not None) and (extension.configuration_settings is not None): if is_skip_prerequisites_specified(extension.configuration_settings): - logger.info("Deprovisioning to prerequisites is skipped") + logger.info("Deprovisioning of prerequisites is skipped") return cluster_subscription = get_subscription_id(cmd.cli_ctx) 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 6fe1fc17715..9f8e5b067ae 100644 --- a/src/k8s-extension/azext_k8s_extension/partner_extensions/ContainerInsights.py +++ b/src/k8s-extension/azext_k8s_extension/partner_extensions/ContainerInsights.py @@ -64,7 +64,7 @@ def Create(self, cmd, client, resource_group_name, cluster_name, name, cluster_t _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 to prerequisites is skipped") + logger.info("Provisioning of prerequisites is skipped") # NOTE-2: Return a valid Extension object, Instance name and flag for Identity create_identity = True @@ -91,7 +91,7 @@ def Delete(self, cmd, client, resource_group_name, cluster_name, name, cluster_t if (extension is not None) and (extension.configuration_settings is not None): if is_skip_prerequisites_specified(extension.configuration_settings): - logger.info("Deprovisioning to prerequisites is skipped") + logger.info("Deprovisioning of prerequisites is skipped") return subscription_id = get_subscription_id(cmd.cli_ctx)