{{- $cozyConfig := lookup "v1" "ConfigMap" "cozy-system" "cozystack" }}
{{- $bundleName := index $cozyConfig.data "bundle-name" }}
{{- $bundle := tpl (.Files.Get (printf "bundles/%s.yaml" $bundleName)) . | fromYaml }}
{{- $host := "example.org" }}
{{- $host := "example.org" }}
{{- if $cozyConfig.data }}
  {{- if hasKey $cozyConfig.data "root-host" }}
    {{- $host = index $cozyConfig.data "root-host" }}
  {{- end }}
{{- end }}
{{- $tenantRoot := dict }}
{{- if .Capabilities.APIVersions.Has "helm.toolkit.fluxcd.io/v2" }}
{{- $tenantRoot = lookup "helm.toolkit.fluxcd.io/v2" "HelmRelease" "tenant-root" "tenant-root" }}
{{- end }}
{{- if and $tenantRoot $tenantRoot.spec $tenantRoot.spec.values $tenantRoot.spec.values.host }}
{{- $host = $tenantRoot.spec.values.host }}
{{- else }}
{{- end }}
---
apiVersion: v1
kind: Namespace
metadata:
  annotations:
    helm.sh/resource-policy: keep
    namespace.cozystack.io/etcd: tenant-root
    namespace.cozystack.io/monitoring: tenant-root
    namespace.cozystack.io/ingress: tenant-root
    namespace.cozystack.io/seaweedfs: tenant-root
    namespace.cozystack.io/host: "{{ $host }}"
  name: tenant-root
---
apiVersion: helm.toolkit.fluxcd.io/v2
kind: HelmRelease
metadata:
  name: tenant-root
  namespace: tenant-root
  labels:
    cozystack.io/ui: "true"
spec:
  interval: 0s
  releaseName: tenant-root
  install:
    remediation:
      retries: -1
  upgrade:
    remediation:
      retries: -1
  chart:
    spec:
      chart: tenant
      sourceRef:
        kind: HelmRepository
        name: cozystack-apps
        namespace: cozy-public
  values:
    host: "{{ $host }}"
  dependsOn:
  {{- range $x := $bundle.releases }}
  {{- if has $x.name (list "cilium" "kubeovn") }}
  - name: {{ $x.name }}
    namespace: {{ $x.namespace }}
  {{- end }}
  {{- end }}
