apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "booklore.fullname" . }}
  labels:
    {{- include "booklore.labels" . | nindent 4 }}
spec:
  selector:
    matchLabels:
      {{- include "booklore.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "booklore.labels" . | nindent 8 }}
        {{- with .Values.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "booklore.serviceAccountName" . }}
      {{- with .Values.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      initContainers:
        - name: wait-for-database
          image: busybox:1.36
          command:
            - /bin/sh
            - -c
            - |
              echo "Waiting for database on port {{ .Values.mariadb.primary.service.ports.mysql }}..."
              until nc -z -v -w5 {{ .Release.Name }}-mariadb {{ .Values.mariadb.primary.service.ports.mysql }}; do
                echo "Waiting for database..."
                sleep 2
              done
              echo "Database is available!"
          resources:
            requests:
              cpu: 10m
              memory: 16Mi
            limits:
              cpu: 50m
              memory: 32Mi
      containers:
        - name: {{ .Chart.Name }}
          {{- with .Values.securityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              #protocol: TCP
          env:
            - name: BOOKLORE_PORT
              value: "6060"
            - name: DATABASE_URL
              value: jdbc:mariadb://{{ .Release.Name }}-mariadb:{{ .Values.mariadb.primary.service.ports.mysql }}/{{ .Values.mariadb.auth.database }}
            - name: DATABASE_USERNAME
              value: {{ .Values.mariadb.auth.username }}
            - name: DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  {{- if .Values.mariadb.auth.existingSecret }}
                  name: {{ .Values.mariadb.auth.existingSecret }}
                  {{- else }}
                  name: {{ .Release.Name }}-mariadb
                  {{- end }}
                  key: mariadb-password
          {{- with .Values.livenessProbe }}
          livenessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.readinessProbe }}
          readinessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- if (or .Values.persistence.dataVolume.enabled .Values.persistence.booksVolume.enabled .Values.extraVolumeMounts) }}
          volumeMounts:
            {{- if .Values.persistence.dataVolume.enabled }}
            - name: data
              mountPath: /app/data
            {{- end }}
            {{- if .Values.persistence.booksVolume.enabled }}
            - name: books
              mountPath: /books
            {{- end }}
            {{- with .Values.extraVolumeMounts }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          {{- end }}
      {{- if (or .Values.persistence.dataVolume.enabled .Values.persistence.booksVolume.enabled .Values.extraVolumes) }}
      volumes:
        {{- if .Values.persistence.dataVolume.enabled }}
        - name: data
          persistentVolumeClaim:
            claimName: {{ if .Values.persistence.dataVolume.existingClaim }}
              {{- .Values.persistence.dataVolume.existingClaim }}
            {{ else }}
              {{- include "booklore.fullname" . }}-data
            {{- end }}
        {{- end }}
        {{- if .Values.persistence.booksVolume.enabled }}
        - name: books
          persistentVolumeClaim:
            claimName: {{ if .Values.persistence.booksVolume.existingClaim }}
              {{- .Values.persistence.booksVolume.existingClaim }}
            {{ else }}
              {{- include "booklore.fullname" . }}-books
            {{- end }}
        {{- end }}
        {{- with .Values.extraVolumes }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- end -}}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
