{{- $cozyConfig := lookup "v1" "ConfigMap" "cozy-system" "cozystack" }}
{{- $clusterDomain := (index $cozyConfig.data "cluster-domain") | default "cozy.local" }}
{{- $oidcEnabled := index $cozyConfig.data "oidc-enabled" }}
{{- $host := index $cozyConfig.data "root-host" }}
{{- if not $host }}
{{- fail "ERROR need root-host in cozystack ConfigMap" }}
{{- end }}
{{- $apiServerEndpoint := index $cozyConfig.data "api-server-endpoint" }}
{{- if not $apiServerEndpoint }}
{{- fail "ERROR need api-server-endpoint in cozystack ConfigMap" }}
{{- end }}

releases:
- name: fluxcd-operator
  releaseName: fluxcd-operator
  chart: cozy-fluxcd-operator
  namespace: cozy-fluxcd
  privileged: true
  dependsOn: []

- name: fluxcd
  releaseName: fluxcd
  chart: cozy-fluxcd
  namespace: cozy-fluxcd
  dependsOn: [fluxcd-operator,cilium,kubeovn]
  values:
    flux-instance:
      instance:
        cluster:
          domain: {{ $clusterDomain }}

- name: cilium
  releaseName: cilium
  chart: cozy-cilium
  namespace: cozy-cilium
  privileged: true
  dependsOn: []
  valuesFiles:
  - values.yaml
  - values-talos.yaml
  - values-kubeovn.yaml

- name: cilium-networkpolicy
  releaseName: cilium-networkpolicy
  chart: cozy-cilium-networkpolicy
  namespace: cozy-cilium
  privileged: true
  dependsOn: [cilium]

- name: kubeovn
  releaseName: kubeovn
  chart: cozy-kubeovn
  namespace: cozy-kubeovn
  privileged: true
  dependsOn: [cilium]
  values:
    cozystack:
      nodesHash: {{ include "cozystack.master-node-ips" . | sha256sum }}
    kube-ovn:
      ipv4:
        POD_CIDR: "{{ index $cozyConfig.data "ipv4-pod-cidr" }}"
        POD_GATEWAY: "{{ index $cozyConfig.data "ipv4-pod-gateway" }}"
        SVC_CIDR: "{{ index $cozyConfig.data "ipv4-svc-cidr" }}"
        JOIN_CIDR: "{{ index $cozyConfig.data "ipv4-join-cidr" }}"

- name: kubeovn-webhook
  releaseName: kubeovn-webhook
  chart: cozy-kubeovn-webhook
  namespace: cozy-kubeovn
  privileged: true
  dependsOn: [cilium,kubeovn,cert-manager]

- name: cozy-proxy
  releaseName: cozystack
  chart: cozy-cozy-proxy
  namespace: cozy-system
  dependsOn: [cilium,kubeovn]

- name: cert-manager-crds
  releaseName: cert-manager-crds
  chart: cozy-cert-manager-crds
  namespace: cozy-cert-manager
  dependsOn: [cilium, kubeovn]

- name: cozystack-api
  releaseName: cozystack-api
  chart: cozy-cozystack-api
  namespace: cozy-system
  dependsOn: [cilium,kubeovn]

- name: cozystack-controller
  releaseName: cozystack-controller
  chart: cozy-cozystack-controller
  namespace: cozy-system
  dependsOn: [cilium,kubeovn]
  {{- if eq (index $cozyConfig.data "telemetry-enabled") "false" }}
  values:
    cozystackController:
      disableTelemetry: true
  {{- end }}

- name: cert-manager
  releaseName: cert-manager
  chart: cozy-cert-manager
  namespace: cozy-cert-manager
  dependsOn: [cert-manager-crds]

- name: cert-manager-issuers
  releaseName: cert-manager-issuers
  chart: cozy-cert-manager-issuers
  namespace: cozy-cert-manager
  dependsOn: [cilium,kubeovn,cert-manager]

- name: victoria-metrics-operator
  releaseName: victoria-metrics-operator
  chart: cozy-victoria-metrics-operator
  namespace: cozy-victoria-metrics-operator
  dependsOn: [cilium,kubeovn,cert-manager]

- name: monitoring-agents
  releaseName: monitoring-agents
  chart: cozy-monitoring-agents
  namespace: cozy-monitoring
  privileged: true
  dependsOn: [victoria-metrics-operator, vertical-pod-autoscaler-crds]
  values:
    scrapeRules:
      etcd:
        enabled: true

