{{- if .Values.service.enabled -}}
apiVersion: v1
kind: Service
metadata:
  name: {{ include "redis.fullname" . }}
  labels:
    {{- include "redis.labels" . | nindent 4 }}
    {{- with .Values.service.labels }}
    {{ toYaml . | indent 4 }}
    {{- end }}
  {{- with .Values.service.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.service.type }}
  {{- if and (eq .Values.service.type "LoadBalancer") .Values.service.loadBalancerIP }}
  loadBalancerIP: {{ .Values.service.loadBalancerIP }}
  {{- end }}
  {{- if and (eq .Values.service.type "LoadBalancer") .Values.service.loadBalancerSourceRanges }}
  loadBalancerSourceRanges:
    {{- with .Values.service.loadBalancerSourceRanges }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
  {{- end }}
  {{- if and (eq .Values.service.type "ClusterIP") .Values.service.clusterIP }}
  clusterIP: {{ .Values.service.clusterIP }}
  {{- end }}
  ports:
    {{- range $key, $val := .Values.ports }}
    {{- if $val.enabled }}
    - name: {{ $key | quote }}
      port: {{ $val.port }}
      targetPort: {{ $key | quote }}
      {{- if and (eq $.Values.service.type "NodePort") $val.nodePort }}
      nodePort: {{ $val.nodePort }}
      {{- end }}
      protocol: {{ default "TCP" $val.protocol | quote }}
    {{- end }}
    {{- end }}
  selector:
    {{- include "redis.selectorLabels" . | nindent 4 }}
{{- end }}
