From 28af27da7955b4ae9770f8924533161297630027 Mon Sep 17 00:00:00 2001 From: Ganga Mahesh Siddem Date: Sat, 16 Apr 2022 20:31:49 -0700 Subject: [PATCH 1/3] bring back containerinsights solution in dcr mode --- .../partner_extensions/ContainerInsights.py | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) 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 fcacbb829bf..acb92d0995d 100644 --- a/src/k8s-extension/azext_k8s_extension/partner_extensions/ContainerInsights.py +++ b/src/k8s-extension/azext_k8s_extension/partner_extensions/ContainerInsights.py @@ -316,6 +316,28 @@ def _ensure_default_log_analytics_workspace_for_monitoring(cmd, subscription_id, return ws_resource_id +def _is_container_insights_solution_exists(cmd, workspace_resource_id): + # extract subscription ID and resource group from workspace_resource_id URL + is_exists = False + _MAX_RETRY_TIMES = 3 + parsed = parse_resource_id(workspace_resource_id) + subscription_id, resource_group, workspace_name = parsed["subscription"], parsed["resource_group"], parsed["name"] + solution_resource_id = "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.OperationsManagement/solutions/ContainerInsights({2})".format(subscription_id, resource_group, workspace_name) + resources = cf_resources(cmd.cli_ctx, subscription_id) + for retry_count in range(0, _MAX_RETRY_TIMES): + try: + resources.get_by_id(solution_resource_id, '2015-11-01-preview') + is_exists = True + break + except HttpResponseError as ex: + if ex.status_code == 404: + is_exists = False + break + if retry_count >= (_MAX_RETRY_TIMES - 1): + raise ex + return is_exists + + def _ensure_container_insights_for_monitoring(cmd, workspace_resource_id): # extract subscription ID and resource group from workspace_resource_id URL parsed = parse_resource_id(workspace_resource_id) @@ -461,11 +483,12 @@ def _get_container_insights_settings(cmd, cluster_resource_group_name, cluster_n raise InvalidArgumentValueError('{} is not a valid Azure resource ID.'.format(workspace_resource_id)) if is_ci_extension_type: + if not _is_container_insights_solution_exists(cmd, workspace_resource_id): + logger.info("creating containerinsights solution resource since it doesnt exists") + _ensure_container_insights_for_monitoring(cmd, workspace_resource_id).result() if useAADAuth: - logger.info("MSI onboarding since omsagent.useAADAuth set to true") + logger.info("creating data collection rule and association") _ensure_container_insights_dcr_for_monitoring(cmd, subscription_id, cluster_resource_group_name, cluster_name, workspace_resource_id) - else: - _ensure_container_insights_for_monitoring(cmd, workspace_resource_id).result() # extract subscription ID and resource group from workspace_resource_id URL parsed = parse_resource_id(workspace_resource_id) @@ -507,7 +530,7 @@ def get_existing_container_insights_extension_dcr_tags(cmd, dcr_url): cmd.cli_ctx, "GET", dcr_url ) json_response = json.loads(resp.text) - if json_response["tags"] is not None: + if ("tags" in json_response) and (json_response["tags"] is not None): tags = json_response["tags"] break except CLIError as e: From 8d4d3e1ba6b65f1711d573b2ddfc2b8efa885e55 Mon Sep 17 00:00:00 2001 From: Ganga Mahesh Siddem Date: Sat, 16 Apr 2022 20:33:46 -0700 Subject: [PATCH 2/3] bring back containerinsights solution in dcr mode --- .../azext_k8s_extension/partner_extensions/ContainerInsights.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 acb92d0995d..b2eecf32f89 100644 --- a/src/k8s-extension/azext_k8s_extension/partner_extensions/ContainerInsights.py +++ b/src/k8s-extension/azext_k8s_extension/partner_extensions/ContainerInsights.py @@ -484,7 +484,7 @@ def _get_container_insights_settings(cmd, cluster_resource_group_name, cluster_n if is_ci_extension_type: if not _is_container_insights_solution_exists(cmd, workspace_resource_id): - logger.info("creating containerinsights solution resource since it doesnt exists") + logger.info("creating containerinsights solution resource since it doesnt exist") _ensure_container_insights_for_monitoring(cmd, workspace_resource_id).result() if useAADAuth: logger.info("creating data collection rule and association") From 5aa76646d798c1f069088736173f960e4fcee276 Mon Sep 17 00:00:00 2001 From: Ganga Mahesh Siddem Date: Mon, 27 Jun 2022 17:52:47 -0700 Subject: [PATCH 3/3] update to stream group and DCR API version --- .../partner_extensions/ContainerInsights.py | 35 ++++--------------- 1 file changed, 7 insertions(+), 28 deletions(-) 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 b2eecf32f89..f2eeb3ff5be 100644 --- a/src/k8s-extension/azext_k8s_extension/partner_extensions/ContainerInsights.py +++ b/src/k8s-extension/azext_k8s_extension/partner_extensions/ContainerInsights.py @@ -92,7 +92,7 @@ def Delete(self, cmd, client, resource_group_name, cluster_name, name, cluster_t if (isinstance(useAADAuthSetting, str) and str(useAADAuthSetting).lower() == "true") or (isinstance(useAADAuthSetting, bool) and useAADAuthSetting): useAADAuth = True if useAADAuth: - association_url = cmd.cli_ctx.cloud.endpoints.resource_manager + f"{cluster_resource_id}/providers/Microsoft.Insights/dataCollectionRuleAssociations/ContainerInsightsExtension?api-version=2019-11-01-preview" + association_url = cmd.cli_ctx.cloud.endpoints.resource_manager + f"{cluster_resource_id}/providers/Microsoft.Insights/dataCollectionRuleAssociations/ContainerInsightsExtension?api-version=2021-04-01" for _ in range(3): try: send_raw_request(cmd.cli_ctx, "GET", association_url,) @@ -106,7 +106,7 @@ def Delete(self, cmd, client, resource_group_name, cluster_name, name, cluster_t pass # its OK to ignore the exception since MSI auth in preview if isDCRAExists: - association_url = cmd.cli_ctx.cloud.endpoints.resource_manager + f"{cluster_resource_id}/providers/Microsoft.Insights/dataCollectionRuleAssociations/ContainerInsightsExtension?api-version=2019-11-01-preview" + association_url = cmd.cli_ctx.cloud.endpoints.resource_manager + f"{cluster_resource_id}/providers/Microsoft.Insights/dataCollectionRuleAssociations/ContainerInsightsExtension?api-version=2021-04-01" for _ in range(3): try: send_raw_request(cmd.cli_ctx, "DELETE", association_url,) @@ -612,7 +612,7 @@ def _ensure_container_insights_dcr_for_monitoring(cmd, subscription_id, cluster_ if (cluster_region not in region_ids): raise ClientRequestError(f"Data Collection Rule Associations are not supported for cluster region {cluster_region}") - dcr_url = cmd.cli_ctx.cloud.endpoints.resource_manager + f"{dcr_resource_id}?api-version=2019-11-01-preview" + dcr_url = cmd.cli_ctx.cloud.endpoints.resource_manager + f"{dcr_resource_id}?api-version=2021-04-01" # get existing tags on the container insights extension DCR if the customer added any existing_tags = get_existing_container_insights_extension_dcr_tags(cmd, dcr_url) @@ -627,18 +627,7 @@ def _ensure_container_insights_dcr_for_monitoring(cmd, subscription_id, cluster_ { "name": "ContainerInsightsExtension", "streams": [ - "Microsoft-Perf", - "Microsoft-ContainerInventory", - "Microsoft-ContainerLog", - "Microsoft-ContainerLogV2", - "Microsoft-ContainerNodeInventory", - "Microsoft-KubeEvents", - "Microsoft-KubeMonAgentEvents", - "Microsoft-KubeNodeInventory", - "Microsoft-KubePodInventory", - "Microsoft-KubePVInventory", - "Microsoft-KubeServices", - "Microsoft-InsightsMetrics", + "Microsoft-ContainerInsights-Group-Default" ], "extensionName": "ContainerInsights", } @@ -647,18 +636,8 @@ def _ensure_container_insights_dcr_for_monitoring(cmd, subscription_id, cluster_ "dataFlows": [ { "streams": [ - "Microsoft-Perf", - "Microsoft-ContainerInventory", - "Microsoft-ContainerLog", - "Microsoft-ContainerLogV2", - "Microsoft-ContainerNodeInventory", - "Microsoft-KubeEvents", - "Microsoft-KubeMonAgentEvents", - "Microsoft-KubeNodeInventory", - "Microsoft-KubePodInventory", - "Microsoft-KubePVInventory", - "Microsoft-KubeServices", - "Microsoft-InsightsMetrics", + "Microsoft-ContainerInsights-Group-Default" + ], "destinations": ["la-workspace"], } @@ -694,7 +673,7 @@ def _ensure_container_insights_dcr_for_monitoring(cmd, subscription_id, cluster_ }, } ) - association_url = cmd.cli_ctx.cloud.endpoints.resource_manager + f"{cluster_resource_id}/providers/Microsoft.Insights/dataCollectionRuleAssociations/ContainerInsightsExtension?api-version=2019-11-01-preview" + association_url = cmd.cli_ctx.cloud.endpoints.resource_manager + f"{cluster_resource_id}/providers/Microsoft.Insights/dataCollectionRuleAssociations/ContainerInsightsExtension?api-version=2021-04-01" for _ in range(3): try: send_raw_request(cmd.cli_ctx, "PUT", association_url, body=association_body,)