Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# --------------------------------------------------------------------------------------------
import json
from azext_aks_preview.azuremonitormetrics.constants import AKS_CLUSTER_API
from azure.cli.core.profiles import ResourceType
from azure.cli.core.azclierror import (
UnknownError,
CLIError
Expand All @@ -14,13 +13,16 @@
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}"
feature_check_url = (
f"{armendpoint}/subscriptions/{cluster_subscription}/resourceGroups/{cluster_resource_group_name}/providers/"
f"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)
raise UnknownError(e) # pylint: disable=raise-missing-from
json_response = json.loads(r.text)
if "azureMonitorProfile" in json_response["properties"]:
if "metrics" in json_response["properties"]["azureMonitorProfile"]:
Expand All @@ -33,4 +35,4 @@ def addon_put(cmd, cluster_subscription, cluster_resource_group_name, cluster_na
r = send_raw_request(cmd.cli_ctx, "PUT", feature_check_url,
body=body, headers=headers)
except CLIError as e:
raise UnknownError(e)
raise UnknownError(e) # pylint: disable=raise-missing-from
50 changes: 26 additions & 24 deletions src/aks-preview/azext_aks_preview/azuremonitormetrics/amg/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,39 @@ def link_grafana_instance(cmd, raw_parameters, azure_monitor_workspace_resource_
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
)
grafanaURI = f"{cmd.cli_ctx.cloud.endpoints.resource_manager}{grafana_resource_id}?api-version={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)
raise CLIError(e) # pylint: disable=raise-missing-from
# 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
roleDefinitionURI = (
f"{cmd.cli_ctx.cloud.endpoints.resource_manager}{azure_monitor_workspace_resource_id}/providers/"
f"Microsoft.Authorization/roleAssignments/{uuid.uuid4()}?api-version={GRAFANA_ROLE_ASSIGNMENT_API}"
)
roleDefinitionId = (
f"{azure_monitor_workspace_resource_id}/providers/"
f"Microsoft.Authorization/roleDefinitions/{MonitoringDataReader}"
)
roleDefinitionId = "{0}/providers/Microsoft.Authorization/roleDefinitions/{1}".format(
azure_monitor_workspace_resource_id,
MonitoringDataReader
association_body = json.dumps(
{
"properties": {
"roleDefinitionId": roleDefinitionId,
"principalId": servicePrincipalId,
}
}
)
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
erroString = (
"Role Assingment failed. Please manually assign the `Monitoring Data Reader` role to "
f"the Azure Monitor Workspace ({azure_monitor_workspace_resource_id}) for the Azure Managed Grafana "
f"System Assigned Managed Identity ({servicePrincipalId})"
)
print(erroString)
# Setting up AMW Integration
Expand All @@ -66,15 +68,15 @@ def link_grafana_instance(cmd, raw_parameters, azure_monitor_workspace_resource_
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
grafanaURI = f"{cmd.cli_ctx.cloud.endpoints.resource_manager}{grafana_resource_id}?api-version={GRAFANA_API}"
targetGrafanaArmPayload["properties"]["grafanaIntegrations"][
"azureMonitorWorkspaceIntegrations"
].append(
{"azureMonitorWorkspaceResourceId": azure_monitor_workspace_resource_id}
)
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)
raise CLIError(e) # pylint: disable=raise-missing-from
return GrafanaLink.SUCCESS
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@
# --------------------------------------------------------------------------------------------
import json

from azext_aks_preview.azuremonitormetrics.constants import MAC_API
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
from azext_aks_preview.azuremonitormetrics.constants import MAC_API
from azext_aks_preview.azuremonitormetrics.amw.defaults import get_default_mac_name_and_region

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)
default_resource_group_name = f"DefaultResourceGroup-{default_mac_region}"
azure_monitor_workspace_resource_id = (
f"/subscriptions/{cluster_subscription}/resourceGroups/{default_resource_group_name}/providers/"
f"microsoft.monitor/accounts/{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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
# --------------------------------------------------------------------------------------------
import json
from azext_aks_preview.azuremonitormetrics.deaults import get_default_region
from azext_aks_preview.azuremonitormetrics.responseparsers.amwlocationresponseparser import parseResourceProviderResponseForLocations
from azext_aks_preview.azuremonitormetrics.responseparsers.amwlocationresponseparser import (
parseResourceProviderResponseForLocations,
)
from azext_aks_preview.azuremonitormetrics.constants import RP_LOCATION_API
from knack.util import CLIError

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
# 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 azext_aks_preview.azuremonitormetrics.amw.create import create_default_mac
from azext_aks_preview.azuremonitormetrics.constants import MAC_API
from azext_aks_preview.azuremonitormetrics.helper import sanitize_resource_id
from azure.cli.core.azclierror import ClientRequestError
from azure.core.exceptions import HttpResponseError
from azure.cli.command_modules.acs._client_factory import get_resources_client
from knack.util import CLIError


def get_amw_region(cmd, azure_monitor_workspace_resource_id):
Expand All @@ -27,7 +24,11 @@ def get_amw_region(cmd, azure_monitor_workspace_resource_id):
def get_azure_monitor_workspace_resource(cmd, cluster_subscription, cluster_region, raw_parameters):
azure_monitor_workspace_resource_id = raw_parameters.get("azure_monitor_workspace_resource_id")
if not azure_monitor_workspace_resource_id:
azure_monitor_workspace_resource_id, azure_monitor_workspace_location = create_default_mac(cmd, cluster_subscription, cluster_region)
azure_monitor_workspace_resource_id, azure_monitor_workspace_location = create_default_mac(
cmd,
cluster_subscription,
cluster_region
)
else:
azure_monitor_workspace_resource_id = sanitize_resource_id(azure_monitor_workspace_resource_id)
azure_monitor_workspace_location = get_amw_region(cmd, azure_monitor_workspace_resource_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
from azure.core import CaseInsensitiveEnumMeta
from enum import Enum
from six import with_metaclass

from azure.core import CaseInsensitiveEnumMeta

AKS_CLUSTER_API = "2023-01-01"
MAC_API = "2023-04-03"
Expand Down Expand Up @@ -70,7 +70,7 @@
}


class GrafanaLink(with_metaclass(CaseInsensitiveEnumMeta, str, Enum)):
class GrafanaLink(str, Enum, metaclass=CaseInsensitiveEnumMeta):
"""
Status of Grafana link to the Prometheus Addon
"""
Expand All @@ -80,7 +80,7 @@ class GrafanaLink(with_metaclass(CaseInsensitiveEnumMeta, str, Enum)):
NOPARAMPROVIDED = "NOPARAMPROVIDED"


class DC_TYPE(with_metaclass(CaseInsensitiveEnumMeta, str, Enum)):
class DC_TYPE(str, Enum, metaclass=CaseInsensitiveEnumMeta):
"""
Types of DC* objects
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
import json
from azext_aks_preview.azuremonitormetrics.constants import DC_API
from azext_aks_preview.azuremonitormetrics.dc.defaults import get_default_dce_name
from azext_aks_preview.azuremonitormetrics.constants import DC_API
from knack.util import CLIError


def create_dce(cmd, cluster_subscription, cluster_resource_group_name, cluster_name, mac_region):
from azure.cli.core.util import send_raw_request
dce_name = get_default_dce_name(cmd, mac_region, cluster_name)
dce_resource_id = "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Insights/dataCollectionEndpoints/{2}".format(cluster_subscription, cluster_resource_group_name, dce_name)
dce_resource_id = (
f"/subscriptions/{cluster_subscription}/resourceGroups/{cluster_resource_group_name}/providers/"
f"Microsoft.Insights/dataCollectionEndpoints/{dce_name}"
)
try:
armendpoint = cmd.cli_ctx.cloud.endpoints.resource_manager
dce_url = f"{armendpoint}{dce_resource_id}?api-version={DC_API}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,60 @@


def get_default_dcr_name(cmd, mac_region, cluster_name):
region = get_default_region(cmd)
if mac_region in MapToClosestMACRegion:
region = MapToClosestMACRegion[mac_region]
region = MapToClosestMACRegion.get(mac_region, get_default_region(cmd))
default_dcr_name = "MSProm-" + region + "-" + cluster_name
return sanitize_name(default_dcr_name, DC_TYPE.DCR, 64)


# pylint: disable=too-many-locals,too-many-branches,too-many-statements,line-too-long
def create_dcr(cmd, mac_region, azure_monitor_workspace_resource_id, cluster_subscription, cluster_resource_group_name, cluster_name, dce_resource_id):
# pylint: disable=too-many-locals,too-many-branches,too-many-statements
def create_dcr(
cmd,
mac_region,
azure_monitor_workspace_resource_id,
cluster_subscription,
cluster_resource_group_name,
cluster_name,
dce_resource_id
):
from azure.cli.core.util import send_raw_request
dcr_name = get_default_dcr_name(cmd, mac_region, cluster_name)
dcr_resource_id = "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Insights/dataCollectionRules/{2}".format(
cluster_subscription,
cluster_resource_group_name,
dcr_name
dcr_resource_id = (
f"/subscriptions/{cluster_subscription}/resourceGroups/{cluster_resource_group_name}/providers/"
f"Microsoft.Insights/dataCollectionRules/{dcr_name}"
)
dcr_creation_body = json.dumps(
{
"location": mac_region,
"kind": "Linux",
"properties": {
"dataCollectionEndpointId": dce_resource_id,
"dataSources": {
"prometheusForwarder": [
{
"name": "PrometheusDataSource",
"streams": ["Microsoft-PrometheusMetrics"],
"labelIncludeFilter": {},
}
]
},
"dataFlows": [
{
"destinations": ["MonitoringAccount1"],
"streams": ["Microsoft-PrometheusMetrics"],
}
],
"description": "DCR description",
"destinations": {
"monitoringAccounts": [
{
"accountResourceId": azure_monitor_workspace_resource_id,
"name": "MonitoringAccount1",
}
]
},
},
}
)
dcr_creation_body = json.dumps({"location": mac_region,
"kind": "Linux",
"properties": {
"dataCollectionEndpointId": dce_resource_id,
"dataSources": {"prometheusForwarder": [{"name": "PrometheusDataSource", "streams": ["Microsoft-PrometheusMetrics"], "labelIncludeFilter": {}}]},
"dataFlows": [{"destinations": ["MonitoringAccount1"], "streams": ["Microsoft-PrometheusMetrics"]}],
"description": "DCR description",
"destinations": {
"monitoringAccounts": [{"accountResourceId": azure_monitor_workspace_resource_id, "name": "MonitoringAccount1"}]}}})
armendpoint = cmd.cli_ctx.cloud.endpoints.resource_manager
dcr_url = f"{armendpoint}{dcr_resource_id}?api-version={DC_API}"
try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,30 @@

def create_dcra(cmd, cluster_region, cluster_subscription, cluster_resource_group_name, cluster_name, dcr_resource_id):
from azure.cli.core.util import send_raw_request
cluster_resource_id = "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.ContainerService/managedClusters/{2}".format(
cluster_subscription,
cluster_resource_group_name,
cluster_name
cluster_resource_id = (
f"/subscriptions/{cluster_subscription}/resourceGroups/{cluster_resource_group_name}/providers/"
f"Microsoft.ContainerService/managedClusters/{cluster_name}"
)
dcra_name = get_default_dcra_name(cmd, cluster_region, cluster_name)
dcra_resource_id = "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Insights/dataCollectionRuleAssociations/{2}".format(
cluster_subscription,
cluster_resource_group_name,
dcra_name
dcra_resource_id = (
f"/subscriptions/{cluster_subscription}/resourceGroups/{cluster_resource_group_name}/providers/"
f"Microsoft.Insights/dataCollectionRuleAssociations/{dcra_name}"
)
# only create or delete the association between the DCR and cluster
association_body = json.dumps({"location": cluster_region,
"properties": {
"dataCollectionRuleId": dcr_resource_id,
"description": "Promtheus data collection association between DCR, DCE and target AKS resource"
}})
association_body = json.dumps(
{
"location": cluster_region,
"properties": {
"dataCollectionRuleId": dcr_resource_id,
"description": "Promtheus data collection association between DCR, DCE and target AKS resource",
},
}
)
armendpoint = cmd.cli_ctx.cloud.endpoints.resource_manager
association_url = f"{armendpoint}{cluster_resource_id}/providers/Microsoft.Insights/dataCollectionRuleAssociations/{dcra_name}?api-version={DC_API}"
association_url = (
f"{armendpoint}{cluster_resource_id}/providers/Microsoft.Insights/"
f"dataCollectionRuleAssociations/{dcra_name}?api-version={DC_API}"
)
try:
headers = ['User-Agent=azuremonitormetrics.create_dcra']
send_raw_request(cmd.cli_ctx, "PUT", association_url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,26 @@
# DCR = 64, DCE = 44, DCRA = 64
# All DC* object names should end only in alpha numeric (after `length` trim)
# DCE remove underscore from cluster name
def sanitize_name(name, type, length):
def sanitize_name(name, dc_type, length):
length = length - 1
if type == DC_TYPE.DCE:
if dc_type == DC_TYPE.DCE:
name = name.replace("_", "")
name = name[0:length]
lastIndexAlphaNumeric = len(name) - 1
while ((name[lastIndexAlphaNumeric].isalnum() is False) and lastIndexAlphaNumeric > -1):
lastIndexAlphaNumeric = lastIndexAlphaNumeric - 1
if (lastIndexAlphaNumeric < 0):
if lastIndexAlphaNumeric < 0:
return ""
return name[0:lastIndexAlphaNumeric + 1]


def get_default_dce_name(cmd, mac_region, cluster_name):
region = get_default_region(cmd)
if mac_region in MapToClosestMACRegion:
region = MapToClosestMACRegion[mac_region]
region = MapToClosestMACRegion.get(mac_region, get_default_region(cmd))
default_dce_name = "MSProm-" + region + "-" + cluster_name
return sanitize_name(default_dce_name, DC_TYPE.DCE, 44)


def get_default_dcra_name(cmd, cluster_region, cluster_name):
region = get_default_region(cmd)
if cluster_region in MapToClosestMACRegion:
region = MapToClosestMACRegion[cluster_region]
region = MapToClosestMACRegion.get(cluster_region, get_default_region(cmd))
default_dcra_name = "ContainerInsightsMetricsExtension-" + region + "-" + cluster_name
return sanitize_name(default_dcra_name, DC_TYPE.DCRA, 64)
Loading