apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "datalore.fullname" . }}-databases-config
  labels:
    {{- include "datalore.labels" . | nindent 4 }}
data:
  connection_checker.yaml: |-
{{- if .Values.connectionChecker }}
{{- tpl (toYaml .Values.connectionChecker) . | nindent 4 }}
{{- else }}
    apiVersion: batch/v1
    kind: Job
    metadata:
      name: connection-check
      labels:
        datalore-databases: connection-check
    spec:
      backoffLimit: 1
      activeDeadlineSeconds: 120
      ttlSecondsAfterFinished: 600
      template:
        metadata:
          labels:
            datalore-databases: connection-check
        spec:
          serviceAccountName: {{ .Values.databasesServiceAccountName }}
          restartPolicy: Never
          containers:
            - name: connection-check
              image: {{ .Values.databaseCommandImage.repository }}:{{ include "datalore.databaseCommandImage.version" . }}
              imagePullPolicy: {{ .Values.databaseCommandImage.pullPolicy }}
              securityContext:
                runAsUser: 5000
              env:
                {{- range $k, $v := .Values.databasesPodEnv }}
                - name: {{ $k }}
                  value: {{ $v | quote }}
                {{- end }}
          {{- with .Values.agentsNodeSelector }}
          nodeSelector:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.agentsAffinity }}
          affinity:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.agentsTolerations }}
          tolerations:
            {{- toYaml . | nindent 12 }}
          {{- end }}
{{- end }}
  introspection.yaml: |-
{{- if .Values.introspection }}
{{- tpl (toYaml .Values.introspection) . | nindent 4 }}
{{- else }}
    apiVersion: batch/v1
    kind: Job
    metadata:
      name: introspection
      labels:
        datalore-databases: introspection
    spec:
      backoffLimit: 1
      activeDeadlineSeconds: 1200
      ttlSecondsAfterFinished: 6000
      template:
        metadata:
          labels:
            datalore-databases: introspection
        spec:
          serviceAccountName: {{ .Values.databasesServiceAccountName }}
          restartPolicy: Never
          containers:
            - name: introspection
              image: {{ .Values.databaseCommandImage.repository }}:{{ include "datalore.databaseCommandImage.version" . }}
              imagePullPolicy: {{ .Values.databaseCommandImage.pullPolicy }}
              securityContext:
                runAsUser: 5000
              env:
                {{- range $k, $v := .Values.databasesPodEnv }}
                - name: {{ $k }}
                  value: {{ $v | quote }}
                {{- end }}
          {{- with .Values.agentsNodeSelector }}
          nodeSelector:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.agentsAffinity }}
          affinity:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.agentsTolerations }}
          tolerations:
            {{- toYaml . | nindent 12 }}
          {{- end }}
{{- end }}
  namespaces_loader.yaml: |-
{{- if .Values.namespacesLoader }}
{{- tpl (toYaml .Values.namespacesLoader) . | nindent 4 }}
{{- else }}
    apiVersion: batch/v1
    kind: Job
    metadata:
      name: namespaces-loader
      labels:
        datalore-databases: namespaces-loader
    spec:
      backoffLimit: 1
      activeDeadlineSeconds: 120
      ttlSecondsAfterFinished: 600
      template:
        metadata:
          labels:
            datalore-databases: namespaces-loader
        spec:
          serviceAccountName: {{ .Values.databasesServiceAccountName }}
          restartPolicy: Never
          containers:
            - name: connection-check
              image: {{ .Values.databaseCommandImage.repository }}:{{ include "datalore.databaseCommandImage.version" . }}
              imagePullPolicy: {{ .Values.databaseCommandImage.pullPolicy }}
              securityContext:
                runAsUser: 5000
              env:
                {{- range $k, $v := .Values.databasesPodEnv }}
                - name: {{ $k }}
                  value: {{ $v | quote }}
                {{- end }}
          {{- with .Values.agentsNodeSelector }}
          nodeSelector:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.agentsAffinity }}
          affinity:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.agentsTolerations }}
          tolerations:
            {{- toYaml . | nindent 12 }}
          {{- end }}
{{- end }}
  sql_session.yaml: |-
{{- if .Values.sqlSessionService }}
{{- tpl (toYaml .Values.sqlSessionService) . | nindent 4 }}
{{- else }}
    apiVersion: v1
    kind: Service
    metadata:
      name: sql-session-service
    spec:
      type: ClusterIP
      ports:
        - port: 8087
          targetPort: 8087
      selector:
        app: sql-session
{{- end }}
    ---
{{- if .Values.sqlSession }}
{{- tpl (toYaml .Values.sqlSession) . | nindent 4 }}
{{- else }}
    apiVersion: batch/v1
    kind: Job
    metadata:
      name: sql-session
      labels:
        app: sql-session
        datalore-databases: sql-session
    spec:
      backoffLimit: 1
      ttlSecondsAfterFinished: 600
      template:
        metadata:
          labels:
            datalore-databases: sql-session
        spec:
          serviceAccountName: {{ .Values.databasesServiceAccountName }}
          restartPolicy: Never
          containers:
            - name: sql-session
              image: {{ .Values.databaseCommandImage.repository }}:{{ include "datalore.databaseCommandImage.version" . }}
              imagePullPolicy: {{ .Values.databaseCommandImage.pullPolicy }}
              env:
                {{- range $k, $v := .Values.databasesPodEnv }}
                - name: {{ $k }}
                  value: {{ $v | quote }}
                {{- end }}
          {{- with .Values.agentsNodeSelector }}
          nodeSelector:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.agentsAffinity }}
          affinity:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.agentsTolerations }}
          tolerations:
            {{- toYaml . | nindent 12 }}
          {{- end }}
{{- end }}
