resource "kubernetes_cron_job_v1" "nextcloud_cron_apache" {
  depends_on = [
    kubernetes_deployment.nextcloud_apache
  ]
  metadata {
    namespace = kubernetes_namespace.nextcloud.metadata[0].name
    name      = "cronjob-trigger"
  }
  spec {
    schedule                      = "*/15 * * * *"
    concurrency_policy            = "Forbid"
    failed_jobs_history_limit     = 2
    successful_jobs_history_limit = 2
    job_template {
      metadata {
        labels = {
          app = "cronjob-trigger"
        }
      }
      spec {
        backoff_limit           = 1
        parallelism             = 1
        completions             = 1
        active_deadline_seconds = 300
        template {
          metadata {
            labels = {
              app = "cronjob-trigger"
            }
          }
          spec {
            container {
              name  = "nextcloud"
              image = "nextcloud:${var.tag}"

              command = ["curl"]
              args    = ["--fail", "-L", "http://http/cron.php"]
              security_context {
                allow_privilege_escalation = false
              }
            }
            restart_policy                   = "Never"
            termination_grace_period_seconds = 30
          }
        }
      }
    }
  }
  lifecycle {
    replace_triggered_by = [
      kubernetes_deployment.nextcloud_apache
    ]
  }
}
