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

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