resource "kubernetes_service_account" "influxdb_service_account" {

  metadata {
    name      = "influxdb-sa"
    namespace = kubernetes_namespace.monitoring.metadata.0.name
  }

  automount_service_account_token = false
}

resource "kubernetes_config_map" "influxdb_init" {

  metadata {
    name      = "influxdb-init"
    namespace = kubernetes_namespace.monitoring.metadata.0.name
  }

  data = {
    "init.iql" = file("./resources/influxdb/init.iql")
  }
}

resource "kubernetes_service" "influxdb" {
  metadata {
    name      = "influxdb"
    namespace = kubernetes_namespace.monitoring.metadata.0.name
  }
  spec {
    port {
      port        = 8086
      name        = "api"
      target_port = "api"
    }
    port {
      port        = 8088
      name        = "rpc"
      target_port = "rpc"
    }
    selector = {
      app = "influxdb"
    }
    cluster_ip = "None"
  }
}

resource "kubernetes_stateful_set" "influxdb" {
  metadata {
    name      = "influxdb"
    namespace = kubernetes_namespace.monitoring.metadata.0.name
  }

  spec {
    selector {
      match_labels = {
        app = "influxdb"
      }
    }
    replicas     = 1
    service_name = "influxdb"
    template {
      metadata {
        labels = {
          app = "influxdb"
        }
      }
      spec {
        service_account_name = kubernetes_service_account.influxdb_service_account.metadata.0.name
        security_context {
          fs_group = 1000
        }
        container {
          name              = "influxdb"
          image             = "influxdb:${local.influxdb_version}"
          image_pull_policy = "IfNotPresent"

          resources {
            requests = {
              memory = "100Mi"
            }
          }

          port {
            name           = "api"
            container_port = "8086"
          }

          port {
            name           = "rpc"
            container_port = "8088"
          }

          liveness_probe {
            http_get {
              path   = "/ping"
              port   = "api"
              scheme = "HTTP"
            }
            initial_delay_seconds = 30
            timeout_seconds       = 5
          }

          readiness_probe {
            http_get {
              path   = "/ping"
              port   = "api"
              scheme = "HTTP"
            }
            initial_delay_seconds = 30
            timeout_seconds       = 5
          }

          security_context {
            run_as_user                = 1000
            run_as_group               = 1000
            allow_privilege_escalation = false
          }

          volume_mount {
            name       = "persistent-storage"
            mount_path = "/var/lib/influxdb"
          }

          volume_mount {
            name       = "init-db"
            mount_path = "/docker-entrypoint-initdb.d"
          }
        }

        volume {
          name = "persistent-storage"
          host_path {
            path = "/var/lib/monitoring/influxdb.data"
          }
        }
        volume {
          name = "init-db"
          config_map {
            name = kubernetes_config_map.influxdb_init.metadata.0.name
          }
        }
      }
    }
  }
  lifecycle {
    replace_triggered_by = [
      kubernetes_config_map.influxdb_init
    ]
  }
}
