From 425bde9636374edbf9d9c84e8cbba40a0f44506f Mon Sep 17 00:00:00 2001 From: Anthony Zawacki Date: Tue, 12 Sep 2023 14:39:29 -0400 Subject: [PATCH] Added create_namespace to make it possible to install prometheus into an existing namespace. --- main.tf | 19 ++++++++++++++++++- variables.tf | 6 ++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/main.tf b/main.tf index e9ffe29..0146d40 100644 --- a/main.tf +++ b/main.tf @@ -1,14 +1,31 @@ resource "kubernetes_namespace" "ns" { + count = var.create_namespace == true ? 1 : 0 + metadata { name = var.namespace + labels = { + istio-injection = "enabled" + } } } +data "kubernetes_namespace" "existing-ns" { + count = var.create_namespace == true ? 0 : 1 + + metadata { + name = var.namespace + } +} + +locals { + ns = try(kubernetes_namespace.ns[0].metadata[0].name, data.kubernetes_namespace.existing-ns[0].metadata[0].name) +} + resource "helm_release" "prometheus" { chart = "prometheus" name = "prometheus" - namespace = kubernetes_namespace.ns.metadata[0].name + namespace = local.ns version = var.prometheus_chart_version repository = "https://prometheus-community.github.io/helm-charts" diff --git a/variables.tf b/variables.tf index cd6e568..b6e7a9e 100644 --- a/variables.tf +++ b/variables.tf @@ -19,6 +19,12 @@ variable "namespace" { default = "prometheus" } +variable "create_namespace" { + description = "Indicates whether the `namespace` needs to be created ('true') or already exists (not `true`)" + type = bool + default = true +} + # helm repo add prometheus-community https://prometheus-community.github.io/helm-charts # helm search repo prometheus-community/prometheus | head -2 variable "prometheus_chart_version" {