resource "helm_release" "zot" {
  name             = "zot"
  repository       = "https://zotregistry.dev/helm-charts"
  chart            = "zot"
  namespace        = "registry"
  create_namespace = true

  values = [
    templatefile("${path.module}/values.yaml.tftpl", { service_uri = var.service_uri })
  ]
}

resource "kubernetes_manifest" "traefik_middleware_request_body" {
  depends_on = [helm_release.zot]
  manifest = {
    apiVersion = "traefik.io/v1alpha1"
    kind       = "Middleware"
    metadata = {
      name      = "request-body"
      namespace = "registry"
    }
    spec = {
      buffering = {
        maxRequestBodyBytes = 0
      }
    }
  }
}

resource "kubernetes_manifest" "traefik_middleware_request_timeouts" {
  depends_on = [helm_release.zot]
  manifest = {
    apiVersion = "traefik.io/v1alpha1"
    kind       = "Middleware"
    metadata = {
      name      = "request-timeouts"
      namespace = "registry"
    }
    spec = {
      headers = {
        customRequestHeaders = {
          "X-Forwarded-Timeout-Read"  = "3600s"
          "X-Forwarded-Timeout-Write" = "3600s"
        }
      }
    }
  }
}

output "installed" {
  value = true
  depends_on = [
    kubernetes_manifest.traefik_middleware_request_body, kubernetes_manifest.traefik_middleware_request_timeouts,
    helm_release.zot
  ]
}
