
locals {
  common-labels = {
    "vynil.solidite.fr/owner-name" = var.instance
    "vynil.solidite.fr/owner-namespace" = var.namespace
    "vynil.solidite.fr/owner-category" = var.category
    "vynil.solidite.fr/owner-component" = var.component
    "app.kubernetes.io/managed-by" = "vynil"
    "app.kubernetes.io/name" = var.component
    "app.kubernetes.io/instance" = var.instance
  }
}

data "kustomization_overlay" "data" {
  common_labels = local.common-labels
  resources = [for file in fileset(path.module, "*.yaml"): file if file != "index.yaml"]
  images {
    name = "quay.io/kubevirt/virt-handler"
    new_name = "${var.images.handler.registry}/${var.images.handler.repository}"
    new_tag = "${var.images.handler.tag}"
  }
  images {
    name = "quay.io/kubevirt/virt-api"
    new_name = "${var.images.api.registry}/${var.images.api.repository}"
    new_tag = "${var.images.api.tag}"
  }
  images {
    name = "quay.io/kubevirt/virt-controller"
    new_name = "${var.images.controller.registry}/${var.images.controller.repository}"
    new_tag = "${var.images.controller.tag}"
  }
  patches {
    target {
      kind = "Deployment"
      name = "virt-controller"
    }
    patch = <<-EOF
    - op: replace
      path: /spec/template/spec/containers/0/imagePullPolicy
      value: "${var.images.controller.pull_policy}"
    - op: replace
      path: /spec/template/spec/containers/0/image
      value: "${var.images.controller.registry}/${var.images.controller.repository}:${var.images.controller.tag}"
    - op: replace
      path: /spec/template/spec/containers/0/args/1
      value: "${var.images.launcher.registry}/${var.images.launcher.repository}:${var.images.launcher.tag}"
    - op: replace
      path: /spec/template/spec/containers/0/args/3
      value: "${var.images.exportserver.registry}/${var.images.exportserver.repository}:${var.images.exportserver.tag}"
    EOF
  }
}
