resource "kubernetes_namespace" "gitea" {
  metadata {
    annotations = {
      name = "gitea"
    }

    labels = {
      name = "gitea"
    }

    name = "gitea"
  }
}

resource "kubernetes_manifest" "internal_cert" {
  count = var.tls_enabled ? 1 : 0
  manifest = {
    apiVersion = "cert-manager.io/v1"
    kind       = "Certificate"
    metadata = {
      name      = "internal-cert"
      namespace = kubernetes_namespace.gitea.metadata[0].name
    }
    spec = {
      secretName = "internal-cert"
      issuerRef = {
        name = var.issuer
        kind = "ClusterIssuer"
      }
      commonName = var.fqdn
      dnsNames   = [var.fqdn]
    }
  }
}

resource "kubernetes_config_map" "gitea_config" {
  metadata {
    name      = "gitea-config"
    namespace = kubernetes_namespace.gitea.metadata[0].name
  }
  data = {
    "app.ini" = local.config
  }
}


resource "kubernetes_service_account" "gitea_service_account" {

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

  automount_service_account_token = false
}

resource "kubernetes_deployment" "gitea" {

  depends_on = [module.gitea_database]

  metadata {
    name      = "gitea-app"
    namespace = kubernetes_namespace.gitea.metadata[0].name
  }
  spec {
    selector {
      match_labels = {
        app = "gitea"
      }
    }
    strategy {
      type = "Recreate"
    }
    template {
      metadata {
        labels = {
          app = "gitea"
        }
        annotations = {
          "checksum/config" = local.config_checksum
        }
      }
      spec {
        service_account_name = kubernetes_service_account.gitea_service_account.metadata[0].name
        container {
          name              = "gitea"
          image             = "gitea/gitea:${var.tag}"
          image_pull_policy = "Always"

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

          liveness_probe {
            http_get {
              path   = "/api/healthz"
              port   = "http"
              scheme = var.tls_enabled ? "HTTPS" : "HTTP"
            }
            initial_delay_seconds = 200
            period_seconds        = 300
            success_threshold     = 1
            failure_threshold     = 5
          }

          readiness_probe {
            tcp_socket {
              port = "http"
            }
            initial_delay_seconds = 5
            period_seconds        = 10
            success_threshold     = 1
            failure_threshold     = 3
          }

          security_context {
            allow_privilege_escalation = false
          }

          port {
            container_port = 3000
            name           = "http"
          }
          port {
            container_port = 22
            name           = "ssh"
          }

          volume_mount {
            mount_path = "/data"
            name       = "persistent-storage"
          }
          volume_mount {
            mount_path = "/data/gitea/conf/app.ini"
            name       = "gitea-config"
            sub_path   = "app.ini"
          }
          dynamic "volume_mount" {
            for_each = var.tls_enabled ? [1] : []
            content {
              mount_path = "/etc/ssl/certs/private"
              name       = "internal-cert"
              read_only  = true
            }
          }
        }
        volume {
          name = "persistent-storage"
          host_path {
            path = local.gitea_path
          }
        }
        volume {
          name = "gitea-config"
          config_map {
            name = kubernetes_config_map.gitea_config.metadata[0].name
          }
        }
        dynamic "volume" {
          for_each = var.tls_enabled ? [1] : []
          content {
            name = "internal-cert"
            secret {
              secret_name = kubernetes_manifest.internal_cert[0].manifest["metadata"]["name"]
              items {
                key  = "tls.crt"
                path = "tls.crt"
              }
              items {
                key  = "tls.key"
                path = "tls.key"
              }
            }
          }
        }
      }
    }
  }

  lifecycle {
    ignore_changes = [spec[0].template[0].metadata[0].annotations["kubectl.kubernetes.io/restartedAt"]]
  }
}

