apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "common.names.fullname" . }}
  labels:
    {{- include "common.labels.standard" $ | nindent 4 }}
    app.kubernetes.io/component: server
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations:
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  {{- if .Values.updateStrategy }}
  strategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "common.labels.matchLabels" $ | nindent 6 }}
      app.kubernetes.io/component: server
  template:
    metadata:
      {{- if .Values.podAnnotations }}
      annotations:
        {{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels:
        {{- include "common.labels.standard" $ | nindent 8 }}
        app.kubernetes.io/component: server
        {{- if .Values.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "invoiceninja.imagePullSecrets" . | nindent 6 }}
      serviceAccountName: {{ include "invoiceninja.serviceAccountName" . }}
      {{- if .Values.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAffinityPreset "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.nodeAffinityPreset.type "key" .Values.nodeAffinityPreset.key "values" .Values.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      initContainers:
        - name: wait-db
          image: {{ include "invoiceninja.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - /bin/sh
            - -cx
            - |
              COUNTER=0;
              [[ -z "${DB_HOST1}" ]] || DB_HOST="${DB_HOST1}";
              [[ -z "${DB_PORT1}" ]] || DB_PORT="${DB_PORT1}";
              while [ $COUNTER -lt 120 ]; do
                if mysqladmin ping -h "$DB_HOST" -P $DB_PORT --connect-timeout=15 --silent; then
                  exit 0;
                fi;
                let COUNTER=COUNTER+1;
                echo "Waiting for DB... Trying again in 2s";
                sleep 2;
              done;
              echo "Did NOT see a database after 240 secs!";
              exit 1;
          securityContext:
            {{- toYaml .Values.containerSecurityContext | nindent 12 }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- end }}
          envFrom:
            - configMapRef:
                name: {{ include "common.names.fullname" . }}
            {{- if .Values.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsSecret "context" $) }}
            {{- end }}
          env:
            {{- if .Values.extraEnvVars }}
              {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
        {{- if and .Values.podSecurityContext .Values.volumePermissions.enabled (or .Values.persistence.public.enabled .Values.persistence.storage.enabled) }}
        - name: volume-permissions
          image: {{ include "invoiceninja.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
            - -ec
            - |
              chown -R {{ .Values.containerSecurityContext.runAsUser }}:{{ .Values.podSecurityContext.fsGroup }} /var/www/app/{public,storage}
          securityContext:
            runAsUser: 0
          {{- if .Values.volumePermissions.resources }}
          resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - mountPath: /var/www/app/public
              name: public
            - mountPath: /var/www/app/storage
              name: storage
        {{- end }}
        {{- if .Values.initContainers }}
          {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.containerSecurityContext | nindent 12 }}
          image: {{ include "invoiceninja.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: fastcgi
              containerPort: {{ .Values.containerPorts.fastcgi }}
              protocol: TCP
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ $.Values.livenessProbe.timeoutSeconds }}
            successThreshold: {{ $.Values.livenessProbe.successThreshold }}
            failureThreshold: {{ $.Values.livenessProbe.failureThreshold }}
            exec:
              command:
              - pgrep
              - php-fpm
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe:
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ $.Values.readinessProbe.timeoutSeconds }}
            successThreshold: {{ $.Values.readinessProbe.successThreshold }}
            failureThreshold: {{ $.Values.readinessProbe.failureThreshold }}
            tcpSocket:
              port: {{ .Values.readinessProbe.port }}
          {{- end }}
          envFrom:
            - configMapRef:
                name: {{ include "common.names.fullname" . }}
            - secretRef:
                name: {{ include "invoiceninja.secretName" . }}
            {{- if .Values.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsSecret "context" $) }}
            {{- end }}
          env:
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "invoiceninja.databaseSecretName" . }}
                  key: mariadb-password
            {{- if or (and .Values.redis.enabled .Values.redis.auth.enabled) (or .Values.externalRedis.password .Values.externalRedis.existingSecret) }}
            - name: REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "invoiceninja.redisSecretName" . }}
                  key: redis-password
            {{- end }}
            {{- if .Values.userEmail }}
            - name: IN_USER_EMAIL
              value: {{ .Values.userEmail | quote }}
            {{- end }}
            {{- if .Values.extraEnvVars }}
              {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
            - mountPath: /var/www/app/public
              name: public
            - mountPath: /var/www/app/storage
              name: storage
            {{- if .Values.extraVolumeMounts }}
              {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if not .Values.nginx.enabled }}
        - name: nginx
          securityContext:
            {{- toYaml .Values.containerSecurityContext | nindent 12 }}
          image: {{ include "invoiceninja.nginx.image" . }}
          imagePullPolicy: {{ .Values.http.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.http.containerPorts.http }}
            - name: https
              containerPort: {{ .Values.http.containerPorts.https }}
          {{- if .Values.http.livenessProbe.enabled }}
          livenessProbe:
            initialDelaySeconds: {{ .Values.http.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.http.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ $.Values.http.livenessProbe.timeoutSeconds }}
            successThreshold: {{ $.Values.http.livenessProbe.successThreshold }}
            failureThreshold: {{ $.Values.http.livenessProbe.failureThreshold }}
            tcpSocket:
              port: http
          {{- end }}
          {{- if .Values.http.readinessProbe.enabled }}
          readinessProbe:
            initialDelaySeconds: {{ .Values.http.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.http.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ $.Values.http.readinessProbe.timeoutSeconds }}
            successThreshold: {{ $.Values.http.readinessProbe.successThreshold }}
            failureThreshold: {{ $.Values.http.readinessProbe.failureThreshold }}
            tcpSocket:
              port: http
          {{- end }}
          envFrom:
            {{- if .Values.http.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.http.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsSecret "context" $) }}
            {{- end }}
          env:
            {{- if .Values.http.extraEnvVars }}
              {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          resources:
            {{- toYaml .Values.http.resources | nindent 12 }}
          volumeMounts:
            - mountPath: /opt/bitnami/nginx/conf/server_blocks
              name: nginx-server-block
            - mountPath: /app
              name: public
            {{- if .Values.http.extraVolumeMounts }}
              {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- end }}
      volumes:
        - name: public
          {{- if .Values.persistence.public.enabled }}
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.public.existingClaim | default (include "invoiceninja.public.storageName" .) }}
          {{- else }}
          emptyDir: {}
          {{ end }}
        - name: storage
          {{- if .Values.persistence.storage.enabled }}
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.storage.existingClaim | default (include "invoiceninja.storage.storageName" .) }}
          {{- else }}
          emptyDir: {}
          {{ end }}
        {{- if not .Values.nginx.enabled }}
        - name: nginx-server-block
          configMap:
            defaultMode: 420
            name: {{ include "invoiceninja.nginx.serverBlockName" . }}
        {{- end }}
        {{- if .Values.extraVolumes }}
          {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
