apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "limesurvey.fullname" . }}
  labels:
    {{- include "limesurvey.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "limesurvey.selectorLabels" . | nindent 6 }}
  {{- if .Values.deployment.updateStrategy }}
  strategy: {{- toYaml .Values.deployment.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "limesurvey.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "limesurvey.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          volumeMounts:
            - name: limesurvey-main
              mountPath: "/var/www/html/plugins"
              subPath: "plugins"
            - name: limesurvey-main
              mountPath: "/var/www/html/upload"
              subPath: "upload"
            - name: limesurvey-main
              mountPath: "/var/lime/sessions"
              subPath: "sessions"
            - name: limesurvey-main
              mountPath: "/var/www/html/application/config"
              subPath: "config"
          {{- range .Values.extraVolumeMounts }}
            - name: {{ .name }}
              mountPath: {{ .mountPath }}
              subPath: {{ .subPath | default "" }}
              readOnly: {{ .readOnly }}
          {{- end }}
          {{- range .Values.extraEmptyDirMounts }}
            - name: {{ .name }}
              mountPath: {{ .mountPath }}
          {{- end }}
          env:
          - name: LIMESURVEY_DB_TYPE
            {{- if eq .Values.mariadb.enabled true }}
            value: "mysql"
            {{- else }}
            value: {{ .Values.externalDatabase.type }}
            {{- end }}
          - name: LIMESURVEY_DB_HOST
            {{- if eq .Values.mariadb.enabled true }}
            value: {{ include "limesurvey.mariadb.fullname" . }}
            {{- else }}
            value: {{ .Values.externalDatabase.host }}
            {{- end }}
          - name: LIMESURVEY_DB_PORT
            {{- if eq .Values.mariadb.enabled true }}
            value: {{ coalesce .Values.mariadb.primary.service.ports.mysql .Values.mariadb.primary.service.port 3306 | quote }}
            {{- else }}
            value: {{ coalesce .Values.externalDatabase.port 3306 | quote }}
            {{- end }}
          - name: LIMESURVEY_DB_USER
            {{- if eq .Values.mariadb.enabled true }}
            value: {{ .Values.mariadb.auth.username }}
            {{- else }}
            value: {{ .Values.externalDatabase.username }}
            {{- end }}
          - name: LIMESURVEY_DB_PASSWORD
            valueFrom:
              secretKeyRef:
                name: {{ include "limesurvey.databaseSecretName" . }}
                key: mariadb-password
          - name: LIMESURVEY_DB_NAME
            {{- if eq .Values.mariadb.enabled true }}
            value: {{ .Values.mariadb.auth.database }}
            {{- else }}
            value: {{ .Values.externalDatabase.database }}
            {{- end }}
          - name: LIMESURVEY_ADMIN_USER
            value: {{ .Values.limesurvey.admin.username | quote }}
          - name: LIMESURVEY_ADMIN_PASSWORD
            valueFrom:
              secretKeyRef:
                name: {{ include "limesurvey.secretName" . }}
                key: limesurvey-admin-password
          - name: LIMESURVEY_ADMIN_NAME
            value: {{ .Values.limesurvey.admin.name | quote }}
          - name: LIMESURVEY_ADMIN_EMAIL
            value: {{ .Values.limesurvey.admin.email | quote }}
          - name: LIMESURVEY_TABLE_PREFIX
            value: {{ .Values.limesurvey.tablePrefix | quote }}
          - name: LIMESURVEY_DEBUG
            value: {{ .Values.limesurvey.debug | quote }}
          - name: LIMESURVEY_SQL_DEBUG
            value: {{ .Values.limesurvey.sqlDebug | quote }}
          - name: LIMESURVEY_USE_INNODB
            value: {{ .Values.limesurvey.useInnodb | quote }}
          - name: LIMESURVEY_DB_SESSIONS
            value: {{ .Values.limesurvey.dbSessions | quote }}
          - name: LIMESURVEY_DONT_SHOW_SCRIPT_NAME
            value: {{ .Values.limesurvey.dontShowScriptName | quote }}
          - name: TZ
            value: {{ .Values.limesurvey.tz | quote }}
          - name: LIMESURVEY_SMTP_HOST
            value: {{ .Values.limesurvey.smtp.host | quote }}
          - name: LIMESURVEY_SMTP_USER
            value: {{ .Values.limesurvey.smtp.user | quote }}
          - name: LIMESURVEY_SMTP_PASSWORD
            valueFrom:
              secretKeyRef:
                name: {{ include "limesurvey.smtpSecretName" . }}
                key: limesurvey-smtp-password
          - name: LIMESURVEY_SMTP_SSL
            value: {{ .Values.limesurvey.smtp.ssl | quote }}
          - name: LIMESURVEY_FROM_EMAIL
            value: {{ .Values.limesurvey.smtp.from_email | quote }}
          - name: LIMESURVEY_SMTP_DEBUG
            value: {{ .Values.limesurvey.smtp.debug | quote }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.deployment.lifecycleHooks }}
          lifecycle: {{- toYaml .Values.deployment.lifecycleHooks | nindent 12 }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          {{- if .Values.limesurvey.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /
              port: http
              httpHeaders:
              - name: X-Forwarded-Proto
                value: https
          {{- end }}
          {{- if .Values.limesurvey.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /
              port: http
              httpHeaders:
              - name: X-Forwarded-Proto
                value: https
          {{- end }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      initContainers:
      - name: db-isalive
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
        env:
        - name: LIMESURVEY_DB_HOST
          {{- if eq .Values.mariadb.enabled true }}
          value: {{ include "limesurvey.mariadb.fullname" . }}
          {{- else }}
          value: {{ .Values.externalDatabase.host }}
          {{- end }}
        - name: LIMESURVEY_DB_PORT
          {{- if eq .Values.mariadb.enabled true }}
          value: {{ coalesce .Values.mariadb.primary.service.ports.mysql .Values.mariadb.primary.service.port 3306 | quote }}
          {{- else }}
          value: {{ coalesce .Values.externalDatabase.port 3306 | quote }}
          {{- end }}
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        command:
          - "sh"
          - "-c"
          - "until nc -z -v -w30 ${LIMESURVEY_DB_HOST} ${LIMESURVEY_DB_PORT}; do echo waiting for db; sleep 5; done;"
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
        - name: limesurvey-main
      {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim | default (include "limesurvey.fullname" .) }}
      {{- else }}
          emptyDir: {}
      {{- end -}}
      {{- range .Values.extraVolumeMounts }}
        - name: {{ .name }}
          {{- if .existingClaim }}
          persistentVolumeClaim:
            claimName: {{ .existingClaim }}
          {{- else if .hostPath }}
          hostPath:
            path: {{ .hostPath }}
          {{- else if .configMap }}
          configMap:
            name: {{ .configMap }}
            {{- with .items }}
            items:
              {{- toYaml . | nindent 14 }}
            {{- end }}
          {{- else }}
          emptyDir: {}
          {{- end }}
      {{- end }}
      {{- range .Values.extraEmptyDirMounts }}
        - name: {{ .name }}
          emptyDir: {}
      {{- end -}}
