From 0125ad76583b688f1775bb897425942056ade7d2 Mon Sep 17 00:00:00 2001 From: jonathan-innis Date: Thu, 1 Apr 2021 10:11:33 -0700 Subject: [PATCH 1/6] Add check for KubernetesConfiguration --- .../azext_k8s_extension/_client_factory.py | 5 ++++ .../azext_k8s_extension/_consts.py | 1 + .../azext_k8s_extension/_validators.py | 25 +++++++++++++++++++ .../azext_k8s_extension/custom.py | 7 ++++-- 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/k8s-extension/azext_k8s_extension/_validators.py diff --git a/src/k8s-extension/azext_k8s_extension/_client_factory.py b/src/k8s-extension/azext_k8s_extension/_client_factory.py index 1a9a10c2615..140c36b547e 100644 --- a/src/k8s-extension/azext_k8s_extension/_client_factory.py +++ b/src/k8s-extension/azext_k8s_extension/_client_factory.py @@ -29,3 +29,8 @@ def cf_resources(cli_ctx, subscription_id=None): def cf_log_analytics(cli_ctx, subscription_id=None): from azure.mgmt.loganalytics import LogAnalyticsManagementClient # pylint: disable=no-name-in-module return get_mgmt_service_client(cli_ctx, LogAnalyticsManagementClient, subscription_id=subscription_id) + + +def _resource_providers_client(cli_ctx): + from azure.mgmt.resource import ResourceManagementClient + return get_mgmt_service_client(cli_ctx, ResourceManagementClient).providers diff --git a/src/k8s-extension/azext_k8s_extension/_consts.py b/src/k8s-extension/azext_k8s_extension/_consts.py index d0fdaf7775f..a06bccac6fa 100644 --- a/src/k8s-extension/azext_k8s_extension/_consts.py +++ b/src/k8s-extension/azext_k8s_extension/_consts.py @@ -6,3 +6,4 @@ EXTENSION_NAME = 'k8s-extension' VERSION = "0.2.0" +PROVIDER_NAMESPACE = 'Microsoft.KubernetesConfiguration' diff --git a/src/k8s-extension/azext_k8s_extension/_validators.py b/src/k8s-extension/azext_k8s_extension/_validators.py new file mode 100644 index 00000000000..ab9a55ce0a1 --- /dev/null +++ b/src/k8s-extension/azext_k8s_extension/_validators.py @@ -0,0 +1,25 @@ +# -------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +# -------------------------------------------------------------------------------------------- + +from knack.log import get_logger +from azext_k8s_extension._client_factory import _resource_providers_client +import azext_k8s_extension._consts as consts + + +logger = get_logger(__name__) + + +def check_cc_registration(cmd): + try: + rp_client = _resource_providers_client(cmd.cli_ctx) + registration_state = rp_client.get(consts.PROVIDER_NAMESPACE).registration_state + + if registration_state != "Registered": + logger.warning("'Extensions' cannot be used because '%s' provider has not been registered." + "More details for registering this provider can be found here - " + "https://aka.ms/EnableCustomLocations", consts.PROVIDER_NAMESPACE) + except Exception: + logger.warning("Unable to fetch registration state of '%s' provider. " + "Failed to enable 'extensions' feature...", consts.PROVIDER_NAMESPACE) diff --git a/src/k8s-extension/azext_k8s_extension/custom.py b/src/k8s-extension/azext_k8s_extension/custom.py index ba9dbfce501..b4b54f80c8b 100644 --- a/src/k8s-extension/azext_k8s_extension/custom.py +++ b/src/k8s-extension/azext_k8s_extension/custom.py @@ -16,6 +16,7 @@ from azext_k8s_extension.vendored_sdks.models import ConfigurationIdentity from azext_k8s_extension.vendored_sdks.models import ErrorResponseException from azext_k8s_extension.vendored_sdks.models import Scope +from azext_k8s_extension._validators import check_cc_registration from azext_k8s_extension.partner_extensions.ContainerInsights import ContainerInsights from azext_k8s_extension.partner_extensions.AzureDefender import AzureDefender @@ -76,9 +77,8 @@ def create_k8s_extension(cmd, client, resource_group_name, cluster_name, name, c """Create a new Extension Instance. """ - extension_type_lower = extension_type.lower() - # Determine ClusterRP + extension_type_lower = extension_type.lower() cluster_rp = __get_cluster_rp(cluster_type) # Configuration Settings & Configuration Protected Settings @@ -133,6 +133,9 @@ def create_k8s_extension(cmd, client, resource_group_name, cluster_name, name, c __validate_version_and_auto_upgrade(extension_instance.version, extension_instance.auto_upgrade_minor_version) __validate_scope_after_customization(extension_instance.scope) + # Check that registration has been done on Microsoft.KubernetesConfiguration for the subscription + check_cc_registration(cmd) + # Create identity, if required if create_identity: extension_instance.identity, extension_instance.location = \ From 6c10657a1f2478d2262780f543d5c8a73129d1dc Mon Sep 17 00:00:00 2001 From: jonathan-innis Date: Thu, 1 Apr 2021 11:01:36 -0700 Subject: [PATCH 2/6] Disable pylint and rename --- src/k8s-extension/azext_k8s_extension/_validators.py | 3 ++- src/k8s-extension/azext_k8s_extension/custom.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/k8s-extension/azext_k8s_extension/_validators.py b/src/k8s-extension/azext_k8s_extension/_validators.py index ab9a55ce0a1..17b73828b06 100644 --- a/src/k8s-extension/azext_k8s_extension/_validators.py +++ b/src/k8s-extension/azext_k8s_extension/_validators.py @@ -11,7 +11,8 @@ logger = get_logger(__name__) -def check_cc_registration(cmd): +# pylint: disable=broad-except +def _validate_cc_registration(cmd): try: rp_client = _resource_providers_client(cmd.cli_ctx) registration_state = rp_client.get(consts.PROVIDER_NAMESPACE).registration_state diff --git a/src/k8s-extension/azext_k8s_extension/custom.py b/src/k8s-extension/azext_k8s_extension/custom.py index b4b54f80c8b..bf264db3ff4 100644 --- a/src/k8s-extension/azext_k8s_extension/custom.py +++ b/src/k8s-extension/azext_k8s_extension/custom.py @@ -16,7 +16,7 @@ from azext_k8s_extension.vendored_sdks.models import ConfigurationIdentity from azext_k8s_extension.vendored_sdks.models import ErrorResponseException from azext_k8s_extension.vendored_sdks.models import Scope -from azext_k8s_extension._validators import check_cc_registration +from azext_k8s_extension._validators import _validate_cc_registration from azext_k8s_extension.partner_extensions.ContainerInsights import ContainerInsights from azext_k8s_extension.partner_extensions.AzureDefender import AzureDefender @@ -134,7 +134,7 @@ def create_k8s_extension(cmd, client, resource_group_name, cluster_name, name, c __validate_scope_after_customization(extension_instance.scope) # Check that registration has been done on Microsoft.KubernetesConfiguration for the subscription - check_cc_registration(cmd) + _validate_cc_registration(cmd) # Create identity, if required if create_identity: From f0e6e506a9e8a56ff8617e12a6c3f18a5daabbd3 Mon Sep 17 00:00:00 2001 From: jonathan-innis Date: Mon, 3 May 2021 09:42:33 -0700 Subject: [PATCH 3/6] Update provider registration link --- src/k8s-extension/azext_k8s_extension/_validators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/k8s-extension/azext_k8s_extension/_validators.py b/src/k8s-extension/azext_k8s_extension/_validators.py index 17b73828b06..319a5259ffa 100644 --- a/src/k8s-extension/azext_k8s_extension/_validators.py +++ b/src/k8s-extension/azext_k8s_extension/_validators.py @@ -20,7 +20,7 @@ def _validate_cc_registration(cmd): if registration_state != "Registered": logger.warning("'Extensions' cannot be used because '%s' provider has not been registered." "More details for registering this provider can be found here - " - "https://aka.ms/EnableCustomLocations", consts.PROVIDER_NAMESPACE) + "https://aka.ms/RegisterKubernetesConfigurationProvider", consts.PROVIDER_NAMESPACE) except Exception: logger.warning("Unable to fetch registration state of '%s' provider. " "Failed to enable 'extensions' feature...", consts.PROVIDER_NAMESPACE) From 75971dbed547aaa94e62564a7f30239f9f9f3e61 Mon Sep 17 00:00:00 2001 From: jonathan-innis Date: Mon, 3 May 2021 09:45:49 -0700 Subject: [PATCH 4/6] Update version --- src/k8s-extension/HISTORY.rst | 5 +++++ src/k8s-extension/setup.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/k8s-extension/HISTORY.rst b/src/k8s-extension/HISTORY.rst index dbf39b332fe..89016d5f307 100644 --- a/src/k8s-extension/HISTORY.rst +++ b/src/k8s-extension/HISTORY.rst @@ -3,6 +3,11 @@ Release History =============== +0.3.1 +++++++++++++++++++ + +* Add provider registration to check to validations + 0.3.0 ++++++++++++++++++ diff --git a/src/k8s-extension/setup.py b/src/k8s-extension/setup.py index 3c9a1882a27..b3512c976db 100644 --- a/src/k8s-extension/setup.py +++ b/src/k8s-extension/setup.py @@ -32,7 +32,7 @@ # TODO: Add any additional SDK dependencies here DEPENDENCIES = [] -VERSION = "0.3.0" +VERSION = "0.3.1" with open('README.rst', 'r', encoding='utf-8') as f: README = f.read() From f5f044863d2f8e78615d591ca44f864daf4584d5 Mon Sep 17 00:00:00 2001 From: jonathan-innis Date: Mon, 3 May 2021 09:51:29 -0700 Subject: [PATCH 5/6] Remove extra blank line --- src/k8s-extension/azext_k8s_extension/consts.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/k8s-extension/azext_k8s_extension/consts.py b/src/k8s-extension/azext_k8s_extension/consts.py index 39fc42bced0..2f39b4d7247 100644 --- a/src/k8s-extension/azext_k8s_extension/consts.py +++ b/src/k8s-extension/azext_k8s_extension/consts.py @@ -7,4 +7,3 @@ EXTENSION_NAME = 'k8s-extension' EXTENSION_PACKAGE_NAME = "azext_k8s_extension" PROVIDER_NAMESPACE = 'Microsoft.KubernetesConfiguration' - From 799df834f196c3a7f73d2f272109dd25ef8e127e Mon Sep 17 00:00:00 2001 From: jonathan-innis Date: Fri, 7 May 2021 09:58:43 -0700 Subject: [PATCH 6/6] Fix bug in import --- src/k8s-extension/azext_k8s_extension/_validators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/k8s-extension/azext_k8s_extension/_validators.py b/src/k8s-extension/azext_k8s_extension/_validators.py index 319a5259ffa..ee44f0e68e8 100644 --- a/src/k8s-extension/azext_k8s_extension/_validators.py +++ b/src/k8s-extension/azext_k8s_extension/_validators.py @@ -5,7 +5,7 @@ from knack.log import get_logger from azext_k8s_extension._client_factory import _resource_providers_client -import azext_k8s_extension._consts as consts +from . import consts logger = get_logger(__name__)