Skip to content

Commit 74e9609

Browse files
authored
{AKS} Monitoring addon MSI auth - Remove unused ContainerInsights Solution resource (#23076)
1 parent ccdf7ed commit 74e9609

File tree

1 file changed

+1
-171
lines changed

1 file changed

+1
-171
lines changed

src/azure-cli/azure/cli/command_modules/acs/addonconfiguration.py

Lines changed: 1 addition & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# Copyright (c) Microsoft Corporation. All rights reserved.
33
# Licensed under the MIT License. See License.txt in the project root for license information.
44
# --------------------------------------------------------------------------------------------
5-
import datetime
65
import json
76

87
from azure.cli.command_modules.acs._client_factory import get_resource_groups_client, get_resources_client
@@ -18,10 +17,8 @@
1817
from azure.cli.command_modules.acs._resourcegroup import get_rg_location
1918
from azure.cli.command_modules.acs._roleassignments import add_role_assignment
2019
from azure.cli.core.azclierror import AzCLIError, ClientRequestError, CLIError
21-
from azure.cli.core.commands import LongRunningOperation
22-
from azure.cli.core.commands.client_factory import get_mgmt_service_client
2320
from azure.cli.core.profiles import ResourceType
24-
from azure.cli.core.util import sdk_no_wait, send_raw_request
21+
from azure.cli.core.util import send_raw_request
2522
from azure.core.exceptions import HttpResponseError
2623
from knack.log import get_logger
2724
from msrestazure.tools import parse_resource_id, resource_id
@@ -459,173 +456,6 @@ def ensure_container_insights_for_monitoring(
459456
error = e
460457
else:
461458
raise error
462-
if not _is_container_insights_solution_exists(cmd, workspace_resource_id):
463-
unix_time_in_millis = int(
464-
(
465-
datetime.datetime.utcnow() -
466-
datetime.datetime.utcfromtimestamp(0)
467-
).total_seconds() * 1000.0
468-
)
469-
470-
solution_deployment_name = "ContainerInsights-{}".format(
471-
unix_time_in_millis
472-
)
473-
474-
# pylint: disable=line-too-long
475-
template = {
476-
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
477-
"contentVersion": "1.0.0.0",
478-
"parameters": {
479-
"workspaceResourceId": {
480-
"type": "string",
481-
"metadata": {
482-
"description": "Azure Monitor Log Analytics Resource ID"
483-
},
484-
},
485-
"workspaceRegion": {
486-
"type": "string",
487-
"metadata": {
488-
"description": "Azure Monitor Log Analytics workspace region"
489-
},
490-
},
491-
"solutionDeploymentName": {
492-
"type": "string",
493-
"metadata": {
494-
"description": "Name of the solution deployment"
495-
},
496-
},
497-
},
498-
"resources": [
499-
{
500-
"type": "Microsoft.Resources/deployments",
501-
"name": "[parameters('solutionDeploymentName')]",
502-
"apiVersion": "2017-05-10",
503-
"subscriptionId": "[split(parameters('workspaceResourceId'),'/')[2]]",
504-
"resourceGroup": "[split(parameters('workspaceResourceId'),'/')[4]]",
505-
"properties": {
506-
"mode": "Incremental",
507-
"template": {
508-
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
509-
"contentVersion": "1.0.0.0",
510-
"parameters": {},
511-
"variables": {},
512-
"resources": [
513-
{
514-
"apiVersion": "2015-11-01-preview",
515-
"type": "Microsoft.OperationsManagement/solutions",
516-
"location": "[parameters('workspaceRegion')]",
517-
"name": "[Concat('ContainerInsights', '(', split(parameters('workspaceResourceId'),'/')[8], ')')]",
518-
"properties": {
519-
"workspaceResourceId": "[parameters('workspaceResourceId')]"
520-
},
521-
"plan": {
522-
"name": "[Concat('ContainerInsights', '(', split(parameters('workspaceResourceId'),'/')[8], ')')]",
523-
"product": "[Concat('OMSGallery/', 'ContainerInsights')]",
524-
"promotionCode": "",
525-
"publisher": "Microsoft",
526-
},
527-
}
528-
],
529-
},
530-
"parameters": {},
531-
},
532-
}
533-
],
534-
}
535-
536-
params = {
537-
"workspaceResourceId": {"value": workspace_resource_id},
538-
"workspaceRegion": {"value": location},
539-
"solutionDeploymentName": {"value": solution_deployment_name},
540-
}
541-
542-
deployment_name = "aks-monitoring-{}".format(unix_time_in_millis)
543-
# publish the Container Insights solution to the Log Analytics workspace
544-
return _invoke_deployment(
545-
cmd,
546-
resource_group,
547-
deployment_name,
548-
template,
549-
params,
550-
validate=False,
551-
no_wait=False,
552-
subscription_id=subscription_id,)
553-
554-
555-
def _is_container_insights_solution_exists(cmd, workspace_resource_id):
556-
# extract subscription ID and resource group from workspace_resource_id URL
557-
is_exists = False
558-
_MAX_RETRY_TIMES = 3
559-
parsed = parse_resource_id(workspace_resource_id)
560-
subscription_id, resource_group, workspace_name = parsed[
561-
"subscription"], parsed["resource_group"], parsed["name"]
562-
solution_resource_id = "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.OperationsManagement/solutions/ContainerInsights({2})".format(
563-
subscription_id, resource_group, workspace_name)
564-
resources = get_resources_client(cmd.cli_ctx, subscription_id)
565-
for retry_count in range(0, _MAX_RETRY_TIMES):
566-
try:
567-
resources.get_by_id(solution_resource_id, '2015-11-01-preview')
568-
is_exists = True
569-
break
570-
except HttpResponseError as ex:
571-
if ex.status_code == 404:
572-
is_exists = False
573-
break
574-
if retry_count >= (_MAX_RETRY_TIMES - 1):
575-
raise ex
576-
return is_exists
577-
578-
579-
def _invoke_deployment(
580-
cmd,
581-
resource_group_name,
582-
deployment_name,
583-
template,
584-
parameters,
585-
validate,
586-
no_wait,
587-
subscription_id=None,
588-
):
589-
DeploymentProperties = cmd.get_models(
590-
"DeploymentProperties",
591-
resource_type=ResourceType.MGMT_RESOURCE_RESOURCES,
592-
)
593-
properties = DeploymentProperties(
594-
template=template, parameters=parameters, mode="incremental"
595-
)
596-
smc = get_mgmt_service_client(
597-
cmd.cli_ctx,
598-
ResourceType.MGMT_RESOURCE_RESOURCES,
599-
subscription_id=subscription_id,
600-
).deployments
601-
if validate:
602-
logger.info("==== BEGIN TEMPLATE ====")
603-
logger.info(json.dumps(template, indent=2))
604-
logger.info("==== END TEMPLATE ====")
605-
606-
Deployment = cmd.get_models(
607-
"Deployment", resource_type=ResourceType.MGMT_RESOURCE_RESOURCES
608-
)
609-
deployment = Deployment(properties=properties)
610-
611-
if validate:
612-
if cmd.supported_api_version(
613-
min_api="2019-10-01",
614-
resource_type=ResourceType.MGMT_RESOURCE_RESOURCES,
615-
):
616-
validation_poller = smc.begin_validate(
617-
resource_group_name, deployment_name, deployment
618-
)
619-
return LongRunningOperation(cmd.cli_ctx)(validation_poller)
620-
return smc.validate(resource_group_name, deployment_name, deployment)
621-
622-
return sdk_no_wait(
623-
no_wait,
624-
smc.begin_create_or_update,
625-
resource_group_name,
626-
deployment_name,
627-
deployment,
628-
)
629459

630460

631461
def add_monitoring_role_assignment(result, cluster_resource_id, cmd):

0 commit comments

Comments
 (0)