apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "tomcat.fullname" . }}
  labels: {{- include "tomcat.labels" . | nindent 4 }}
spec:
  strategy:
    type: {{ .Values.updateStrategy }}
    {{- if (eq "Recreate" .Values.updateStrategy) }}
    rollingUpdate: null
    {{- end }}
  selector:
    matchLabels: {{- include "tomcat.matchLabels" . | nindent 6 }}
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels: {{- include "tomcat.labels" . | nindent 8 }}
    {{- if .Values.podAnnotations }}
    annotations: {{- toYaml .Values.podAnnotations | nindent 8 }}
    {{- end }}
    spec:
{{- include "tomcat.imagePullSecrets" . | indent 6 }}
      {{- if .Values.affinity }}
      affinity: {{- include "tomcat.tplValue" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "tomcat.tplValue" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "tomcat.tplValue" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.securityContext.fsGroup }}
        runAsUser: {{ .Values.securityContext.runAsUser }}
      {{- end }}
      {{- if and .Values.volumePermissions.enabled .Values.persistence.enabled }}
      initContainers:
        - name: volume-permissions
          image: {{ template "tomcat.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
            - -ec
            - |
              chown -R {{ .Values.securityContext.runAsUser }}:{{ .Values.securityContext.fsGroup }} /bitnami/tomcat
          securityContext:
            runAsUser: 0
          {{- if .Values.volumePermissions.resources }}
          resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: /bitnami/tomcat
      {{- end }}
      containers:
        - name: tomcat
          image: {{ template "tomcat.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.command }}
          command: {{- include "tomcat.tplValue" ( dict "value" .Values.command "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: TOMCAT_USERNAME
              value: {{ .Values.tomcatUsername | quote }}
            - name: TOMCAT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "tomcat.fullname" . }}
                  key: tomcat-password
            - name: TOMCAT_ALLOW_REMOTE_MANAGEMENT
              value: {{ .Values.tomcatAllowRemoteManagement | quote }}
          ports:
            - name: http
              containerPort: 8080
          livenessProbe:
            httpGet:
              path: /
              port: http
            initialDelaySeconds: 120
            timeoutSeconds: 5
            failureThreshold: 6
          readinessProbe:
            httpGet:
              path: /
              port: http
            initialDelaySeconds: 30
            timeoutSeconds: 3
            periodSeconds: 51
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: /bitnami/tomcat
      volumes:
        - name: data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ template "tomcat.pvc" . }}
          {{- else }}
          emptyDir: {}
          {{- end -}}
