# This creates a discovery Service for each member in the core set, and ties to
# the use of the Neo4j discovery type "K8S" with the configured selectors.
# Done as many times as there are core machines, or just once if standalone.
#
# IMPORTANT - related to scaling, in order for the cluster to form correctly
# there must be one of these per core node.  As a result, we intentionally create
# too many of them in the causal cluster scenario to ensure that when pods scale
# up, there are extra discovery addresses to cover them.
##########################################################################
{{- $dot := . }}
{{- $servers := int .Values.core.numberOfServers }}
{{- $instances := .Values.core.discoveryService.instances }}
{{- if .Values.core.standalone }}
{{- $instances := .Values.core.discoveryService.standaloneOnly }}
{{- end }}
{{- range $index, $value := $instances }}
---
apiVersion: v1
kind: Service
metadata:
  name: discovery-{{ template "neo4j.fullname" $dot }}-{{ . }}
  labels:
    neo4j.com/coreindex: "{{ $index }}"
    neo4j.com/cluster: {{ template "neo4j.fullname" $dot }}
    neo4j.com/role: CORE
    neo4j.com/bolt: "false"
    neo4j.com/http: "false"
    app.kubernetes.io/managed-by: {{ $dot.Release.Service | quote }}
    app.kubernetes.io/instance: {{ $dot.Release.Name | quote }}
    helm.sh/chart: "{{ $dot.Chart.Name }}-{{ $dot.Chart.Version }}"
    app.kubernetes.io/name: {{ template "neo4j.name" $dot }}
    app.kubernetes.io/component: core
{{- with $dot.Values.core.discoveryService.labels }}
    {{ toYaml . | nindent 4 }}
{{- end }}
{{- with $dot.Values.core.discoveryService.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
{{- end }}
spec:
{{- if (or (eq $dot.Values.core.discoveryService.type "ClusterIP") (empty $dot.Values.core.discoveryService.type)) }}
  type: ClusterIP
  clusterIP: None
{{- else if eq $dot.Values.core.discoveryService.type "LoadBalancer" }}
  type: LoadBalancer
  {{- with $dot.Values.core.discoveryService.loadBalancerSourceRanges }}
  loadBalancerSourceRanges:
    {{ toYaml . | nindent 4 }}
  {{- end -}}
{{- else }}
  type: {{ $dot.Values.core.discoveryService.type }}
{{- end }}
  publishNotReadyAddresses: true
  ports:
    - name: tcp-discovery
      port: 5000
      targetPort: 5000
      protocol: TCP
    - name: tcp-transaction
      port: 6000
      targetPort: 6000
      protocol: TCP
    - name: tcp-raft
      port: 7000
      targetPort: 7000
      protocol: TCP
    - name: tcp-boltrouting
      port: 7688
      targetPort: 7688
      protocol: TCP
{{- if $dot.Values.metrics.prometheus.enabled }}
    - name: tcp-prometheus
      port: 2004
      targetPort: 2004
      protocol: TCP
{{- end }}
{{- if $dot.Values.metrics.graphite.enabled }}
    - name: tcp-graphite
      port: 2003
      targetPort: 2003
      protocol: TCP
{{- end }}
{{- if $dot.Values.metrics.jmx.enabled }}
    - name: tcp-jmx
      port: 3637
      targetPort: 3637
      protocol: TCP
{{- end }}
  selector:
    statefulset.kubernetes.io/pod-name: "{{ template "neo4j.fullname" $dot }}-core-{{ . }}"
{{- end }}
