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

apiVersion: {{ template "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "common.names.fullname" . }}
  namespace: {{ .Release.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:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  {{- $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 }}
  {{- if .Values.updateStrategy }}
  strategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
      {{- if .Values.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
    spec:
      {{- include "odoo.imagePullSecrets" . | nindent 6 }}
      serviceAccountName: {{ template "odoo.serviceAccountName" . }}
      automountServiceAccountToken: {{ .Values.automountServiceAccountToken }}
      {{- if .Values.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- 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.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.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName | quote }}
      {{- end }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if or (and .Values.volumePermissions.enabled .Values.persistence.enabled) .Values.initContainers }}
      initContainers:
        {{- if and .Values.volumePermissions.enabled .Values.persistence.enabled }}
        - name: volume-permissions
          image: {{ include "odoo.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          command:
            - /bin/bash
          args:
            - -ec
            - |
              mkdir -p /bitnami/odoo
              find /bitnami/odoo -mindepth 1 -maxdepth 1 -not -name ".snapshot" -not -name "lost+found" | xargs -r chown -R odoo:root
          {{- if .Values.volumePermissions.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.volumePermissions.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.volumePermissions.resources }}
          resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- else if ne .Values.volumePermissions.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.volumePermissions.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: odoo-data
              mountPath: /bitnami/odoo
              {{- if .Values.persistence.subPath }}
              subPath: {{ .Values.persistence.subPath }}
              {{- end }}
        {{- end }}
        {{- if .Values.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
      containers:
        - name: {{ template "common.names.fullname" . }}
          image: {{ template "odoo.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- 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 }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.image.debug | quote }}
            - name: ALLOW_EMPTY_PASSWORD
              value: {{ ternary "yes" "no" .Values.allowEmptyPassword | quote }}
            - name: ODOO_DATABASE_HOST
              value: {{ template "odoo.databaseHost" . }}
            - name: ODOO_DATABASE_PORT_NUMBER
              value: {{ template "odoo.databasePort" . }}
            - name: ODOO_DATABASE_NAME
              value: {{ template "odoo.databaseName" . }}
            - name: ODOO_DATABASE_FILTER
              value: {{ .Values.odooDatabaseFilter | quote }}
            - name: ODOO_DATABASE_USER
              value: {{ template "odoo.databaseUser" . }}
            {{- if .Values.usePasswordFiles }}
            - name: ODOO_DATABASE_PASSWORD_FILE
              value: {{ printf "/opt/bitnami/odoo/secrets/%s" (include "odoo.databaseSecretPasswordKey" .) }}
            {{- else }}
            - name: ODOO_DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "odoo.databaseSecretName" . }}
                  key: {{ include "odoo.databaseSecretPasswordKey" . }}
            {{- end }}
            {{- if and (not .Values.postgresql.enabled) .Values.externalDatabase.create }}
            - name: POSTGRESQL_CLIENT_DATABASE_HOST
              value: {{ template "odoo.databaseHost" . }}
            - name: POSTGRESQL_CLIENT_DATABASE_PORT_NUMBER
              value: {{ template "odoo.databasePort" . }}
            - name: POSTGRESQL_CLIENT_POSTGRES_USER
              value: {{ .Values.externalDatabase.postgresqlPostgresUser }}
            {{- if .Values.usePasswordFiles }}
            - name: POSTGRESQL_CLIENT_POSTGRES_PASSWORD_FILE
              value: {{ printf "/opt/bitnami/odoo/secrets/%s" (include "odoo.databaseSecretPostgresPasswordKey" .) }}
            {{- else }}
            - name: POSTGRESQL_CLIENT_POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "odoo.databaseSecretName" . }}
                  key: {{ include "odoo.databaseSecretPostgresPasswordKey" . }}
            {{- end }}
            - name: POSTGRESQL_CLIENT_CREATE_DATABASE_NAME
              value: {{ template "odoo.databaseName" . }}
            - name: POSTGRESQL_CLIENT_CREATE_DATABASE_USERNAME
              value: {{ template "odoo.databaseUser" . }}
            {{- if .Values.usePasswordFiles }}
            - name: POSTGRESQL_CLIENT_CREATE_DATABASE_PASSWORD_FILE
              value: {{ printf "/opt/bitnami/odoo/secrets/%s" (include "odoo.databaseSecretPasswordKey" .) }}
            {{- else }}
            - name: POSTGRESQL_CLIENT_CREATE_DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "odoo.databaseSecretName" . }}
                  key: {{ include "odoo.databaseSecretPasswordKey" . }}
            {{- end }}
            {{- end }}
            - name: ODOO_EMAIL
              value: {{ .Values.odooEmail | quote }}
            {{- if .Values.usePasswordFiles }}
            - name: ODOO_PASSWORD_FILE
              value: "/opt/bitnami/odoo/secrets/odoo-password"
            {{- else }}
            - name: ODOO_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "odoo.secretName" . }}
                  key: odoo-password
            {{- end }}
            - name: ODOO_SKIP_BOOTSTRAP
              value: {{ ternary "yes" "no" .Values.odooSkipInstall | quote }}
            - name: ODOO_LOAD_DEMO_DATA
              value: {{ ternary "yes" "no" .Values.loadDemoData | quote }}
            {{- if .Values.withoutDemo }}
            - name: WITHOUT_DEMO
              value: {{ .Values.withoutDemo | quote }}
            {{- end }}
            {{- if .Values.smtpHost }}
            - name: ODOO_SMTP_HOST
              value: {{ .Values.smtpHost | quote }}
            {{- end }}
            {{- if .Values.smtpPort }}
            - name: ODOO_SMTP_PORT_NUMBER
              value: {{ .Values.smtpPort | quote }}
            {{- end }}
            {{- if .Values.smtpUser }}
            - name: ODOO_SMTP_USER
              value: {{ .Values.smtpUser | quote }}
            {{- end }}
            {{- if or .Values.smtpPassword .Values.smtpExistingSecret }}
            {{- if .Values.usePasswordFiles }}
            - name: ODOO_SMTP_PASSWORD_FILE
              value: "/opt/bitnami/odoo/secrets/smtp-password"
            {{- else }}
            - name: ODOO_SMTP_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "odoo.smtpSecretName" . }}
                  key: smtp-password
            {{- end }}
            {{- end }}
            {{- if .Values.smtpProtocol }}
            - name: ODOO_SMTP_PROTOCOL
              value: {{ .Values.smtpProtocol | quote }}
            {{- end }}
            {{- if .Values.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.containerPorts.http }}
            {{- if .Values.extraContainerPorts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraContainerPorts "context" $) | nindent 12 }}
            {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.livenessProbe "enabled" "path") "context" $) | nindent 12 }}
            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: {{- include "common.tplvalues.render" (dict "value" (omit .Values.readinessProbe "enabled" "path") "context" $) | nindent 12 }}
            httpGet:
              path: {{ .Values.readinessProbe.path }}
              port: http
          {{- end }}
          {{- if .Values.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.startupProbe "enabled" "path") "context" $) | nindent 12 }}
            httpGet:
              path: {{ .Values.startupProbe.path }}
              port: http
          {{- end }}
          {{- 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 }}
          volumeMounts:
            - name: odoo-data
              mountPath: /bitnami/odoo
              {{- if .Values.persistence.subPath }}
              subPath: {{ .Values.persistence.subPath }}
              {{- end }}
            {{- if .Values.usePasswordFiles }}
            - name: odoo-secrets
              mountPath: /opt/bitnami/odoo/secrets
            {{- end }}
            {{- if .Values.customPostInitScripts }}
            - mountPath: /docker-entrypoint-init.d
              name: custom-postinit
            {{- end }}
            {{- if .Values.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: odoo-data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ (tpl .Values.persistence.existingClaim $) | default (include "common.names.fullname" .) }}
          {{- else }}
          emptyDir: {}
          {{- end }}
        {{- if .Values.usePasswordFiles }}
        - name: odoo-secrets
          projected:
            sources:
              - secret:
                  name: {{ include "odoo.secretName" . }}
              - secret:
                  name: {{ include "odoo.databaseSecretName" . }}
              {{- if or .Values.smtpPassword .Values.smtpExistingSecret }}
              - secret:
                  name: {{ template "odoo.smtpSecretName" . }}
              {{- end }}
        {{- end }}
        {{- if .Values.customPostInitScripts }}
        - name: custom-postinit
          configMap:
            name: {{ printf "%s-postinit" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
            defaultMode: 0755
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
