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

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "appsmith.rts.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: rts
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.rts.replicaCount }}
  {{- if .Values.rts.updateStrategy }}
  strategy: {{- toYaml .Values.rts.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.rts.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: rts
  template:
    metadata:
      {{- if .Values.rts.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.rts.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: rts
    spec:
      serviceAccountName: {{ template "appsmith.serviceAccountName" . }}
      {{- include "appsmith.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.rts.automountServiceAccountToken }}
      {{- if .Values.rts.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.rts.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.rts.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.rts.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.rts.podAffinityPreset "component" "rts" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.rts.podAntiAffinityPreset "component" "rts" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.rts.nodeAffinityPreset.type "key" .Values.rts.nodeAffinityPreset.key "values" .Values.rts.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.rts.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.rts.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.rts.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.rts.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.rts.priorityClassName }}
      priorityClassName: {{ .Values.rts.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.rts.schedulerName }}
      schedulerName: {{ .Values.rts.schedulerName | quote }}
      {{- end }}
      {{- if .Values.rts.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.rts.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.rts.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.rts.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.rts.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.rts.terminationGracePeriodSeconds }}
      {{- end }}
      initContainers:
        {{- if not .Values.diagnosticMode.enabled }}
        {{- include "appsmith.waitForDBInitContainer" . | nindent 8 }}
        {{- include "appsmith.waitForBackendInitContainer" . | nindent 8 }}
        {{- end }}
        {{- if .Values.rts.initContainers }}
          {{- include "common.tplvalues.render" (dict "value" .Values.rts.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: appsmith
          image: {{ template "appsmith.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.rts.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.rts.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.rts.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.rts.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.rts.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.rts.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: "rts"
            - name: APPSMITH_DATABASE_HOST
              value: {{ include "appsmith.mongodb.hosts" . | quote }}
            - name: APPSMITH_DATABASE_PORT_NUMBER
              value: {{ include "appsmith.mongodb.port" . | quote }}
            {{- if .Values.usePasswordFiles }}
            - name: APPSMITH_DATABASE_PASSWORD_FILE
              value: {{ printf "/secrets/%s" (include "appsmith.mongodb.secretKey" .) }}
            {{- else }}
            - name: APPSMITH_DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "appsmith.mongodb.secretName" . }}
                  key: {{ include "appsmith.mongodb.secretKey" . }}
            {{- end }}
            - name: APPSMITH_DATABASE_USER
              value: {{ ternary (index .Values.mongodb.auth.usernames 0) .Values.externalDatabase.username .Values.mongodb.enabled | quote }}
            - name: APPSMITH_DATABASE_NAME
              value: {{ ternary (index .Values.mongodb.auth.databases 0) .Values.externalDatabase.database .Values.mongodb.enabled | quote }}
            - name: APPSMITH_API_HOST
              value: {{ include "appsmith.backend.fullname" . | quote }}
            - name: APPSMITH_API_PORT
              value: {{ .Values.backend.service.ports.http | quote }}
            - name: APPSMITH_RTS_PORT
              value: {{ .Values.rts.containerPorts.http | quote }}
            {{- if .Values.rts.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.rts.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.rts.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.rts.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.rts.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.rts.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.rts.resources }}
          resources: {{- toYaml .Values.rts.resources | nindent 12 }}
          {{- else if ne .Values.rts.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.rts.resourcesPreset) | nindent 12 }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.rts.containerPorts.http }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.rts.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.rts.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.rts.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.rts.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- if .Values.rts.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.rts.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.rts.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.rts.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /rts-api/v1/health-check
              port: http
          {{- end }}
          {{- if .Values.rts.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.rts.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.rts.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.rts.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /rts-api/v1/health-check
              port: http
          {{- end }}
          {{- end }}
          {{- if .Values.rts.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.rts.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.usePasswordFiles }}
            - name: appsmith-secrets
              mountPath: /secrets
            {{- end }}
          {{- if .Values.rts.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.rts.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.rts.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.rts.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        {{- if .Values.usePasswordFiles }}
        - name: appsmith-secrets
          projected:
            sources:
              - secret:
                  name:  {{ include "appsmith.mongodb.secretName" . }}
                  items:
                    # Only copy database password
                    - key: {{ include "appsmith.mongodb.secretKey" . }}
                      path: {{ include "appsmith.mongodb.secretKey" . }}
        {{- end }}
        {{- if .Values.rts.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.rts.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
