locals {
    annotations = {
      "vynil.solidite.fr/meta" = "core"
      "vynil.solidite.fr/name" = var.namespace
    }
    crd-argo-cd = { for k, v in var.crds.argo-cd : k => v if k!="enable" }
    crd-argo-events = { for k, v in var.crds.argo-events : k => v if k!="enable" }
    crd-argo-workflows = { for k, v in var.crds.argo-workflows : k => v if k!="enable" }
    crd-fission = { for k, v in var.crds.fission : k => v if k!="enable" }
    crd-keda = { for k, v in var.crds.keda : k => v if k!="enable" }
    crd-minio = { for k, v in var.crds.minio : k => v if k!="enable" }
    crd-olm = { for k, v in var.crds.olm : k => v if k!="enable" }
    crd-whereabouts = { for k, v in var.crds.whereabouts : k => v if k!="enable" }
    crd-volumesnapshot = { for k, v in var.crds.volumesnapshot : k => v if k!="enable" }
    crd-opentelemetry = { for k, v in var.crds.opentelemetry : k => v if k!="enable" }
    crd-jaeger = { for k, v in var.crds.jaeger : k => v if k!="enable" }
    crd-prometheus = { for k, v in var.crds.prometheus : k => v if k!="enable" }
    crd-flux = { for k, v in var.crds.flux : k => v if k!="enable" }
    crd-tekton_pipelines = { for k, v in var.crds.tekton_pipelines : k => v if k!="enable" }
    crd-tekton_triggers = { for k, v in var.crds.tekton_triggers : k => v if k!="enable" }
}

resource "kubectl_manifest" "crd-tekton_pipelines" {
  count = (var.crds.tekton_pipelines.enable || var.tekton.tekton_pipelines.enable) ? 1 : 0
  yaml_body  = <<-EOF
    apiVersion: "vynil.solidite.fr/v1"
    kind: "Install"
    metadata:
      name: "crd-tekton-pipelines"
      namespace: "${var.namespace}"
      labels: ${jsonencode(local.common-labels)}
    spec:
      distrib: "${var.component}"
      category: "crd"
      component: "tekton-pipelines"
      options: ${jsonencode(local.crd-tekton_pipelines)}
  EOF
}

resource "kubectl_manifest" "crd-tekton_triggers" {
  count = (var.crds.tekton_triggers.enable || var.tekton.tekton_triggers.enable) ? 1 : 0
  yaml_body  = <<-EOF
    apiVersion: "vynil.solidite.fr/v1"
    kind: "Install"
    metadata:
      name: "crd-tekton-triggers"
      namespace: "${var.namespace}"
      labels: ${jsonencode(local.common-labels)}
    spec:
      distrib: "${var.component}"
      category: "crd"
      component: "tekton-triggers"
      options: ${jsonencode(local.crd-tekton_triggers)}
  EOF
}

resource "kubectl_manifest" "crd-jaeger" {
  count = (var.crds.jaeger.enable || var.monitor.jaeger.enable) ? 1 : 0
  yaml_body  = <<-EOF
    apiVersion: "vynil.solidite.fr/v1"
    kind: "Install"
    metadata:
      name: "crd-jaeger"
      namespace: "${var.namespace}"
      labels: ${jsonencode(local.common-labels)}
    spec:
      distrib: "${var.component}"
      category: "crd"
      component: "jaeger"
      options: ${jsonencode(local.crd-jaeger)}
  EOF
}

resource "kubectl_manifest" "crd-opentelemetry" {
  count = (var.crds.opentelemetry.enable || var.monitor.opentelemetry.enable) ? 1 : 0
  yaml_body  = <<-EOF
    apiVersion: "vynil.solidite.fr/v1"
    kind: "Install"
    metadata:
      name: "crd-opentelemetry"
      namespace: "${var.namespace}"
      labels: ${jsonencode(local.common-labels)}
    spec:
      distrib: "${var.component}"
      category: "crd"
      component: "opentelemetry"
      options: ${jsonencode(local.crd-opentelemetry)}
  EOF
}

resource "kubectl_manifest" "crd-prometheus" {
  count = (var.crds.prometheus.enable || var.monitor.prometheus.enable) ? 1 : 0
  yaml_body  = <<-EOF
    apiVersion: "vynil.solidite.fr/v1"
    kind: "Install"
    metadata:
      name: "crd-prometheus"
      namespace: "${var.namespace}"
      labels: ${jsonencode(local.common-labels)}
    spec:
      distrib: "${var.component}"
      category: "crd"
      component: "prometheus"
      options: ${jsonencode(local.crd-prometheus)}
  EOF
}

resource "kubectl_manifest" "crd-flux" {
  count = (var.crds.flux.enable || var.flux.enable) ? 1 : 0
  yaml_body  = <<-EOF
    apiVersion: "vynil.solidite.fr/v1"
    kind: "Install"
    metadata:
      name: "crd-flux"
      namespace: "${var.namespace}"
      labels: ${jsonencode(local.common-labels)}
    spec:
      distrib: "${var.component}"
      category: "crd"
      component: "flux"
      options: ${jsonencode(local.crd-flux)}
  EOF
}

