resource "kubernetes_service" "nextcloud_redis" {
  metadata {
    name      = "redis"
    namespace = kubernetes_namespace.nextcloud.metadata[0].name
  }
  spec {
    port {
      port = 6379
      name = "redis"
    }
    selector = {
      app = "redis"
    }

    type       = "ClusterIP"
    cluster_ip = "None"
  }
}

resource "kubernetes_deployment" "nextcloud_redis" {
  metadata {
    name      = "redis"
    namespace = kubernetes_namespace.nextcloud.metadata[0].name
  }
  spec {
    selector {
      match_labels = {
        app = "redis"
      }
    }
    strategy {
      type = "RollingUpdate"
    }
    template {
      metadata {
        labels = {
          app = "redis"
        }
      }
      spec {
        container {
          name  = "redis"
          image = "redis:latest"

          security_context {
            allow_privilege_escalation = false
          }

          readiness_probe {
            tcp_socket {
              port = "redis"
            }
            initial_delay_seconds = 10
            period_seconds        = 15
            success_threshold     = 1
            failure_threshold     = 3
          }

          liveness_probe {
            tcp_socket {
              port = "redis"
            }
            initial_delay_seconds = 10
            period_seconds        = 15
            success_threshold     = 1
            failure_threshold     = 3
          }

          port {
            container_port = 6379
            name           = "redis"
            protocol       = "TCP"
          }
        }
      }
    }
  }
}
