{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{/*
These services are necessary for avoiding long startups on IP changes. This behavior
is taken from the upstream scylla-operator
*/}}

{{- if .Values.service.internal.enabled }}
{{- $fullName := include "common.names.fullname" . }}
{{- $replicaCount := .Values.replicaCount | int }}
{{- $root := . }}
{{- range $i, $e := until $replicaCount }}
{{- $targetPod := printf "%s-%d" (printf "%s" $fullName) $i }}
{{- $_ := set $ "targetPod" $targetPod }}
apiVersion: v1
kind: Service
metadata:
  name: {{ printf "%s-%d-internal" $fullName $i }}
  namespace: {{ include "common.names.namespace" $root | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $root.Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: scylladb
    app.kubernetes.io/component: scylladb
    pod: {{ $targetPod }}
  {{- if or $root.Values.service.internal.annotations $root.Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list $root.Values.service.internal.annotations $root.Values.commonAnnotations ) "context" $root ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  type: ClusterIP
  publishNotReadyAddresses: true
  ports:
    - name: intra
      port: {{ $root.Values.containerPorts.intra }}
      targetPort: intra
    - name: tls
      port: {{ $root.Values.containerPorts.tls }}
      targetPort: tls
    - name: jmx
      port: {{ $root.Values.containerPorts.jmx }}
      targetPort: jmx
    - name: cql
      port: {{ $root.Values.containerPorts.cql }}
      targetPort: cql
    - name: cql-shard
      port: {{ $root.Values.containerPorts.cqlShard }}
      targetPort: cql
    {{- if $root.Values.service.internal.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" $root.Values.service.internal.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list $root.Values.podLabels $root.Values.commonLabels ) "context" $root ) }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: scylladb
    app.kubernetes.io/component: scylladb
    statefulset.kubernetes.io/pod-name: {{ $targetPod }}
---
  {{- end }}
{{- end }}