|
| 1 | +# Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved. |
| 2 | +# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl. |
| 3 | + |
| 4 | +locals { |
| 5 | + deployment_name = "kps" |
| 6 | + vars = { "region" = var.region, "tenancy_ocid" = var.tenancy_ocid } |
| 7 | + scrape_configs = flatten([for i in fileset("${path.module}/templates", "*.scrapeConfigs.yaml") : file("${path.module}/templates/${i}")]) |
| 8 | + grafana_datasources = flatten([for i in fileset("${path.module}/templates", "grafana.*.datasource.yaml") : yamldecode(templatefile("${path.module}/templates/${i}", local.vars))]) |
| 9 | + grafana_dashboards = flatten([for i in fileset("${path.module}/templates", "grafana.*.dashboard.json") : { "name" = i, "label" = split(".", i)[1] }]) |
| 10 | + grafana_plugins = file("${path.module}/templates/grafana.plugins.yaml") |
| 11 | +} |
| 12 | + |
| 13 | +output dash { |
| 14 | + value = local.grafana_dashboards |
| 15 | +} |
| 16 | + |
| 17 | +resource "random_password" "grafana_password" { |
| 18 | + count = local.enable_monitoring_stack ? 1 : 0 |
| 19 | + length = 20 |
| 20 | + special = true |
| 21 | + override_special = "#$%&@!_+=./;:][{}]" |
| 22 | +} |
| 23 | + |
| 24 | +output "grafana_password" { |
| 25 | + value = local.enable_monitoring_stack ? random_password.grafana_password[0].result : "" |
| 26 | + sensitive = true |
| 27 | +} |
| 28 | + |
| 29 | +resource "helm_release" "kube_prometheus_stack" { |
| 30 | + count = local.enable_monitoring_stack ? 1 : 0 |
| 31 | + name = local.deployment_name |
| 32 | + repository = "https://prometheus-community.github.io/helm-charts" |
| 33 | + chart = "kube-prometheus-stack" |
| 34 | + namespace = "monitoring" |
| 35 | + version = "45.8.0" |
| 36 | + wait = false |
| 37 | + create_namespace = true |
| 38 | + |
| 39 | + set { |
| 40 | + name = "prometheus.prometheusSpec.additionalScrapeConfigs" |
| 41 | + value = join("\n", local.scrape_configs) |
| 42 | + } |
| 43 | + |
| 44 | + # set { |
| 45 | + # name = "grafana.sidecar.datasources.defaultDatasourceEnabled" |
| 46 | + # value = false |
| 47 | + # } |
| 48 | + |
| 49 | + set { |
| 50 | + name = "grafana.adminPassword" |
| 51 | + value = random_password.grafana_password[0].result |
| 52 | + } |
| 53 | + |
| 54 | + values = [ |
| 55 | + yamlencode({ "grafana" = { |
| 56 | + "additionalDataSources" = local.grafana_datasources, |
| 57 | + "plugins" = yamldecode(local.grafana_plugins) |
| 58 | + } }) |
| 59 | + ] |
| 60 | + |
| 61 | + depends_on = [ |
| 62 | + oci_containerengine_node_pool.oci_oke_node_pool |
| 63 | + ] |
| 64 | +} |
| 65 | + |
| 66 | +resource "kubernetes_config_map_v1" "grafana_dashboards" { |
| 67 | + count = local.enable_monitoring_stack ? length(local.grafana_dashboards) : 0 |
| 68 | + |
| 69 | + metadata { |
| 70 | + name = "${local.deployment_name}-grafana-${local.grafana_dashboards[count.index].label}" |
| 71 | + namespace = "monitoring" |
| 72 | + labels = { |
| 73 | + "grafana_dashboard" = "1" |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + data = { |
| 78 | + "${local.grafana_dashboards[count.index].name}" = "${file("${path.module}/templates/${local.grafana_dashboards[count.index].name}")}" |
| 79 | + } |
| 80 | + depends_on = [ |
| 81 | + helm_release.kube_prometheus_stack |
| 82 | + ] |
| 83 | +} |
| 84 | + |
| 85 | +resource "kubernetes_config_map_v1" "grafana_plugins" { |
| 86 | + count = local.enable_monitoring_stack ? 1 : 0 |
| 87 | + |
| 88 | + metadata { |
| 89 | + name = "${local.deployment_name}-grafana-plugins" |
| 90 | + namespace = "monitoring" |
| 91 | + labels = { |
| 92 | + "grafana_plugin" = "1" |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + data = { |
| 97 | + "plugins" = local.grafana_plugins |
| 98 | + } |
| 99 | + |
| 100 | + depends_on = [ |
| 101 | + helm_release.kube_prometheus_stack |
| 102 | + ] |
| 103 | +} |
0 commit comments