Skip to content

Commit

Permalink
Check Provider is Registered with Subscription Before Making Requests (
Browse files Browse the repository at this point in the history
…#18)

* Add check for KubernetesConfiguration

* Disable pylint and rename

* Update provider registration link

* Update version

* Remove extra blank line

* Fix bug in import
  • Loading branch information
jonathan-innis authored May 7, 2021
1 parent 9709504 commit 3f620a8
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/k8s-extension/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
Release History
===============

0.3.1
++++++++++++++++++

* Add provider registration to check to validations

0.3.0
++++++++++++++++++

Expand Down
5 changes: 5 additions & 0 deletions src/k8s-extension/azext_k8s_extension/_client_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 26 additions & 0 deletions src/k8s-extension/azext_k8s_extension/_validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# --------------------------------------------------------------------------------------------
# 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
from . import consts


logger = get_logger(__name__)


# 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

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/RegisterKubernetesConfigurationProvider", consts.PROVIDER_NAMESPACE)
except Exception:
logger.warning("Unable to fetch registration state of '%s' provider. "
"Failed to enable 'extensions' feature...", consts.PROVIDER_NAMESPACE)
1 change: 1 addition & 0 deletions src/k8s-extension/azext_k8s_extension/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@

EXTENSION_NAME = 'k8s-extension'
EXTENSION_PACKAGE_NAME = "azext_k8s_extension"
PROVIDER_NAMESPACE = 'Microsoft.KubernetesConfiguration'
13 changes: 8 additions & 5 deletions src/k8s-extension/azext_k8s_extension/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
from azure.cli.core.azclierror import ResourceNotFoundError, MutuallyExclusiveArgumentError, \
InvalidArgumentValueError, CommandNotFoundError, RequiredArgumentMissingError
from azure.cli.core.commands.client_factory import get_subscription_id
from .vendored_sdks.models import ConfigurationIdentity
from .vendored_sdks.models import ErrorResponseException
from .vendored_sdks.models import Scope
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 _validate_cc_registration

from .partner_extensions.ContainerInsights import ContainerInsights
from .partner_extensions.AzureDefender import AzureDefender
Expand Down Expand Up @@ -78,9 +79,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
Expand Down Expand Up @@ -135,6 +135,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
_validate_cc_registration(cmd)

# Create identity, if required
if create_identity:
extension_instance.identity, extension_instance.location = \
Expand Down
2 changes: 1 addition & 1 deletion src/k8s-extension/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 3f620a8

Please sign in to comment.