- name: kubevirt-operator
  releaseName: kubevirt-operator
  chart: cozy-kubevirt-operator
  namespace: cozy-kubevirt
  dependsOn: [cilium,kubeovn,victoria-metrics-operator]

- name: kubevirt
  releaseName: kubevirt
  chart: cozy-kubevirt
  namespace: cozy-kubevirt
  privileged: true
  dependsOn: [cilium,kubeovn,kubevirt-operator]
  {{- $cpuAllocationRatio := index $cozyConfig.data "cpu-allocation-ratio" }}
  {{- if $cpuAllocationRatio }}
  values:
    cpuAllocationRatio: {{ $cpuAllocationRatio }}
  {{- end }}

- name: kubevirt-instancetypes
  releaseName: kubevirt-instancetypes
  chart: cozy-kubevirt-instancetypes
  namespace: cozy-kubevirt
  dependsOn: [cilium,kubeovn,kubevirt-operator,kubevirt]

- name: kubevirt-cdi-operator
  releaseName: kubevirt-cdi-operator
  chart: cozy-kubevirt-cdi-operator
  namespace: cozy-kubevirt-cdi
  dependsOn: [cilium,kubeovn]

- name: kubevirt-cdi
  releaseName: kubevirt-cdi
  chart: cozy-kubevirt-cdi
  namespace: cozy-kubevirt-cdi
  dependsOn: [cilium,kubeovn,kubevirt-cdi-operator]

- name: gpu-operator
  releaseName: gpu-operator
  chart: cozy-gpu-operator
  namespace: cozy-gpu-operator
  privileged: true
  optional: true
  dependsOn: [cilium,kubeovn]
  valuesFiles:
  - values.yaml
  - values-talos.yaml

- name: metallb
  releaseName: metallb
  chart: cozy-metallb
  namespace: cozy-metallb
  privileged: true
  dependsOn: [cilium,kubeovn]

- name: etcd-operator
  releaseName: etcd-operator
  chart: cozy-etcd-operator
  namespace: cozy-etcd-operator
  dependsOn: [cilium,kubeovn,cert-manager]

- name: grafana-operator
  releaseName: grafana-operator
  chart: cozy-grafana-operator
  namespace: cozy-grafana-operator
  dependsOn: [cilium,kubeovn]

- name: mariadb-operator
  releaseName: mariadb-operator
  chart: cozy-mariadb-operator
  namespace: cozy-mariadb-operator
  dependsOn: [cilium,kubeovn,cert-manager,victoria-metrics-operator]
  values:
    mariadb-operator:
      clusterName: {{ $clusterDomain }}

- name: postgres-operator
  releaseName: postgres-operator
  chart: cozy-postgres-operator
  namespace: cozy-postgres-operator
  dependsOn: [cilium,kubeovn,cert-manager]

- name: kafka-operator
  releaseName: kafka-operator
  chart: cozy-kafka-operator
  namespace: cozy-kafka-operator
  dependsOn: [cilium,kubeovn,victoria-metrics-operator]
  values:
    strimzi-kafka-operator:
      kubernetesServiceDnsDomain: {{ $clusterDomain }}

- name: clickhouse-operator
  releaseName: clickhouse-operator
  chart: cozy-clickhouse-operator
  namespace: cozy-clickhouse-operator
  dependsOn: [cilium,kubeovn,victoria-metrics-operator]

- name: rabbitmq-operator
  releaseName: rabbitmq-operator
  chart: cozy-rabbitmq-operator
  namespace: cozy-rabbitmq-operator
  dependsOn: [cilium,kubeovn]

- name: redis-operator
  releaseName: redis-operator
  chart: cozy-redis-operator
  namespace: cozy-redis-operator
  dependsOn: [cilium,kubeovn]

- name: piraeus-operator
  releaseName: piraeus-operator
  chart: cozy-piraeus-operator
  namespace: cozy-linstor
  dependsOn: [cilium,kubeovn,cert-manager,victoria-metrics-operator]

- name: linstor
  releaseName: linstor
  chart: cozy-linstor
  namespace: cozy-linstor
  privileged: true
  dependsOn: [piraeus-operator,cilium,kubeovn,cert-manager,snapshot-controller]

- name: snapshot-controller
  releaseName: snapshot-controller
  chart: cozy-snapshot-controller
  namespace: cozy-snapshot-controller
  dependsOn: [cilium,kubeovn,cert-manager-issuers]

- name: objectstorage-controller
  releaseName: objectstorage-controller
  chart: cozy-objectstorage-controller
  namespace: cozy-objectstorage-controller
  dependsOn: [cilium,kubeovn]

