From c105a3b4b1e6f2da98391a6319b5da9f94f94964 Mon Sep 17 00:00:00 2001 From: RMcVelia Date: Mon, 4 Dec 2023 17:46:13 +0000 Subject: [PATCH] WIP --- aks/application/data.tf | 19 +++++++++++++++++++ aks/application/resources.tf | 34 ++++++++++++++++++++++++++++++++++ aks/application/variables.tf | 17 +++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 aks/application/data.tf diff --git a/aks/application/data.tf b/aks/application/data.tf new file mode 100644 index 0000000..ee023a0 --- /dev/null +++ b/aks/application/data.tf @@ -0,0 +1,19 @@ +data "azurerm_resource_group" "monitoring" { + count = var.azure_enable_container_monitoring ? 1 : 0 + + name = "${var.azure_resource_prefix}-${var.service_short}-mn-rg" +} + +data "azurerm_monitor_action_group" "main" { + count = var.azure_enable_container_monitoring ? 1 : 0 + + name = "${var.azure_resource_prefix}-${var.service_name}" + resource_group_name = data.azurerm_resource_group.monitoring[0].name +} + +data "azurerm_kubernetes_cluster" "main" { + count = var.azure_enable_container_monitoring ? 1 : 0 + + name = "${var.cluster_configuration_map.resource_prefix}-aks" + resource_group_name = var.cluster_configuration_map.resource_group_name +} diff --git a/aks/application/resources.tf b/aks/application/resources.tf index f6d3faa..1ab3c50 100644 --- a/aks/application/resources.tf +++ b/aks/application/resources.tf @@ -244,3 +244,37 @@ resource "kubernetes_secret" "ghcr_auth" { }) } } + +resource "azurerm_monitor_metric_alert" "container_restarts" { + count = var.azure_enable_container_monitoring ? 1 : 0 + + name = "${local.app_name}-restarts" + resource_group_name = data.azurerm_resource_group.monitoring[0].name + scopes = [data.azurerm_kubernetes_cluster.main[0].id] + description = "Action will be triggered when container restarts greate than 0" + window_size = "PT30M" + + criteria { + metric_namespace = "Insights.container/pods" + metric_name = "restartingContainerCount" + aggregation = "Maximum" + operator = "GreaterThan" + threshold = 0 + + dimension { + name = "controllerName" + operator = "Include" + values = ["${local.app_name}"] + } + } + + action { + action_group_id = data.azurerm_monitor_action_group.main[0].id + } + + lifecycle { + ignore_changes = [ + tags + ] + } +} diff --git a/aks/application/variables.tf b/aks/application/variables.tf index 97a206f..3b0477d 100644 --- a/aks/application/variables.tf +++ b/aks/application/variables.tf @@ -102,3 +102,20 @@ variable "github_personal_access_token" { default = null description = "Github Personal Access Token (PAT) of github_username" } + +variable "azure_resource_prefix" { + type = string + default = null + description = "Prefix of Azure resources for the service" +} + +variable "service_short" { + type = string + default = null + description = "Short name of the service" +} + +variable "azure_enable_container_monitoring" { + type = bool + default = false +}