resource "kubectl_manifest" "crd-olm" {
  count = (var.crds.olm.enable || var.olm.enable) ? 1 : 0
  yaml_body  = <<-EOF
    apiVersion: "vynil.solidite.fr/v1"
    kind: "Install"
    metadata:
      name: "crd-olm"
      namespace: "${var.namespace}"
      labels: ${jsonencode(local.common-labels)}
    spec:
      distrib: "${var.component}"
      category: "crd"
      component: "olm"
      options: ${jsonencode(local.crd-olm)}
  EOF
}

resource "kubectl_manifest" "crd-keda" {
  count = (var.crds.keda.enable) ? 1 : 0
  yaml_body  = <<-EOF
    apiVersion: "vynil.solidite.fr/v1"
    kind: "Install"
    metadata:
      name: "crd-keda"
      namespace: "${var.namespace}"
      labels: ${jsonencode(local.common-labels)}
    spec:
      distrib: "${var.component}"
      category: "crd"
      component: "keda"
      options: ${jsonencode(local.crd-keda)}
  EOF
}

resource "kubectl_manifest" "crd-fission" {
  count = (var.crds.fission.enable) ? 1 : 0
  yaml_body  = <<-EOF
    apiVersion: "vynil.solidite.fr/v1"
    kind: "Install"
    metadata:
      name: "crd-fission"
      namespace: "${var.namespace}"
      labels: ${jsonencode(local.common-labels)}
    spec:
      distrib: "${var.component}"
      category: "crd"
      component: "fission"
      options: ${jsonencode(local.crd-fission)}
  EOF
}

resource "kubectl_manifest" "crd-minio" {
  count = (var.crds.minio.enable) ? 1 : 0
  yaml_body  = <<-EOF
    apiVersion: "vynil.solidite.fr/v1"
    kind: "Install"
    metadata:
      name: "crd-minio"
      namespace: "${var.namespace}"
      labels: ${jsonencode(local.common-labels)}
    spec:
      distrib: "${var.component}"
      category: "crd"
      component: "minio"
      options: ${jsonencode(local.crd-minio)}
  EOF
}

resource "kubectl_manifest" "crd-argo-cd" {
  count = (var.crds.argo-cd.enable) ? 1 : 0
  yaml_body  = <<-EOF
    apiVersion: "vynil.solidite.fr/v1"
    kind: "Install"
    metadata:
      name: "crd-argo-cd"
      namespace: "${var.namespace}"
      labels: ${jsonencode(local.common-labels)}
    spec:
      distrib: "${var.component}"
      category: "crd"
      component: "argo-cd"
      options: ${jsonencode(local.crd-argo-cd)}
  EOF
}

resource "kubectl_manifest" "crd-argo-events" {
  count = (var.crds.argo-events.enable) ? 1 : 0
  yaml_body  = <<-EOF
    apiVersion: "vynil.solidite.fr/v1"
    kind: "Install"
    metadata:
      name: "crd-argo-events"
      namespace: "${var.namespace}"
      labels: ${jsonencode(local.common-labels)}
    spec:
      distrib: "${var.component}"
      category: "crd"
      component: "argo-events"
      options: ${jsonencode(local.crd-argo-events)}
  EOF
}

resource "kubectl_manifest" "crd-argo-workflows" {
  count = (var.crds.argo-workflows.enable) ? 1 : 0
  yaml_body  = <<-EOF
    apiVersion: "vynil.solidite.fr/v1"
    kind: "Install"
    metadata:
      name: "crd-argo-workflows"
      namespace: "${var.namespace}"
      labels: ${jsonencode(local.common-labels)}
    spec:
      distrib: "${var.component}"
      category: "crd"
      component: "argo-workflows"
      options: ${jsonencode(local.crd-argo-workflows)}
  EOF
}

resource "kubectl_manifest" "crd-whereabouts" {
  count = (var.crds.whereabouts.enable ) ? 1 : 0
  yaml_body  = <<-EOF
    apiVersion: "vynil.solidite.fr/v1"
    kind: "Install"
    metadata:
      name: "crd-whereabouts"
      namespace: "${var.namespace}"
      labels: ${jsonencode(local.common-labels)}
    spec:
      distrib: "${var.component}"
      category: "crd"
      component: "whereabouts"
      options: ${jsonencode(local.crd-whereabouts)}
  EOF
}

resource "kubectl_manifest" "crd-volumesnapshot" {
  count = (var.crds.volumesnapshot.enable) ? 1 : 0
  yaml_body  = <<-EOF
    apiVersion: "vynil.solidite.fr/v1"
    kind: "Install"
    metadata:
      name: "crd-volumesnapshot"
      namespace: "${var.namespace}"
      labels: ${jsonencode(local.common-labels)}
    spec:
      distrib: "${var.component}"
      category: "crd"
      component: "volumesnapshot"
      options: ${jsonencode(local.crd-volumesnapshot)}
  EOF
}
