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

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "common.names.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: client
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.client.replicaCount }}
  {{- if .Values.client.updateStrategy }}
  strategy: {{- toYaml .Values.client.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.client.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: client
  template:
    metadata:
      {{- if .Values.client.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.client.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: client
    spec:
      serviceAccountName: {{ template "appsmith.serviceAccountName" . }}
      {{- include "appsmith.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.client.automountServiceAccountToken }}
      {{- if .Values.client.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.client.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.client.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.client.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.client.podAffinityPreset "component" "client" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.client.podAntiAffinityPreset "component" "client" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.client.nodeAffinityPreset.type "key" .Values.client.nodeAffinityPreset.key "values" .Values.client.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.client.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.client.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.client.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.client.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.client.priorityClassName }}
      priorityClassName: {{ .Values.client.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.client.schedulerName }}
      schedulerName: {{ .Values.client.schedulerName | quote }}
      {{- end }}
      {{- if .Values.client.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.client.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.client.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.client.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.client.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.client.terminationGracePeriodSeconds }}
      {{- end }}
      initContainers:
        - name: preserve-logs-symlinks
          image: {{ include "appsmith.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.client.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.client.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.client.resources }}
          resources: {{- toYaml .Values.client.resources | nindent 12 }}
          {{- else if ne .Values.client.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.client.resourcesPreset) | nindent 12 }}
          {{- end }}
          command:
            - /bin/bash
          args:
            - -ec
            - |
              #!/bin/bash
              . /opt/bitnami/scripts/libfs.sh
              . /opt/bitnami/scripts/liblog.sh

              info "Copying the nginx symlinks"
              # We copy the logs folder because it has symlinks to stdout and stderr
              if ! is_dir_empty /opt/bitnami/nginx/logs; then
                cp -r /opt/bitnami/nginx/logs /emptydir/nginx-logs-dir
              fi
              info "Copy operation completed"
          volumeMounts:
            - name: empty-dir
              mountPath: /emptydir
        {{- if not .Values.diagnosticMode.enabled }}
        {{- include "appsmith.waitForRTSInitContainer" . | nindent 8 }}
        {{- include "appsmith.waitForBackendInitContainer" . | nindent 8 }}
        {{- end }}
        {{- if .Values.client.initContainers }}
          {{- include "common.tplvalues.render" (dict "value" .Values.client.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: appsmith
          image: {{ template "appsmith.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.client.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.client.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.client.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.client.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.client.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.client.args "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" (or .Values.image.debug .Values.diagnosticMode.enabled) | quote }}
            - name: APPSMITH_MODE
              value: "client"
            - name: APPSMITH_API_HOST
              value: {{ include "appsmith.backend.fullname" . | quote }}
            - name: APPSMITH_API_PORT
              value: {{ .Values.backend.service.ports.http | quote }}
            - name: APPSMITH_RTS_HOST
              value: {{ include "appsmith.rts.fullname" . | quote }}
            - name: APPSMITH_RTS_PORT
              value: {{ .Values.rts.service.ports.http | quote }}
            - name: APPSMITH_UI_HTTP_PORT
              value: {{ .Values.client.containerPorts.http | quote }}
            - name: APPSMITH_UI_HTTPS_PORT
              value: {{ .Values.client.containerPorts.https | quote }}
            {{- if .Values.client.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.client.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.client.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.client.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.client.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.client.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.client.resources }}
          resources: {{- toYaml .Values.client.resources | nindent 12 }}
          {{- else if ne .Values.client.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.client.resourcesPreset) | nindent 12 }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.client.containerPorts.http }}
            - name: https
              containerPort: {{ .Values.client.containerPorts.https }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.client.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.client.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.client.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.client.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- if .Values.client.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.client.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.client.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.client.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /
              port: http
          {{- end }}
          {{- if .Values.client.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.client.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.client.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.client.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /
              port: http
          {{- end }}
          {{- end }}
          {{- if .Values.client.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.client.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /opt/bitnami/appsmith/conf
              subPath: app-conf-dir
            - name: empty-dir
              mountPath: /opt/bitnami/appsmith/logs
              subPath: app-logs-dir
            - name: empty-dir
              mountPath: /opt/bitnami/appsmith/tmp
              subPath: app-tmp-dir
            - name: empty-dir
              mountPath: /opt/bitnami/nginx/conf
              subPath: nginx-conf-dir
            - name: empty-dir
              mountPath: /certs
              subPath: nginx-certs-dir
            - name: empty-dir
              mountPath: /opt/bitnami/nginx/tmp
              subPath: nginx-tmp-dir
            - name: empty-dir
              mountPath: /opt/bitnami/nginx/logs
              subPath: nginx-logs-dir
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
          {{- if .Values.client.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.client.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.client.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.client.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        {{- if .Values.client.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.client.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
