#
# Namespace
#
resource "kubernetes_namespace" "collabora" {
  metadata {
    annotations = {
      name = "collabora"
    }

    labels = {
      name = "collabora"
    }

    name = "collabora"
  }
}

resource "kubernetes_secret" "collabora_admin_credentials" {
  metadata {
    name      = "collabora-admin-credentials"
    namespace = kubernetes_namespace.collabora.metadata[0].name
  }
  data = {
    username = "admin"
    password = var.collabora_admin_password
  }
  type = "Opaque"
}

resource "kubernetes_config_map" "collabora" {

  metadata {
    name      = "collabora-config"
    namespace = kubernetes_namespace.collabora.metadata[0].name
  }
  data = {
    "extra_params" = "-o:ssl.enable=true"
    "aliasgroup1"  = "https://${local.nextcloud_fqdn}"
  }
}

resource "kubernetes_service_account" "collabora_service_account" {

  metadata {
    name      = "collabora-app-sa"
    namespace = kubernetes_namespace.collabora.metadata[0].name
  }

  automount_service_account_token = false
}


resource "kubernetes_service" "collabora_office" {
  metadata {
    name      = "http"
    namespace = kubernetes_namespace.collabora.metadata[0].name
  }
  spec {
    port {
      port        = 80
      target_port = "http"
    }
    selector = {
      app = "collabora"
    }
    cluster_ip = "None"
  }
}

resource "kubernetes_ingress_v1" "collabora_ingress" {
  depends_on = [
    kubernetes_stateful_set.collabora_office
  ]
  metadata {
    name      = "collabora-ingress"
    namespace = kubernetes_namespace.collabora.metadata[0].name

    annotations = {
      "kubernetes.io/ingress.class"                  = "public"
      "cert-manager.io/cluster-issuer"               = "letsencrypt-prod"
      "kubernetes.io/tls-acme"                       = "true"
      "nginx.ingress.kubernetes.io/backend-protocol" = "HTTPS"
    }
  }
  spec {
    tls {
      hosts       = [local.collabora_fqdn]
      secret_name = "collabora-tls"
    }
    rule {
      host = local.collabora_fqdn
      http {
        path {
          backend {
            service {
              name = "http"
              port {
                number = 80
              }
            }
          }
        }
      }
    }
  }
}


resource "kubernetes_stateful_set" "collabora_office" {

  metadata {
    name      = "collabora"
    namespace = kubernetes_namespace.collabora.metadata[0].name
  }

  spec {
    service_name = "collabora"
    selector {
      match_labels = {
        app = "collabora"
      }
    }
    template {
      metadata {
        annotations = {
          "cluster-autoscaler.kubernetes.io/safe-to-evict" = "true"
        }
        labels = {
          app = "collabora"
        }
      }
      spec {
        termination_grace_period_seconds = 60
        service_account_name             = kubernetes_service_account.collabora_service_account.metadata[0].name
        container {
          name              = "collabora"
          image             = "collabora/code:${local.collabora_version}"
          image_pull_policy = "IfNotPresent"
          port {
            name           = "http"
            container_port = 9980
            protocol       = "TCP"
          }
          startup_probe {
            tcp_socket {
              # path   = "/"
              port = 9980
              # scheme = "HTTP"
            }
            failure_threshold = 30
            period_seconds    = 3
          }
          liveness_probe {
            tcp_socket {
              # path   = "/"
              port = 9980
              # scheme = "HTTP"
            }
            initial_delay_seconds = 0
            period_seconds        = 30
            timeout_seconds       = 30
            success_threshold     = 1
            failure_threshold     = 4
          }
          readiness_probe {
            tcp_socket {
              # path   = "/"
              port = 9980
              # scheme = "HTTP"
            }
            initial_delay_seconds = 0
            period_seconds        = 30
            timeout_seconds       = 30
            success_threshold     = 1
            failure_threshold     = 2
          }
          env_from {
            config_map_ref {
              name = kubernetes_config_map.collabora.metadata[0].name
            }
          }
          env {
            name = "username"
            value_from {
              secret_key_ref {
                name = kubernetes_secret.collabora_admin_credentials.metadata[0].name
                key  = "username"
              }
            }
          }
          env {
            name = "password"
            value_from {
              secret_key_ref {
                name = kubernetes_secret.collabora_admin_credentials.metadata[0].name
                key  = "password"
              }
            }
          }
          volume_mount {
            name       = "tmp"
            mount_path = "/tmp"
          }
        }
        volume {
          name = "tmp"
          empty_dir {
          }
        }
      }
    }
  }
}
