resource "kubernetes_ingress_v1" "ingress" {
  depends_on = [
    kubernetes_deployment.sftpgo,
  ]
  metadata {
    name      = "webdav-ingress"
    namespace = kubernetes_namespace.sftpgo.metadata[0].name

    annotations = merge({
      "kubernetes.io/ingress.class"                         = "public"
      "cert-manager.io/cluster-issuer"                      = "letsencrypt-prod"
      "kubernetes.io/tls-acme"                              = "true"
      "nginx.ingress.kubernetes.io/ssl-redirect"            = "true"
      "nginx.ingress.kubernetes.io/proxy-body-size"         = "4G"
      "nginx.ingress.kubernetes.io/proxy-request-buffering" = "on"
      "nginx.ingress.kubernetes.io/proxy-buffer-size"       = "32k"
      "nginx.ingress.kubernetes.io/proxy-buffers-number"    = "16"
      },
      var.tls_enabled ? {
        "nginx.org/ssl-services"                       = "webdav"
        "nginx.org/ssl-redirect"                       = "true"
        "nginx.org/redirect-to-https"                  = "true"
        "nginx.ingress.kubernetes.io/backend-protocol" = "HTTPS"
      } : {}
    )

  }
  spec {
    tls {
      hosts       = [var.fqdn]
      secret_name = "webdav-tls"
    }
    rule {
      host = var.fqdn
      http {
        path {
          backend {
            service {
              name = "webdav"
              port {
                name = var.tls_enabled ? "davs" : "dav"
              }
            }
          }
        }
      }
    }
  }
}
