resource "kubernetes_cluster_role" "telegraf" {

  metadata {
    name = "telegraf-role"
    labels = {
      "rbac.authorization.k8s.io/aggregate-view-telegraf-stats" = "true"
    }
  }

  rule {
    api_groups = ["metrics.k8s.io"]
    resources  = ["pods"]
    verbs      = ["get", "watch", "list"]
  }

  rule {
    api_groups = [""]
    resources = [
      "nodes/log",
      "nodes/metrics",
      "nodes/proxy",
      "nodes/spec",
      "nodes/stats",
    ]
    verbs = ["*"]
  }

  rule {
    api_groups = ["", "apps"]
    resources  = ["pods", "endpoints", "deployments", "statefulsets", "daemonsets", "persistentvolumeclaims", "services", "persistentvolumes", "nodes"]
    verbs      = ["get", "watch", "list"]
  }

  rule {
    api_groups = ["networking.k8s.io"]
    resources  = ["ingresses"]
    verbs      = ["get", "watch", "list"]
  }
}

resource "kubernetes_cluster_role_binding" "telegraf" {

  metadata {
    name = "telegraf-role-binding"
  }

  role_ref {
    api_group = "rbac.authorization.k8s.io"
    kind      = "ClusterRole"
    name      = kubernetes_cluster_role.telegraf.metadata.0.name
  }

  subject {
    kind      = "ServiceAccount"
    name      = kubernetes_service_account.telegraf.metadata.0.name
    namespace = kubernetes_namespace.monitoring.metadata.0.name
  }
}

resource "kubernetes_service_account" "telegraf" {
  metadata {
    name      = "telegraf-sa"
    namespace = kubernetes_namespace.monitoring.metadata.0.name
  }

  automount_service_account_token = false
}

resource "kubernetes_config_map" "telegraf" {
  metadata {
    name      = "telegraf-config"
    namespace = kubernetes_namespace.monitoring.metadata.0.name
  }

  data = {
    "telegraf.conf" = file("./resources/telegraf/telegraf.conf")
  }
}

resource "kubernetes_daemonset" "telegraf" {

  depends_on = [
    kubernetes_stateful_set.influxdb
  ]

  metadata {
    name      = "telegraf"
    namespace = kubernetes_namespace.monitoring.metadata.0.name
  }

  spec {
    selector {
      match_labels = {
        app = "telegraf"
      }
    }
    strategy {
      type = "RollingUpdate"
    }
    template {
      metadata {
        labels = {
          app = "telegraf"
        }
      }
      spec {
        service_account_name            = kubernetes_service_account.telegraf.metadata.0.name
        automount_service_account_token = true
        container {
          name              = "telegraf"
          image             = "telegraf:${local.telegraf_version}"
          image_pull_policy = "IfNotPresent"

          resources {
            requests = {
              memory = "100Mi"
            }
          }
          env {
            name  = "HOST_PROC"
            value = "/hostfs/proc"
          }
          env {
            name  = "HOST_SYS"
            value = "/hostfs/sys"
          }

          env {
            name  = "HOST_MOUNT_PREFIX"
            value = "/hostfs"
          }
          volume_mount {
            name       = "hostfsro"
            mount_path = "/hostfs"
            read_only  = true
          }
          volume_mount {
            mount_path = "/etc/telegraf"
            name       = "config"
            read_only  = true
          }
        }
        volume {
          name = "hostfsro"
          host_path {
            path = "/"
          }
        }
        volume {
          name = "config"
          config_map {
            name = kubernetes_config_map.telegraf.metadata.0.name
          }
        }
      }
    }
  }
  lifecycle {
    replace_triggered_by = [
      kubernetes_config_map.telegraf
    ]
  }
}
