apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "wildfly.fullname" . }}
  labels: {{- include "wildfly.labels" . | nindent 4 }}
spec:
  strategy:
    type: {{ .Values.updateStrategy }}
    {{- if (eq "Recreate" .Values.updateStrategy) }}
    rollingUpdate: null
    {{- end }}
  selector:
    matchLabels: {{- include "wildfly.matchLabels" . | nindent 6 }}
  template:
    metadata:
      labels: {{- include "wildfly.labels" . | nindent 8 }}
    {{- if .Values.podAnnotations }}
      annotations: {{- toYaml .Values.podAnnotations | nindent 8 }}
    {{- end }}
    spec:
{{- include "wildfly.imagePullSecrets" . | indent 6 }}
      {{- if .Values.affinity }}
      affinity: {{- include "wildfly.tplValue" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "wildfly.tplValue" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "wildfly.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 "wildfly.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
            - -ec
            - |
              chown -R {{ .Values.securityContext.runAsUser }}:{{ .Values.securityContext.fsGroup }} /bitnami/wildfly
          securityContext:
            runAsUser: 0
          {{- if .Values.volumePermissions.resources }}
          resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: /bitnami/wildfly
      {{- end }}
      containers:
        - name: wildfly
          image: {{ template "wildfly.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          env:
            - name: WILDFLY_USERNAME
              value: {{ default "" .Values.wildflyUsername | quote }}
            - name: WILDFLY_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "wildfly.fullname" . }}
                  key: wildfly-password
            {{- if .Values.extraEnvVars }}
            {{- include "wildfly.tplValue" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          {{- if or .Values.extraEnvVarsCM .Values.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.extraEnvVarsSecret }}
            {{- end }}
          {{- end }}
          ports:
            - name: http
              containerPort: 8080
            - name: mgmt
              containerPort: 9990
          livenessProbe:
            {{- include "wildfly.tplValue" ( dict "value" .Values.livenessProbe "context" $) | nindent 12 }}
          readinessProbe:
            {{- include "wildfly.tplValue" ( dict "value" .Values.readinessProbe "context" $) | nindent 12 }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: /bitnami/wildfly
            {{- if .Values.extraVolumeMounts }}
            {{- include "wildfly.tplValue" ( dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.sidecars }}
        {{- include "wildfly.tplValue" (dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ template "wildfly.fullname" . }}
          {{- else }}
          emptyDir: {}
        {{- end -}}
        {{- if .Values.extraVolumes }}
        {{- include "wildfly.tplValue" ( dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
