forked from Azure/azure-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AKS] feat: Azure Monitor Metrics addon (v2) (Managed Prometheus) GA (A…
- Loading branch information
1 parent
f391873
commit 6b4249f
Showing
31 changed files
with
1,392 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
36 changes: 36 additions & 0 deletions
36
src/azure-cli/azure/cli/command_modules/acs/azuremonitormetrics/addonput.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
# -------------------------------------------------------------------------------------------- | ||
import json | ||
from azure.cli.command_modules.acs.azuremonitormetrics.constants import AKS_CLUSTER_API | ||
from azure.cli.core.azclierror import ( | ||
UnknownError, | ||
CLIError | ||
) | ||
|
||
|
||
# pylint: disable=line-too-long | ||
def addon_put(cmd, cluster_subscription, cluster_resource_group_name, cluster_name): | ||
from azure.cli.core.util import send_raw_request | ||
armendpoint = cmd.cli_ctx.cloud.endpoints.resource_manager | ||
feature_check_url = f"{armendpoint}/subscriptions/{cluster_subscription}/resourceGroups/{cluster_resource_group_name}/providers/Microsoft.ContainerService/managedClusters/{cluster_name}?api-version={AKS_CLUSTER_API}" | ||
try: | ||
headers = ['User-Agent=azuremonitormetrics.addon_get'] | ||
r = send_raw_request(cmd.cli_ctx, "GET", feature_check_url, | ||
body={}, headers=headers) | ||
except CLIError as e: | ||
raise UnknownError(e) | ||
json_response = json.loads(r.text) | ||
if "azureMonitorProfile" in json_response["properties"]: | ||
if "metrics" in json_response["properties"]["azureMonitorProfile"]: | ||
if json_response["properties"]["azureMonitorProfile"]["metrics"]["enabled"] is False: | ||
# What if enabled doesn't exist | ||
json_response["properties"]["azureMonitorProfile"]["metrics"]["enabled"] = True | ||
try: | ||
headers = ['User-Agent=azuremonitormetrics.addon_put'] | ||
body = json.dumps(json_response) | ||
r = send_raw_request(cmd.cli_ctx, "PUT", feature_check_url, | ||
body=body, headers=headers) | ||
except CLIError as e: | ||
raise UnknownError(e) |
Empty file.
89 changes: 89 additions & 0 deletions
89
src/azure-cli/azure/cli/command_modules/acs/azuremonitormetrics/amg/link.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
# -------------------------------------------------------------------------------------------- | ||
import json | ||
import uuid | ||
from knack.util import CLIError | ||
from azure.cli.command_modules.acs.azuremonitormetrics.constants import ( | ||
GRAFANA_API, | ||
GRAFANA_ROLE_ASSIGNMENT_API, | ||
GrafanaLink | ||
) | ||
from azure.cli.command_modules.acs.azuremonitormetrics.helper import sanitize_resource_id | ||
|
||
|
||
def link_grafana_instance(cmd, raw_parameters, azure_monitor_workspace_resource_id): | ||
from azure.cli.core.util import send_raw_request | ||
# GET grafana principal ID | ||
try: | ||
grafana_resource_id = raw_parameters.get("grafana_resource_id") | ||
if grafana_resource_id is None or grafana_resource_id == "": | ||
return GrafanaLink.NOPARAMPROVIDED | ||
grafana_resource_id = sanitize_resource_id(grafana_resource_id) | ||
grafanaURI = "{0}{1}?api-version={2}".format( | ||
cmd.cli_ctx.cloud.endpoints.resource_manager, | ||
grafana_resource_id, | ||
GRAFANA_API | ||
) | ||
headers = ['User-Agent=azuremonitormetrics.link_grafana_instance'] | ||
grafanaArmResponse = send_raw_request(cmd.cli_ctx, "GET", grafanaURI, body={}, headers=headers) | ||
servicePrincipalId = grafanaArmResponse.json()["identity"]["principalId"] | ||
except CLIError as e: | ||
raise CLIError(e) | ||
# Add Role Assignment | ||
try: | ||
MonitoringDataReader = "b0d8363b-8ddd-447d-831f-62ca05bff136" | ||
roleDefinitionURI = "{0}{1}/providers/Microsoft.Authorization/roleAssignments/{2}?api-version={3}".format( | ||
cmd.cli_ctx.cloud.endpoints.resource_manager, | ||
azure_monitor_workspace_resource_id, | ||
uuid.uuid4(), | ||
GRAFANA_ROLE_ASSIGNMENT_API | ||
) | ||
roleDefinitionId = "{0}/providers/Microsoft.Authorization/roleDefinitions/{1}".format( | ||
azure_monitor_workspace_resource_id, | ||
MonitoringDataReader | ||
) | ||
association_body = json.dumps({ | ||
"properties": { | ||
"roleDefinitionId": roleDefinitionId, | ||
"principalId": servicePrincipalId | ||
} | ||
}) | ||
headers = ['User-Agent=azuremonitormetrics.add_role_assignment'] | ||
send_raw_request(cmd.cli_ctx, "PUT", roleDefinitionURI, body=association_body, headers=headers) | ||
except CLIError as e: | ||
if e.response.status_code != 409: | ||
erroString = "Role Assingment failed. Please manually assign the `Monitoring Data Reader` role\ | ||
to the Azure Monitor Workspace ({0}) for the Azure Managed Grafana\ | ||
System Assigned Managed Identity ({1})".format( | ||
azure_monitor_workspace_resource_id, | ||
servicePrincipalId | ||
) | ||
print(erroString) | ||
# Setting up AMW Integration | ||
targetGrafanaArmPayload = grafanaArmResponse.json() | ||
if targetGrafanaArmPayload["properties"] is None: | ||
raise CLIError("Invalid grafana payload to add AMW integration") | ||
if "grafanaIntegrations" not in json.dumps(targetGrafanaArmPayload): | ||
targetGrafanaArmPayload["properties"]["grafanaIntegrations"] = {} | ||
if "azureMonitorWorkspaceIntegrations" not in json.dumps(targetGrafanaArmPayload): | ||
targetGrafanaArmPayload["properties"]["grafanaIntegrations"]["azureMonitorWorkspaceIntegrations"] = [] | ||
amwIntegrations = targetGrafanaArmPayload["properties"]["grafanaIntegrations"]["azureMonitorWorkspaceIntegrations"] | ||
if amwIntegrations != [] and azure_monitor_workspace_resource_id in json.dumps(amwIntegrations).lower(): | ||
return GrafanaLink.ALREADYPRESENT | ||
try: | ||
grafanaURI = "{0}{1}?api-version={2}".format( | ||
cmd.cli_ctx.cloud.endpoints.resource_manager, | ||
grafana_resource_id, | ||
GRAFANA_API | ||
) | ||
targetGrafanaArmPayload["properties"]["grafanaIntegrations"]["azureMonitorWorkspaceIntegrations"].append({ | ||
"azureMonitorWorkspaceResourceId": azure_monitor_workspace_resource_id | ||
}) | ||
targetGrafanaArmPayload = json.dumps(targetGrafanaArmPayload) | ||
headers = ['User-Agent=azuremonitormetrics.setup_amw_grafana_integration', 'Content-Type=application/json'] | ||
send_raw_request(cmd.cli_ctx, "PUT", grafanaURI, body=targetGrafanaArmPayload, headers=headers) | ||
except CLIError as e: | ||
raise CLIError(e) | ||
return GrafanaLink.SUCCESS |
Empty file.
48 changes: 48 additions & 0 deletions
48
src/azure-cli/azure/cli/command_modules/acs/azuremonitormetrics/amw/create.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
# -------------------------------------------------------------------------------------------- | ||
import json | ||
|
||
from azure.cli.command_modules.acs.azuremonitormetrics.constants import MAC_API | ||
from azure.cli.command_modules.acs.azuremonitormetrics.amw.defaults import get_default_mac_name_and_region | ||
from azure.cli.command_modules.acs._client_factory import get_resource_groups_client, get_resources_client | ||
from azure.core.exceptions import HttpResponseError | ||
from knack.util import CLIError | ||
|
||
|
||
def create_default_mac(cmd, cluster_subscription, cluster_region): | ||
from azure.cli.core.util import send_raw_request | ||
default_mac_name, default_mac_region = get_default_mac_name_and_region(cmd, cluster_region) | ||
default_resource_group_name = "DefaultResourceGroup-{0}".format(default_mac_region) | ||
azure_monitor_workspace_resource_id = \ | ||
"/subscriptions/{0}/resourceGroups/{1}/providers/microsoft.monitor/accounts/{2}"\ | ||
.format( | ||
cluster_subscription, | ||
default_resource_group_name, | ||
default_mac_name | ||
) | ||
# Check if default resource group exists or not, if it does not then create it | ||
resource_groups = get_resource_groups_client(cmd.cli_ctx, cluster_subscription) | ||
resources = get_resources_client(cmd.cli_ctx, cluster_subscription) | ||
|
||
if resource_groups.check_existence(default_resource_group_name): | ||
try: | ||
resource = resources.get_by_id(azure_monitor_workspace_resource_id, MAC_API) | ||
# If MAC already exists then return from here | ||
return azure_monitor_workspace_resource_id, resource.location | ||
except HttpResponseError as ex: | ||
if ex.status_code != 404: | ||
raise ex | ||
else: | ||
resource_groups.create_or_update(default_resource_group_name, {"location": default_mac_region}) | ||
association_body = json.dumps({"location": default_mac_region, "properties": {}}) | ||
armendpoint = cmd.cli_ctx.cloud.endpoints.resource_manager | ||
association_url = f"{armendpoint}{azure_monitor_workspace_resource_id}?api-version={MAC_API}" | ||
try: | ||
headers = ['User-Agent=azuremonitormetrics.create_default_mac'] | ||
send_raw_request(cmd.cli_ctx, "PUT", association_url, | ||
body=association_body, headers=headers) | ||
return azure_monitor_workspace_resource_id, default_mac_region | ||
except CLIError as e: | ||
raise e |
45 changes: 45 additions & 0 deletions
45
src/azure-cli/azure/cli/command_modules/acs/azuremonitormetrics/amw/defaults.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
# -------------------------------------------------------------------------------------------- | ||
import json | ||
from azure.cli.command_modules.acs.azuremonitormetrics.deaults import get_default_region | ||
from azure.cli.command_modules.acs.azuremonitormetrics.responseparsers.amwlocationresponseparser import ( | ||
parseResourceProviderResponseForLocations | ||
) | ||
from azure.cli.command_modules.acs.azuremonitormetrics.constants import RP_LOCATION_API | ||
from knack.util import CLIError | ||
|
||
|
||
def get_supported_rp_locations(cmd, rp_name): | ||
from azure.cli.core.util import send_raw_request | ||
supported_locations = [] | ||
headers = ['User-Agent=azuremonitormetrics.get_supported_rp_locations'] | ||
armendpoint = cmd.cli_ctx.cloud.endpoints.resource_manager | ||
association_url = f"{armendpoint}/providers/{rp_name}?api-version={RP_LOCATION_API}" | ||
r = send_raw_request(cmd.cli_ctx, "GET", association_url, headers=headers) | ||
data = json.loads(r.text) | ||
supported_locations = parseResourceProviderResponseForLocations(data) | ||
return supported_locations | ||
|
||
|
||
def get_default_mac_region(cmd, cluster_region): | ||
supported_locations = get_supported_rp_locations(cmd, 'Microsoft.Monitor') | ||
if cluster_region in supported_locations: | ||
return cluster_region | ||
if len(supported_locations) > 0: | ||
return supported_locations[0] | ||
cloud_name = cmd.cli_ctx.cloud.name | ||
if cloud_name.lower() == 'azurechinacloud': | ||
raise CLIError("Azure China Cloud is not supported for the Azure Monitor Metrics addon") | ||
if cloud_name.lower() == 'azureusgovernment': | ||
return "usgovvirginia" | ||
# default to public cloud | ||
return get_default_region(cmd) | ||
|
||
|
||
def get_default_mac_name_and_region(cmd, cluster_region): | ||
default_mac_region = get_default_mac_region(cmd, cluster_region) | ||
default_mac_name = "DefaultAzureMonitorWorkspace-" + default_mac_region | ||
default_mac_name = default_mac_name[0:43] | ||
return default_mac_name, default_mac_region |
Oops, something went wrong.