apiVersion: v1
kind: Service
metadata:
  name: {{ include "common.names.fullname" . }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
{{- if .Values.service.labels }}
{{- include "common.tplvalues.render" (dict "value" .Values.service.labels "context" $) | nindent 4 }}
{{- end }}
{{- if or (and .Values.metrics.enabled .Values.metrics.service.annotations) .Values.service.annotations }}
  annotations:
  {{- if and .Values.metrics.enabled .Values.metrics.service.annotations }}
    {{- tpl (toYaml .Values.metrics.service.annotations) $ | nindent 4 }}
  {{- end }}
  {{- if .Values.service.annotations }}
    {{- tpl (toYaml .Values.service.annotations) $ | nindent 4 }}
  {{- end }}
{{- end }}

spec:
  type: {{ .Values.service.type }}
  {{- if eq .Values.service.type "LoadBalancer" }}
  {{- if .Values.service.loadBalancerIP }}
  loadBalancerIP: {{ .Values.service.loadBalancerIP }}
  {{- end }}
  {{- end }}
  {{- if (or (eq .Values.service.type "LoadBalancer") (eq .Values.service.type "NodePort")) }}
  externalTrafficPolicy: {{ .Values.service.externalTrafficPolicy | quote }}
  {{- end }}
  ports:
    - name: http
      port: {{ .Values.service.port }}
      targetPort: http
      {{- if (and (eq .Values.service.type "NodePort") (not (empty .Values.service.nodePort)))}}
      nodePort: {{ .Values.service.nodePort }}
      {{- else if eq .Values.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
{{- if .Values.service.extraPorts }}
  {{- tpl (toYaml .Values.service.extraPorts) $ | nindent 4 }}
{{- end }}
  selector: {{- include "common.labels.matchLabels" . | nindent 4 }}
