apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "manticoresearch.fullname" . }}-worker
  labels:
    name: {{ include "manticoresearch.fullname" . }}-worker
    {{- include "manticoresearch.labels" . | nindent 4 }}
    label: "{{ include "manticoresearch.fullname" . }}-worker"
spec:
  replicas: {{ .Values.worker.replicaCount }}
  selector:
    matchLabels:
      name: {{ include "manticoresearch.fullname" . }}-worker
  serviceName: {{ include "manticoresearch.fullname" . }}-worker-svc
  template:
    metadata:
      {{- if or .Values.exporter.enabled .Values.podAnnotations }}
      annotations:
        {{- if .Values.exporter.enabled }}
        {{- toYaml .Values.exporter.annotations | nindent 8 }}
        {{- end }}
        {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- end }}
      labels:
        name: "{{ include "manticoresearch.fullname" . }}-worker"
        label: "manticore-worker"
        {{- include "manticoresearch.selectorLabels" . | nindent 8 }}
    spec:
      serviceAccountName: {{ include "manticoresearch.serviceAccountName" . }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
      - name: {{ .Chart.Name }}-worker
        securityContext:
          {{- toYaml .Values.securityContext | nindent 12 }}
        image: {{ .Values.worker.image.repository }}:{{ .Values.worker.image.tag | default .Chart.AppVersion }}
        imagePullPolicy: {{ .Values.worker.image.pullPolicy }}
        lifecycle:
          preStop:
            exec:
              command: [ "/bin/sh", "-c", "./shutdown.sh" ]
        env:
          - name: WORKER_LABEL
            value: "manticore-worker"
          - name: AUTO_ADD_TABLES_IN_CLUSTER
            value: "{{ .Values.worker.autoAddTablesInCluster }}"
          - name: CONFIGMAP_PATH
            value: {{ .Values.worker.config.path }}
          - name: MANTICORE_PORT
            value: "{{ .Values.worker.service.ql.port }}"
          - name: CLUSTER_NAME
            value: {{ .Values.worker.clusterName }}
          - name: BALANCER_URL
            value: {{ include "manticoresearch.fullname" . }}-balancer-svc:{{ .Values.balancer.service.ql.targetPort }}
          - name: WORKER_SERVICE
            value: {{ include "manticoresearch.fullname" . }}-worker-svc
        readinessProbe:
          tcpSocket:
            port: {{ .Values.worker.service.ql.port }}
          initialDelaySeconds: 5
          periodSeconds: 3
        livenessProbe:
          tcpSocket:
            port: {{ .Values.worker.service.ql.port }}
          initialDelaySeconds: 5
          periodSeconds: 3
        volumeMounts:
          - name: data
            mountPath: /var/lib/manticore/
          - name: config-volume
            mountPath: {{ .Values.worker.config.path }}
            subPath: manticore.conf
        resources:
          {{- toYaml .Values.resources | nindent 10 }}
      {{- if .Values.exporter.enabled }}
      - name: {{ .Chart.Name }}-exporter
        securityContext:
          {{- toYaml .Values.securityContext | nindent 12 }}
        image: "{{ .Values.exporter.image.repository }}:{{ .Values.exporter.image.tag | default .Chart.AppVersion }}"
        imagePullPolicy: {{ .Values.exporter.image.pullPolicy }}
        env:
          - name: MANTICORE_HOST
            valueFrom:
              fieldRef:
                fieldPath: status.podIP
          - name: MANTICORE_PORT
            value: "{{ .Values.balancer.service.ql.targetPort }}"
        livenessProbe:
          httpGet:
            path: /health
            port: {{ index .Values.exporter.annotations "prometheus.io/port" }}
          initialDelaySeconds: 3
          periodSeconds: 3
        resources:
          {{- toYaml .Values.resources | nindent 10 }}
      {{- end }}
      volumes:
        - name: config-volume
          configMap:
            name: {{ include "manticoresearch.fullname" . }}-worker-config
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: {{ .Values.worker.volume.size }}
