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

{{- if eq .Values.architecture "replication" }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "keydb.replica.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: keydb
    app.kubernetes.io/component: replica
  {{- if or .Values.replica.service.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" (dict "values" (list .Values.replica.service.annotations .Values.commonAnnotations) "context" .) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.replica.service.type }}
  {{- if and .Values.replica.service.clusterIP (eq .Values.replica.service.type "ClusterIP") }}
  clusterIP: {{ .Values.replica.service.clusterIP }}
  {{- end }}
  {{- if .Values.replica.service.sessionAffinity }}
  sessionAffinity: {{ .Values.replica.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.replica.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.replica.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if or (eq .Values.replica.service.type "LoadBalancer") (eq .Values.replica.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.replica.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .Values.replica.service.type "LoadBalancer") (not (empty .Values.replica.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.replica.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and (eq .Values.replica.service.type "LoadBalancer") (not (empty .Values.replica.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.replica.service.loadBalancerIP }}
  {{- end }}
  ports:
    - name: tcp-keydb
      port: {{ .Values.replica.service.ports.keydb }}
      targetPort: keydb
      {{- if and (or (eq .Values.replica.service.type "NodePort") (eq .Values.replica.service.type "LoadBalancer")) (not (empty .Values.replica.service.nodePorts.keydb)) }}
      nodePort: {{ .Values.replica.service.nodePorts.keydb }}
      {{- else if eq .Values.replica.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.replica.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.replica.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" (dict "values" (list .Values.replica.podLabels .Values.commonLabels) "context" .) | fromYaml }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: keydb
    app.kubernetes.io/component: replica
{{- end }}
