apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
    app.kubernetes.io/name: [[ .Name | quote ]]
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
  name: [[ .Name | quote ]]
  namespace: {{ .Release.Namespace | quote }}
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: [[ .Name | quote ]]
      app.kubernetes.io/instance: {{ .Release.Name }}
      app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
      app.kubernetes.io/managed-by: {{ .Release.Service }}
  replicas: {{ .Values.[[ .Name ]].replicas }}
  template:
    metadata:
      labels:
        app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
        app.kubernetes.io/name: [[ .Name | quote ]]
        app.kubernetes.io/managed-by: {{ .Release.Service }}
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      containers:
        - name: [[ .Name | quote]]
          image: "{{ coalesce .Values.[[ .Name ]].image.registry .Values.global.registry "docker.io"}}/{{ coalesce .Values.[[ .Name ]].image.repository .Values.global.repository }}/{{ .Values.[[ .Name ]].image.name }}:{{ .Values.[[ .Name ]].image.tag | default "latest" }}"
          imagePullPolicy: {{ .Values.[[ .Name ]].image.imagePullPolicy | default "IfNotPresent" }}
          ports:
            {{- range $port := .Values.[[ .Name ]].ports }}
            - containerPort: {{ $port.containerPort }}
              name: {{ $port.name }}
              protocol: {{ $port.protocol }}
            {{- end }}
          {{- if .Values.[[ .Name ]].resources }}  
          resources:
            {{- toYaml .Values.[[ .Name ]].resources | nindent 12 }}
          {{- end }}
          {{- if .Values.[[ .Name ]].probes }}
          {{- if and (.Values.[[ .Name ]].probes.liveness) (eq .Values.[[ .Name ]].probes.liveness.type "httpGet") }}
          livenessProbe:
            httpGet:
              path: {{ .Values.[[ .Name ]].probes.liveness.config.path }}
              port: {{ .Values.[[ .Name ]].probes.liveness.config.port }}
            {{- if .Values.[[ .Name ]].probes.liveness.config.initialDelaySeconds }}
            initialDelaySeconds: {{ .Values.[[ .Name ]].probes.liveness.config.initialDelaySeconds }}
            {{- end }}
            {{- if .Values.[[ .Name ]].probes.liveness.config.periodSeconds }}
            periodSeconds: {{ .Values.[[ .Name ]].probes.liveness.config.periodSeconds }}
            {{- end }}
            {{- if .Values.[[ .Name ]].probes.liveness.config.timeoutSeconds }}
            timeoutSeconds: {{ .Values.[[ .Name ]].probes.liveness.config.timeoutSeconds }}
            {{- end }}
            {{- if .Values.[[ .Name ]].probes.liveness.config.successThreshold }}
            successThreshold: {{ .Values.[[ .Name ]].probes.liveness.config.successThreshold }}
            {{- end }}
            {{- if .Values.[[ .Name ]].probes.liveness.config.failureThreshold }}
            failureThreshold: {{ .Values.[[ .Name ]].probes.liveness.config.failureThreshold }}
            {{- end }}
            {{- if .Values.[[ .Name ]].probes.liveness.config.terminationGracePeriodSeconds }}
            terminationGracePeriodSeconds: {{ .Values.[[ .Name ]].probes.liveness.config.terminationGracePeriodSeconds }}
            {{- end }}
          {{- end }}
          {{- if and (.Values.[[ .Name ]].probes.readiness) (eq .Values.[[ .Name ]].probes.readiness.type "httpGet") }}
          readinessProbe:
            httpGet:
              path: {{ .Values.[[ .Name ]].probes.readiness.config.path }}
              port: {{ .Values.[[ .Name ]].probes.readiness.config.port }}
            {{- if .Values.[[ .Name ]].probes.readiness.config.initialDelaySeconds }}
            initialDelaySeconds: {{ .Values.[[ .Name ]].probes.readiness.config.initialDelaySeconds }}
            {{- end }}
            {{- if .Values.[[ .Name ]].probes.readiness.config.periodSeconds }}
            periodSeconds: {{ .Values.[[ .Name ]].probes.readiness.config.periodSeconds }}
            {{- end }}
            {{- if .Values.[[ .Name ]].probes.readiness.config.timeoutSeconds }}
            timeoutSeconds: {{ .Values.[[ .Name ]].probes.readiness.config.timeoutSeconds }}
            {{- end }}
            {{- if .Values.[[ .Name ]].probes.readiness.config.successThreshold }}
            successThreshold: {{ .Values.[[ .Name ]].probes.readiness.config.successThreshold }}
            {{- end }}
            {{- if .Values.[[ .Name ]].probes.readiness.config.failureThreshold }}
            failureThreshold: {{ .Values.[[ .Name ]].probes.readiness.config.failureThreshold }}
            {{- end }}
            {{- if .Values.[[ .Name ]].probes.readiness.config.terminationGracePeriodSeconds }}
            terminationGracePeriodSeconds: {{ .Values.[[ .Name ]].probes.readiness.config.terminationGracePeriodSeconds }}
            {{- end }}
          {{- end }}
          {{- end}}
          env:
            [[- $componentName := .Name ]]
            [[- range $config := .Configs ]]
            {{- range $key, $value := .Values.[[ $componentName ]].configs }}
            - name: {{ $key | snakecase | upper }}
              valueFrom:
                secretKeyRef:
                  name: [[ $config.Name ]]
                  key: {{ $key | snakecase | upper }}
            {{- end }}
            [[- end ]]