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

apiVersion: v1
kind: Service
metadata:
  name: {{ printf "%s-headless" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: etcd
  annotations:
    service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
    {{- if or .Values.service.headless.annotations .Values.commonAnnotations }}
    {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.service.headless.annotations .Values.commonAnnotations ) "context" . ) }}
    {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
    {{- end }}
spec:
  type: ClusterIP
  clusterIP: None
  publishNotReadyAddresses: true
  ports:
    {{- if .Values.service.clientPortNameOverride }}
    {{- if .Values.auth.client.secureTransport }}
    - name: {{ .Values.service.clientPortNameOverride }}-ssl
    {{- else }}
    - name: {{ .Values.service.clientPortNameOverride }}
    {{- end }}
    {{- else }}
    - name: client
    {{- end }}
      port: {{ .Values.containerPorts.client }}
      targetPort: client
    {{- if .Values.service.peerPortNameOverride }}
    {{- if .Values.auth.peer.secureTransport }}
    - name: {{ .Values.service.peerPortNameOverride }}-ssl
    {{- else }}
    - name: {{ .Values.service.peerPortNameOverride }}
    {{- end }}
    {{- else }}
    - name: peer
    {{- end }}
      port: {{ .Values.containerPorts.peer }}
      targetPort: peer
    {{- if .Values.metrics.useSeparateEndpoint }}
    {{- if .Values.service.metricsPortNameOverride }}
    - name: {{ .Values.service.metricsPortNameOverride }}
    {{- else }}
    - name: metrics
    {{- end }}
      port: {{ .Values.containerPorts.metrics }}
      targetPort: metrics
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.podLabels .Values.commonLabels ) "context" . ) }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: etcd
