{{- $domains := .Values.domains -}}
{{- if .Values.global }}
  {{- if .Values.global.domains }}
    {{- $domains = .Values.global.domains -}}
  {{- end -}}
{{- end -}}

{{- if not $domains -}}
  {{- $name := (dict "name" (.Values.environment.ADDRESS | trimPrefix "https://" | trimPrefix "http://")) -}}
  {{- $domains = (list (merge .Values.environment $name)) -}}
{{- end -}}

{{- range $domains }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "layer1_port_owncloud.fullname"  (mergeOverwrite $ .) }}
  namespace: {{ $.Values.global.namespace.name | default $.Release.Namespace  }}
  labels:
{{ include "layer1_port_owncloud.labels" (mergeOverwrite $ .) | indent 4 }}
spec:
  replicas: {{ $.Values.replicaCount }}
  selector:
    matchLabels:
{{ include "layer1_port_owncloud.labels" (mergeOverwrite $ .) | indent 6 }}
  template:
    metadata:
      labels:
{{ include "layer1_port_owncloud.labels" (mergeOverwrite $ .) | indent 8 }}
    spec:
    {{- with $.Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      containers:
        - name: {{ $.Chart.Name }}
          image: {{ template "layer1_port_owncloud.image"  $ }}
          imagePullPolicy: {{ $.Values.image.pullPolicy }}
          env:
          - name: OWNCLOUD_OAUTH_CLIENT_ID
            valueFrom:
              secretKeyRef:
                name: layer1-port-owncloud-{{ .name | replace "." "-" | replace ":" "-" }}
                key: "oauth-client-id"
          - name: OWNCLOUD_OAUTH_CLIENT_SECRET
            valueFrom:
              secretKeyRef:
                name: layer1-port-owncloud-{{ .name | replace "." "-" | replace ":" "-" }}
                key: "oauth-client-secret"
          envFrom:
          - configMapRef:
              name: mservice
          - configMapRef:
              name: proxy
          - configMapRef:
              name: globalenvvar
          - configMapRef:
              name: portowncloudconfig-{{ .name | replace "." "-" | replace ":" "-" }}
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /metrics
              port: http
            periodSeconds: 10
          readinessProbe:
            httpGet:
              path: /metrics
              port: http
            periodSeconds: 10
          resources:
            {{- toYaml $.Values.resources | nindent 12 }}
      {{- with $.Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with $.Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with $.Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
{{- end }}
