Skip to content

Commit

Permalink
Add support to skip provisioning of prerequisites for Azure Monitor K…
Browse files Browse the repository at this point in the history
…8s extensions (#234)
  • Loading branch information
ms-hujia authored Aug 1, 2023
1 parent 2561f35 commit 4fa705d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions src/k8s-extension/azext_k8s_extension/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4fa705d

Please sign in to comment.