- name: telepresence
  releaseName: traffic-manager
  chart: cozy-telepresence
  namespace: cozy-telepresence
  optional: true
  dependsOn: [cilium,kubeovn]

- name: dashboard
  releaseName: dashboard
  chart: cozy-dashboard
  namespace: cozy-dashboard
  values:
    {{- $dashboardKCconfig := lookup "v1" "ConfigMap" "cozy-dashboard" "kubeapps-auth-config" }}
    {{- $dashboardKCValues := dig "data" "values.yaml" "" $dashboardKCconfig | fromYaml }}
    {{- toYaml (deepCopy $dashboardKCValues | mergeOverwrite (fromYaml (include "cozystack.defaultDashboardValues" .))) | nindent 4 }}
  dependsOn:
  - cilium
  - kubeovn
  {{- if eq $oidcEnabled "true" }}
  - keycloak-configure
  {{- end }}

- name: kamaji
  releaseName: kamaji
  chart: cozy-kamaji
  namespace: cozy-kamaji
  dependsOn: [cilium,kubeovn,cert-manager]

- name: capi-operator
  releaseName: capi-operator
  chart: cozy-capi-operator
  namespace: cozy-cluster-api
  privileged: true
  dependsOn: [cilium,kubeovn,cert-manager]

- name: capi-providers-bootstrap
  releaseName: capi-providers-bootstrap
  chart: cozy-capi-providers-bootstrap
  namespace: cozy-cluster-api
  privileged: true
  dependsOn: [cilium,kubeovn,capi-operator]

- name: capi-providers-core
  releaseName: capi-providers-core
  chart: cozy-capi-providers-core
  namespace: cozy-cluster-api
  privileged: true
  dependsOn: [cilium,kubeovn,capi-operator]

- name: capi-providers-cpprovider
  releaseName: capi-providers-cpprovider
  chart: cozy-capi-providers-cpprovider
  namespace: cozy-cluster-api
  privileged: true
  dependsOn: [cilium,kubeovn,capi-operator]

- name: capi-providers-infraprovider
  releaseName: capi-providers-infraprovider
  chart: cozy-capi-providers-infraprovider
  namespace: cozy-cluster-api
  privileged: true
  dependsOn: [cilium,kubeovn,capi-operator]

- name: external-dns
  releaseName: external-dns
  chart: cozy-external-dns
  namespace: cozy-external-dns
  optional: true
  dependsOn: [cilium,kubeovn]

- name: external-secrets-operator
  releaseName: external-secrets-operator
  chart: cozy-external-secrets-operator
  namespace: cozy-external-secrets-operator
  optional: true
  dependsOn: [cilium,kubeovn]

- name: bootbox
  releaseName: bootbox
  chart: cozy-bootbox
  namespace: cozy-bootbox
  privileged: true
  optional: true
  dependsOn: [cilium,kubeovn]

{{- if $oidcEnabled }}
- name: keycloak
  releaseName: keycloak
  chart: cozy-keycloak
  namespace: cozy-keycloak
  dependsOn: [postgres-operator]

- name: keycloak-operator
  releaseName: keycloak-operator
  chart: cozy-keycloak-operator
  namespace: cozy-keycloak
  dependsOn: [keycloak]

- name: keycloak-configure
  releaseName: keycloak-configure
  chart: cozy-keycloak-configure
  namespace: cozy-keycloak
  dependsOn: [keycloak-operator]
  values:
    cozystack:
      configHash: {{ $cozyConfig | toJson | sha256sum }}
{{- end }}

- name: goldpinger
  releaseName: goldpinger
  chart: cozy-goldpinger
  namespace: cozy-goldpinger
  privileged: true
  dependsOn: [monitoring-agents]

- name: vertical-pod-autoscaler
  releaseName: vertical-pod-autoscaler
  chart: cozy-vertical-pod-autoscaler
  namespace: cozy-vertical-pod-autoscaler
  privileged: true
  dependsOn: [monitoring-agents]
  values:
    vertical-pod-autoscaler:
      recommender:
        extraArgs:
          prometheus-address: http://vmselect-shortterm.tenant-root.svc.{{ $clusterDomain }}:8481/select/0/prometheus/

- name: vertical-pod-autoscaler-crds
  releaseName: vertical-pod-autoscaler-crds
  chart: cozy-vertical-pod-autoscaler-crds
  namespace: cozy-vertical-pod-autoscaler
  privileged: true
  dependsOn: [cilium, kubeovn]

- name: reloader
  releaseName: reloader
  chart: cozy-reloader
  namespace: cozy-reloader
