# helm_charts_readreplicas-deployment.yaml
apiVersion: "apps/v1"
kind: Deployment
metadata:
  name: "{{ template "neo4j.replica.fullname" . }}"
  labels:
    app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
    app.kubernetes.io/instance: {{ .Release.Name | quote }}
    helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    app.kubernetes.io/name: {{ template "neo4j.name" . }}
    app.kubernetes.io/component: replica
spec:
{{- if not .Values.readReplica.autoscaling.enabled }}
  replicas: {{ .Values.readReplica.numberOfServers }}
{{- end }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ template "neo4j.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name | quote }}
      app.kubernetes.io/component: replica
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ template "neo4j.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name | quote }}
        app.kubernetes.io/component: replica
    spec:
      containers:
      - name: neo4j
        image: "{{ .Values.image }}:{{ .Values.imageTag }}"
        imagePullPolicy: "{{ .Values.imagePullPolicy }}"
        # Most pod config is factored into a different configMap, which is user overrideable.
        envFrom:
          - configMapRef:
              {{- if .Values.readReplica.configMap }}
              name: "{{ .Values.readReplica.configMap }}"
              {{- else }}
              name: {{ template "neo4j.replicaConfig.fullname" . }}
              {{- end }}
        env:
          {{- if .Values.authEnabled }}
          - name: NEO4J_SECRETS_PASSWORD
            valueFrom:
              secretKeyRef:
                name: {{ template "neo4j.secrets.fullname" . }}
                key: neo4j-password
          {{- end }}
        command:
          - "/bin/bash"
          - "-c"
          - |
            # Replicas advertise by bare IP, not hostname.  This is because deployments in kubernetes
            # don't provide good FQDNs. (https://github.com/kubernetes/kubernetes/issues/60789)
            # The FQDN advertisement address is necessary for the akka cluster formation in Neo4j to work,
            # so if you advertise with a bare local hostname or something invalid, the read replica will be
            # unable to join the raft group.
            export HOST=$(hostname -i)
            export NEO4J_causal__clustering_discovery__advertised__address=${NEO4J_causal__clustering_discovery__advertised__address:-$HOST}
            export NEO4J_dbms_default__advertised__address=${NEO4J_dbms_default__advertised__address:-$HOST}
            export NEO4J_dbms_connector_bolt_advertised__address=${NEO4J_dbms_connector_bolt_advertised__address:-$HOST}
            export NEO4J_dbms_connector_http_advertised__address=${NEO4J_dbms_connector_http_advertised__address:-$HOST}
            export NEO4J_dbms_connector_https_advertised__address=${NEO4J_dbms_connector_https_advertised__address:-$HOST}

            # These settings are *not* overrideable, because they must match the addresses the
            # core members see to avoid akka rejections.
            export NEO4J_causal__clustering_discovery__advertised__address=$HOST:5000
            export NEO4J_causal__clustering_transaction__advertised__address=$HOST:6000
            export NEO4J_causal__clustering_raft__advertised__address=$HOST:7000

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

            echo "Starting Neo4j READ_REPLICA on $HOST"
            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: plugins
          mountPath: /plugins
        resources:
{{ toYaml .Values.readReplica.resources | indent 10 }}
{{- if .Values.core.sidecarContainers }}
{{ toYaml .Values.core.sidecarContainers | indent 6 }}
{{- end }}
{{- if .Values.readReplica.initContainers }}
      initContainers:
{{ toYaml .Values.readReplica.initContainers | indent 6 }}
{{- end }}
{{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
{{- end }}
{{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
{{- end }}
{{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
{{- end }}
      volumes:
        - name: plugins
          emptyDir: {}
{{- if .Values.imagePullSecret }}
      imagePullSecrets:
      - name: {{ .Values.imagePullSecret }}
{{- end -}}