{{- $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]
  values:
    flux-instance:
      instance:
        cluster:
          domain: {{ $clusterDomain }}

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

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

- name: cozystack-controller
  releaseName: cozystack-controller
  chart: cozy-cozystack-controller
  namespace: cozy-system
  dependsOn: []
  {{- 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: [cert-manager]

- name: victoria-metrics-operator
  releaseName: victoria-metrics-operator
  chart: cozy-victoria-metrics-operator
  namespace: cozy-victoria-metrics-operator
  dependsOn: [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: etcd-operator
  releaseName: etcd-operator
  chart: cozy-etcd-operator
  namespace: cozy-etcd-operator
  dependsOn: [cert-manager]

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

- name: mariadb-operator
  releaseName: mariadb-operator
  chart: cozy-mariadb-operator
  namespace: cozy-mariadb-operator
  dependsOn: [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: [cert-manager,victoria-metrics-operator]

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

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

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

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

- name: piraeus-operator
  releaseName: piraeus-operator
  chart: cozy-piraeus-operator
  namespace: cozy-linstor
  dependsOn: [cert-manager]

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

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

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

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

- 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" (dict) $dashboardKCconfig }}
    {{- toYaml (deepCopy $dashboardKCValues | mergeOverwrite (fromYaml (include "cozystack.defaultDashboardValues" .))) | nindent 4 }}
  {{- if eq $oidcEnabled "true" }}
  dependsOn: [keycloak-configure]
  {{- else }}
  dependsOn: []
  {{- end }}

{{- 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: []

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