resource "kubernetes_namespace" "redis" {
  metadata {
    name = var.namespace
  }

  lifecycle {
    ignore_changes = [metadata]
  }
}

resource "random_password" "redis" {
  length           = 32
  special          = true
  override_special = "_%@"
}

resource "kubernetes_secret" "redis" {
  metadata {
    name      = "redis"
    namespace = kubernetes_namespace.redis.metadata.0.name
  }

  data = {
    "redis-password" = random_password.redis.result
  }

  type = "Opaque"
}

resource "helm_release" "redis" {
  depends_on = [var.wait_on]
  name             = "redis"
  repository       = "https://charts.bitnami.com/bitnami"
  chart            = "redis"
  namespace        = kubernetes_namespace.redis.metadata.0.name
  version          = "20.1.7"
  create_namespace = true
  wait             = true
  wait_for_jobs    = true

  values = [
    templatefile("${path.module}/values.yaml", {
      architecture       = var.architecture,
      authExistingSecret = "redis"
    })
  ]
}

output "installed" {
  value = "true"
  depends_on = [helm_release.redis]
}

output "password" {
  value     = random_password.redis.result
  sensitive = true
}
