apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ template "aerospike.fullname" . }}
  labels:
    app: {{ template "aerospike.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
    {{- with .Values.labels }}
    {{ toYaml . | nindent 4 }}
    {{- end }}
  {{- with .Values.annotations }}
  annotations:
    {{ toYaml . | nindent 4 }}
  {{- end }}
spec:
  serviceName: {{ template "aerospike.fullname" . }}
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "aerospike.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ template "aerospike.name" . }}
        release: {{ .Release.Name }}
      annotations:
        checksum/config: {{ .Values.confFile | sha256sum }}
    spec:
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      containers:
      - name: {{ template "aerospike.fullname" . }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        {{ if .Values.command }}
        command:
          {{ toYaml .Values.command | nindent 10 }}
        {{ end }}
        {{ if .Values.args }}
        args:
          {{ toYaml .Values.args | nindent 10 }}
        {{ end }}
        ports:
        - containerPort: 3000
          name: clients
        - containerPort: 3002
          name: mesh
        - containerPort: 3003
          name: info
        readinessProbe:
          tcpSocket:
              port: 3000
          initialDelaySeconds: 15
          timeoutSeconds: 1
        volumeMounts:
        - name: config-volume
          mountPath: /etc/aerospike
        {{- range $pv := .Values.persistentVolume }}
        - name: {{ $pv.name | quote }}
          mountPath: {{ $pv.mountPath | quote }}
        {{- end }}
        resources:
          {{ toYaml .Values.resources | nindent 10 }}
      {{ if .Values.metrics.enabled }}
      - name: {{ template "aerospike.fullname" . }}-metrics
        image: "{{ .Values.metrics.image.repository }}:{{ .Values.metrics.image.tag }}"
        ports:
        - containerPort: 9145
          name: metrics
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector:
        {{ toYaml .Values.nodeSelector | nindent 8 }}
      {{- end }}
      {{- if .Values.affinity }}
      affinity:
        {{ toYaml .Values.affinity | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations}}
      tolerations:
        {{ toYaml .Values.tolerations | nindent 8 }}
      {{- end }}
      volumes:
      - name: config-volume
        configMap:
          name: {{ template "aerospike.fullname" . }}
          items:
          - key: aerospike.conf
            path: aerospike.conf
      {{ if .Values.image.pullSecret }}
      imagePullSecrets:
      - name: {{ .Values.image.pullSecret }}
      {{- end }}
  volumeClaimTemplates:
  {{- range $pv := .Values.persistentVolume }}
  - metadata:
      name: {{ $pv.name | quote }}
    spec:
      {{ toYaml $pv.template | nindent 6 }}
  {{- end }}
