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

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