locals {
    tekton_pipelines = { for k, v in var.tekton.tekton_pipelines : k => v if k!="enable" }
    tekton_triggers = { for k, v in var.tekton.tekton_triggers : k => v if k!="enable" }
}

resource "kubernetes_namespace_v1" "tekton-ns" {
  count = var.tekton.enable && ( var.tekton.tekton_pipelines.enable || var.tekton.tekton_triggers.enable)? 1 : 0
  metadata {
    annotations = local.annotations
    labels = local.common-labels
    name = var.tekton.namespace
  }
}

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

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