{{- if ne (include "tenant.name" .) "tenant-root" }}
---
apiVersion: v1
kind: Namespace
metadata:
  name: {{ include "tenant.name" . }}
  {{- if hasPrefix "tenant-" .Release.Namespace }}
  {{- $existingNS := lookup "v1" "Namespace" "" .Release.Namespace }}
  {{- if $existingNS }}
  annotations:
    {{- if .Values.host }}
    namespace.cozystack.io/host: "{{ .Values.host }}"
    {{- else }}
    {{ $parentHost := index $existingNS.metadata.annotations "namespace.cozystack.io/host" | required (printf "namespace %s has no namespace.cozystack.io/host annotation" $.Release.Namespace) }}
    namespace.cozystack.io/host: "{{ splitList "-" (include "tenant.name" .) | last }}.{{ $parentHost }}"
    {{- end }}
    {{- range $x := list "etcd" "monitoring" "ingress" }}
    {{- if (index $.Values $x) }}
    namespace.cozystack.io/{{ $x }}: "{{ include "tenant.name" $ }}"
    {{- else }}
    namespace.cozystack.io/{{ $x }}: "{{ index $existingNS.metadata.annotations (printf "namespace.cozystack.io/%s" $x) | required (printf "namespace %s has no namespace.cozystack.io/%s annotation" $.Release.Namespace $x) }}"
    {{- end }}
    {{- end }}
  ownerReferences:
  - apiVersion: v1
    blockOwnerDeletion: true
    controller: true
    kind: Namespace
    name: {{ .Release.Namespace }}
    uid: {{ $existingNS.metadata.uid }}
  {{- else }}
  {{- fail (printf "error lookup exiting namespace: %s" .Release.Namespace) }}
  {{- end }}
  {{- end }}
{{- end }}
