apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "nominatim.fullname" . }}
  namespace: {{ .Values.global.namespace }}
  labels:
    {{- include "nominatim.labels" . | nindent 4 }}
spec:
  serviceName: {{ include "nominatim.serviceName" . }}
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "nominatim.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "nominatim.selectorLabels" . | nindent 8 }}
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    spec:
      {{- with .Values.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
      - name: nominatim
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        {{- with .Values.securityContext }}
        securityContext:
          {{- toYaml . | nindent 10 }}
        {{- end }}
        ports:
        - containerPort: {{ .Values.service.port }}
          name: http
        volumeMounts:
        - name: nominatim-data
          mountPath: /var/lib/postgresql
        - name: nominatim-flatnode
          mountPath: /nominatim-flatnode
        env:
        - name: NOMINATIM_PBF_URL
          valueFrom:
            configMapKeyRef:
              name: {{ include "nominatim.configMapName" . }}
              key: NOMINATIM_PBF_URL
        - name: NOMINATIM_REPLICATION_URL
          valueFrom:
            configMapKeyRef:
              name: {{ include "nominatim.configMapName" . }}
              key: NOMINATIM_REPLICATION_URL
        - name: NOMINATIM_IMPORT_STYLE
          valueFrom:
            configMapKeyRef:
              name: {{ include "nominatim.configMapName" . }}
              key: NOMINATIM_IMPORT_STYLE
        - name: NOMINATIM_THREADS
          valueFrom:
            configMapKeyRef:
              name: {{ include "nominatim.configMapName" . }}
              key: NOMINATIM_THREADS
        - name: NOMINATIM_FLATNODE_FILE
          valueFrom:
            configMapKeyRef:
              name: {{ include "nominatim.configMapName" . }}
              key: NOMINATIM_FLATNODE_FILE
        {{- range $key, $value := .Values.env }}
        - name: {{ $key }}
          value: {{ $value | quote }}
        {{- end }}
        resources:
          {{- toYaml .Values.resources | nindent 10 }}
        {{- if .Values.probes.liveness.enabled }}
        livenessProbe:
          httpGet:
            path: {{ .Values.probes.liveness.path }}
            port: {{ .Values.probes.liveness.port }}
          initialDelaySeconds: {{ .Values.probes.liveness.initialDelaySeconds }}
          periodSeconds: {{ .Values.probes.liveness.periodSeconds }}
          timeoutSeconds: {{ .Values.probes.liveness.timeoutSeconds }}
          failureThreshold: {{ .Values.probes.liveness.failureThreshold }}
        {{- end }}
        {{- if .Values.probes.readiness.enabled }}
        readinessProbe:
          httpGet:
            path: {{ .Values.probes.readiness.path }}
            port: {{ .Values.probes.readiness.port }}
          initialDelaySeconds: {{ .Values.probes.readiness.initialDelaySeconds }}
          periodSeconds: {{ .Values.probes.readiness.periodSeconds }}
          timeoutSeconds: {{ .Values.probes.readiness.timeoutSeconds }}
          failureThreshold: {{ .Values.probes.readiness.failureThreshold }}
        {{- end }}
      volumes:
      - name: nominatim-data
        persistentVolumeClaim:
          claimName: {{ include "nominatim.dataPvcName" . }}
      - name: nominatim-flatnode
        persistentVolumeClaim:
          claimName: {{ include "nominatim.flatnodePvcName" . }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
