apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "layer0_web.fullname" . }}
  namespace: {{ .Release.Namespace  }}
  labels:
{{ include "layer0_web.labels" . | indent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
{{ include "layer0_web.labels" . | indent 6 }}
  template:
    metadata:
      labels:
{{ include "layer0_web.labels" . | indent 8 }}
    spec:
      volumes:
        - name: domainsconfig
          configMap:
            name: domainsconfig
            items:
            - key: domains.json
              path: domains.json
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image: {{ template "layer0_web.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          volumeMounts:
            - name: domainsconfig
              mountPath: /srv/domains.json
              subPath: domains.json
              readOnly: true
{{- if.Values.global.domains }}
          env:
  {{- range $domain := .Values.global.domains }}
    {{- $name := $domain.name -}}
    {{- $upper_name := regexReplaceAll "\\W+" $name "_" | upper -}}
    {{- $lower_name := regexReplaceAll "\\W+" $name "-" | lower -}}
    {{- $client_id := printf "%s_%s" $upper_name "OAUTH_CLIENT_ID" }}
    {{- $client_secret := printf "%s_%s" $upper_name "OAUTH_CLIENT_SECRET" }}
          - name: {{ $client_id }}
            valueFrom:
              secretKeyRef:
                name: layer1-port-owncloud-{{ $lower_name }}
                key: oauth-client-id
          - name: {{ $client_secret }}
            valueFrom:
              secretKeyRef:
                name: layer1-port-owncloud-{{ $lower_name }}
                key: oauth-client-secret
  {{- end }}
{{- end }}
          envFrom:
          - configMapRef:
              name: mservice
          - configMapRef:
              name: proxy
          - configMapRef:
              name: globalenvvar
          - configMapRef:
              name: webconfig
          ports:
            - name: http
              containerPort: {{ .Values.service.targetPort }}
              protocol: TCP
            - name: metrics
              containerPort: 9999
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /metrics
              port: metrics
            periodSeconds: 10
          readinessProbe:
            httpGet:
              path: /metrics
              port: metrics
            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 }}
