{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
  replicas: {{ .Values.replicaCount }}
  {{- if .Values.updateStrategy }}
  strategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
      annotations:
        checksum/secret: {{ include (print $.Template.BasePath "/secrets.yaml") . | sha256sum }}
        {{- if .Values.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "ejbca.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.automountServiceAccountToken }}
      {{- if .Values.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "ejbca.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 "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "customLabels" $podLabels "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.schedulerName }}
      schedulerName: {{ .Values.schedulerName }}
      {{- end }}
      {{- if .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- 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 }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
        - name: prepare-writable-files
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          image: {{ template "ejbca.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          command:
            - /bin/bash
          args:
            - -ec
            - |
              #!/bin/bash

              . /opt/bitnami/scripts/liblog.sh

              # We need the standalone.conf to be writable but not the rest of the bin folder
              info "Copying standalone.conf"
              cp /opt/bitnami/wildfly/bin/standalone.conf /wildfly-standalone-conf-dir
              info "Writable files successfully copied"

          volumeMounts:
            - name: empty-dir
              mountPath: /wildfly-standalone-conf-dir
              subPath: wildfly-standalone-conf-dir
      {{- if .Values.initContainers }}
      {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: ejbca
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          image: {{ template "ejbca.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.args "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" (or .Values.image.debug .Values.diagnosticMode.enabled) | quote }}
            {{- if .Values.usePasswordFiles }}
            - name: EJBCA_ADMIN_PASSWORD_FILE
              value: "/opt/bitnami/ejbca/secrets/ejbca-admin-password"
            - name: EJBCA_DATABASE_PASSWORD_FILE
              value: "/opt/bitnami/ejbca/secrets/mariadb-password"
            {{- if .Values.ejbcaKeystoreExistingSecret }}
            - name: EJBCA_SERVER_CERT_PASSWORD_FILE
              value: "/opt/bitnami/ejbca/secrets/keystore-password"
            {{- end }}
            {{- else }}
            - name: EJBCA_ADMIN_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "ejbca.secretName" . }}
                  key: ejbca-admin-password
            - name: EJBCA_DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "ejbca.databaseSecretName" . }}
                  key: mariadb-password
            {{- if .Values.ejbcaKeystoreExistingSecret }}
            - name: EJBCA_SERVER_CERT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.ejbcaKeystoreExistingSecret }}
                  key: keystore-password
            {{- end }}
            {{- end }}
            - name: EJBCA_ADMIN_USERNAME
              value: {{ .Values.ejbcaAdminUsername | quote }}
            - name: EJBCA_DATABASE_HOST
              value: {{ include "ejbca.databaseHost" . | quote }}
            - name: EJBCA_DATABASE_PORT
              value: {{ include "ejbca.databasePort" . | quote }}
            - name: EJBCA_DATABASE_NAME
              value: {{ include "ejbca.databaseName" . | quote }}
            - name: EJBCA_DATABASE_USERNAME
              value: {{ include "ejbca.databaseUsername" . | quote }}
            - name: EJBCA_HTTP_PORT_NUMBER
              value: {{ .Values.containerPorts.http | quote }}
            - name: EJBCA_HTTPS_PORT_NUMBER
              value: {{ .Values.containerPorts.https | quote }}
            - name: EJBCA_HTTPS_ADVERTISED_PORT_NUMBER
              value: {{ .Values.service.advertisedHttpsPort | quote }}
            {{- if .Values.ejbcaKeystoreExistingSecret }}
            - name: EJBCA_SERVER_CERT_FILE
              value: /opt/bitnami/ejbca/mounted-jks/keystore.jks
            {{- end }}
            {{- if .Values.ejbcaJavaOpts }}
            - name: JAVA_OPTS
              value: {{ .Values.ejbcaJavaOpts | quote }}
            {{- end }}
            {{- if .Values.ejbcaCA.name }}
            - name: EJBCA_CA_NAME
              value: {{ .Values.ejbcaCA.name | quote }}
            {{- end }}
            {{- if .Values.ejbcaCA.baseDN }}
            - name: EJBCA_BASE_DN
              value: {{ .Values.ejbcaCA.baseDN | quote }}
            {{- end }}
            {{- if .Values.extraEnvVars }}
              {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.extraEnvVarsSecret }}
            {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.containerPorts.http }}
            - name: https
              containerPort: {{ .Values.containerPorts.https }}
            {{- /* Currently hardcoded in EJBCA logic: https://github.com/bitnami/containers/blob/main/bitnami/ejbca/8/debian-12/rootfs/opt/bitnami/scripts/libejbca.sh#L212 */}}
            - name: https-pub
              containerPort: 8442
            - name: ajp
              containerPort: 8009
            {{- if .Values.extraContainerPorts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraContainerPorts "context" $) | nindent 12 }}
            {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.startupProbe.enabled }}
          startupProbe:
            initialDelaySeconds: {{ .Values.startupProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.startupProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.startupProbe.timeoutSeconds }}
            successThreshold: {{ .Values.startupProbe.successThreshold }}
            failureThreshold: {{ .Values.startupProbe.failureThreshold }}
            httpGet:
              path: /ejbca/doc
              port: http
          {{- end }}
          {{- if .Values.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customLivenessProbe "context" $) | nindent 12 }}
          {{- else 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 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- if .Values.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customReadinessProbe "context" $) | nindent 12 }}
          {{- else 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 }}
            httpGet:
              path: /ejbca/doc
              port: http
          {{- end }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /opt/bitnami/wildfly/standalone
              subPath: wildfly-standalone-dir
            - name: empty-dir
              mountPath: /opt/bitnami/wildfly/bin/standalone.conf
              subPath: wildfly-standalone-conf-dir/standalone.conf
            - name: empty-dir
              mountPath: /opt/bitnami/wildfly/tmp
              subPath: wildfly-tmp-dir
            - name: empty-dir
              mountPath: /opt/bitnami/ejbca/tmp
              subPath: app-tmp-dir
            - name: empty-dir
              mountPath: /opt/bitnami/wildfly/domain/configuration
              subPath: wildfly-domain-conf-dir
            - name: empty-dir
              mountPath: /opt/bitnami/ejbca/conf
              subPath: app-conf-dir
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: ejbca-data
              mountPath: /bitnami/wildfly
              subPath: wildfly
            {{- if .Values.usePasswordFiles }}
            - name: ejbca-secrets
              mountPath: /opt/bitnami/ejbca/secrets
            {{- end }}
            {{- if .Values.ejbcaKeystoreExistingSecret }}
            - name: ejbca-keystore
              mountPath: /opt/bitnami/ejbca/mounted-jks/keystore.jks
              subPath: "keystore.jks"
            {{- end }}
            {{- if .Values.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- else if ne .Values.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.resourcesPreset) | nindent 12 }}
          {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        {{- if .Values.ejbcaKeystoreExistingSecret }}
        - name: ejbca-keystore
          secret:
            secretName: {{ .Values.ejbcaKeystoreExistingSecret }}
            items:
            - key: "keystore.jks"
              path: "keystore.jks"
        {{- end }}
        {{- if .Values.usePasswordFiles }}
        - name: ejbca-secrets
          projected:
            sources:
              - secret:
                  name:  {{ include "ejbca.secretName" . }}
              - secret:
                  name:  {{ include "ejbca.databaseSecretName" . }}
              {{- if .Values.ejbcaKeystoreExistingSecret }}
              - secret:
                  name: {{ .Values.ejbcaKeystoreExistingSecret }}
              {{- end }}
        {{- end }}
        - name: ejbca-data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim | default (include "common.names.fullname" .) }}
          {{- else }}
          emptyDir: {}
          {{ end }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
