apiVersion: "apps/v1beta2"
kind: StatefulSet
metadata:
  name: "{{ template "neo4j.core.fullname" . }}"
spec:
  serviceName: {{ template "neo4j.fullname" . }}
  replicas: {{ .Values.core.numberOfServers }}
  selector:
    matchLabels:
      release: {{ .Release.Name | quote }}
      app: {{ template "neo4j.name" . }}
      component: core
  template:
    metadata:
      labels:
        heritage: {{ .Release.Service | quote }}
        release: {{ .Release.Name | quote }}
        chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
        app: {{ template "neo4j.name" . }}
        component: core
    spec:
      containers:
      - name: {{ template "neo4j.fullname" . }}
        image: "{{ .Values.image }}:{{ .Values.imageTag }}"
        imagePullPolicy: "{{ .Values.imagePullPolicy }}"
        env:
          - name: NEO4J_ACCEPT_LICENSE_AGREEMENT
            value: "{{ .Values.acceptLicenseAgreement }}"
          - name: NEO4J_dbms_mode
            value: CORE
          - name: NUMBER_OF_CORES
            value: "{{ .Values.core.numberOfServers }}"
          - name: AUTH_ENABLED
            value: "{{ .Values.authEnabled }}"
          - name: NEO4J_causal__clustering_discovery__type
            value: DNS
          - name: NEO4J_causal__clustering_initial__discovery__members
            value: "{{ template "neo4j.fullname" . }}.{{ .Release.Namespace }}.svc.cluster.local:5000"
          {{- if .Values.authEnabled }}
          - name: NEO4J_SECRETS_PASSWORD
            valueFrom:
              secretKeyRef:
                name: {{ template "neo4j.secrets.fullname" . }}
                key: neo4j-password
          {{- end }}
{{- if .Values.core.extraVars }}
{{ toYaml .Values.core.extraVars | indent 10 }}
{{- end }}
        command:
          - "/bin/bash"
          - "-c"
          - |
            export NEO4J_dbms_connectors_default__advertised__address=$(hostname -f)
            export NEO4J_causal__clustering_discovery__advertised__address=$(hostname -f):5000
            export NEO4J_causal__clustering_transaction__advertised__address=$(hostname -f):6000
            export NEO4J_causal__clustering_raft__advertised__address=$(hostname -f):7000

            if [ "${AUTH_ENABLED:-}" == "true" ]; then
              export NEO4J_AUTH="neo4j/${NEO4J_SECRETS_PASSWORD}"
            else
              export NEO4J_AUTH="none"
            fi

            exec /docker-entrypoint.sh "neo4j"
        ports:
        - containerPort: 5000
          name: discovery
        - containerPort: 7000
          name: raft
        - containerPort: 6000
          name: tx
        - containerPort: 7474
          name: browser
        - containerPort: 7687
          name: bolt
        volumeMounts:
        - name: datadir
          mountPath: "{{ .Values.core.persistentVolume.mountPath }}"
          {{- if .Values.core.persistentVolume.subPath }}
          subPath: {{ .Values.core.persistentVolume.subPath }}
          {{- end }}
        - name: plugins
          mountPath: /plugins
        resources:
{{ toYaml .Values.resources | indent 10 }}
{{- if .Values.core.sidecarContainers }}
{{ toYaml .Values.core.sidecarContainers | indent 6 }}
{{- end }}
{{- if .Values.imagePullSecret }}
      imagePullSecrets:
      - name: {{ .Values.imagePullSecret }}
{{- end -}}
{{- if .Values.core.initContainers }}
      initContainers:
{{ toYaml .Values.core.initContainers | indent 6 }}
{{- end }}
      volumes:
        - name: plugins
          emptyDir: {}
  volumeClaimTemplates:
    - metadata:
        name: datadir
        annotations:
        {{- if .Values.core.persistentVolume.annotations }}
{{ toYaml .Values.core.persistentVolume.annotations | indent 12 }}
        {{- end }}
      spec:
        accessModes:
          - ReadWriteOnce
      {{- if .Values.core.persistentVolume.storageClass }}
      {{- if (eq "-" .Values.core.persistentVolume.storageClass) }}
        storageClassName: ""
      {{- else }}
        storageClassName: "{{ .Values.core.persistentVolume.storageClass }}"
      {{- end }}
      {{- end }}
        resources:
          requests:
            storage: "{{ .Values.core.persistentVolume.size }}"
