apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "datalore.fullname" . }}
  labels:
    {{- include "datalore.labels" . | nindent 4 }}
spec:
  replicas: 1
  serviceName: {{ include "datalore.fullname" . }}
  selector:
    matchLabels:
      {{- include "datalore.selectorLabels" . | nindent 6 }}
  {{- with .Values.volumeClaimTemplates }}
  volumeClaimTemplates:
    {{- toYaml . | nindent 4 }}
  {{- end }}
  template:
    metadata:
    {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      labels:
        {{- include "datalore.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "datalore.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.securityContext | nindent 8 }}
      containers:
        - name: datalore
          securityContext:
            {{- toYaml .Values.dataloreSecurityContext | nindent 12 }}
          image: "{{ .Values.serverImage.repository }}:{{ include "datalore.serverImage.version" . }}"
          imagePullPolicy: {{ .Values.serverImage.pullPolicy }}
          ports:
            - name: http
              containerPort: 8080
            - name: http-internal
              containerPort: 8081
            - name: agents-manager
              containerPort: {{ .Values.agentsManagerPort }}
            - name: computation
              containerPort: {{ .Values.computationPort }}
            - name: external-server
              containerPort: {{ .Values.externalServerPort }}
          livenessProbe:
            initialDelaySeconds: 120
            httpGet:
              path: /health
              port: http
          env:
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "datalore.databaseSecretName" . }}
                  key: {{ quote .Values.databaseSecret.key }}
            - name: DATABASES_CONNECTION_CHECKER_K8S_YAML
              value: file:///opt/datalore/configs/databases/connection_checker.yaml
            - name: DATABASES_INTROSPECTION_K8S_YAML
              value: file:///opt/datalore/configs/databases/introspection.yaml
            - name: DATABASES_NAMESPACES_LOADER_K8S_YAML
              value: file:///opt/datalore/configs/databases/namespaces_loader.yaml
            - name: SQL_SESSION_K8S_YAML
              value: file:///opt/datalore/configs/databases/sql_session.yaml
            - name: GIT_FETCH_TASK_K8S_YAML
              value: file:///opt/datalore/configs/git/fetch_job.yaml
            - name: GIT_CLONE_TASK_K8S_YAML
              value: file:///opt/datalore/configs/git/clone_job.yaml
            - name: GIT_BACKUP_TASK_K8S_YAML
              value: file:///opt/datalore/configs/git/backup_job.yaml
            - name: GIT_DIFF_TASK_K8S_YAML
              value: file:///opt/datalore/configs/git/diff_job.yaml
            - name: DATALORE_NAMESPACE
              value: {{ .Release.Namespace }}
            - name: DATALORE_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            {{- if .Values.plansConfig }}
            - name: DATALORE_PLANS_CONFIGURATION
              value: file:///opt/datalore/configs/plans_config.yaml
            {{- end }}
            {{- if .Values.sqlCellsApiHost }}
            - name: SQL_CELLS_API_HOST
              value: {{ .Values.sqlCellsApiHost }}
            {{- end }}
            {{- if .Values.sqlCellsApiPort }}
            - name: SQL_CELLS_API_PORT
              value: {{ quote .Values.sqlCellsApiPort }}
            {{- end }}
            {{- if .Values.dataloreEnvRaw }}
            {{- toYaml .Values.dataloreEnvRaw | nindent 12 }}
            {{- end }}
          envFrom:
            - secretRef:
                name: {{ include "datalore.fullname" . }}-user-env
            {{- if .Values.dataloreEnvExternalSecret }}
            - secretRef:
                name: {{ .Values.dataloreEnvExternalSecret }}
            {{- end }}
          volumeMounts:
            - mountPath: /opt/datalore/configs/agents-config.yaml
              name: agents-config
              subPath: agents-config.yaml
            - mountPath: /opt/datalore/configs/databases
              name: databases-configs
            - mountPath: /opt/datalore/configs/git
              name: git-configs
            {{- if .Values.logbackConfig }}
            - mountPath: /opt/datalore/tomcat/conf/logback.xml
              name: logback-config
              subPath: logback.xml
            {{- end }}
            - mountPath: /opt/data
              name: storage
            {{- if .Values.customEnvs }}
            - mountPath: /opt/datalore/configs/environment_info
              name: envs
            {{- end }}
            {{- if .Values.plansConfig }}
            - mountPath: /opt/datalore/configs/plans_config.yaml
              name: plans-config
              subPath: plans_config.yaml
            {{- end }}
            {{- if .Values.volumeMounts }}
            {{- toYaml .Values.volumeMounts | nindent 12 }}
            {{- end }}
          resources:
            {{- toYaml .Values.dataloreResources | nindent 12 }}
        {{- if .Values.internalDatabase }}
        - name: postgres
          securityContext:
            {{- toYaml .Values.postgresSecurityContext | nindent 12 }}
          image: "{{ .Values.postgresImage.repository }}:{{ .Values.postgresImage.version }}"
          imagePullPolicy: {{ .Values.postgresImage.pullPolicy }}
          ports:
            - containerPort: 5432
          env:
            - name: PGDATA
              value: /var/lib/postgresql/data/db-data
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "datalore.databaseSecretName" . }}
                  key: {{ quote .Values.databaseSecret.key }}
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgresql-data
          resources:
            {{- toYaml .Values.postgresResources | nindent 12 }}
        {{- end }}
      volumes:
        - name: agents-config
          configMap:
            name: {{ include "datalore.fullname" . }}-agents-config
        {{- if .Values.logbackConfig }}
        - name: logback-config
          configMap:
            name: {{ include "datalore.fullname" . }}-logback-config
        {{- end }}
        {{- if .Values.customEnvs }}
        - name: envs
          configMap:
            name: {{ include "datalore.fullname" . }}-envs-config
        {{- end }}
        {{- if .Values.plansConfig }}
        - name: plans-config
          configMap:
            name: {{ include "datalore.fullname" . }}-plans-config
        {{- end }}
        - name: databases-configs
          configMap:
            name: {{ include "datalore.fullname" . }}-databases-config
        - name: git-configs
          configMap:
            name: {{ include "datalore.fullname" . }}-git-config
        {{- if .Values.volumes }}
        {{- toYaml .Values.volumes | nindent 8 }}
        {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
