apiVersion: v1
kind: Service
metadata:
  name: {{ template "rabbitmq.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    app: {{ template "rabbitmq.name" . }}
    chart: {{ template "rabbitmq.chart" . }}
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
{{- if .Values.service.annotations }}
  annotations:
{{ toYaml .Values.service.annotations | indent 4 }}
{{- end }}
spec:
  type: {{ .Values.service.type }}
{{- if and (eq .Values.service.type "LoadBalancer") .Values.service.loadBalancerSourceRanges }}
  loadBalancerSourceRanges:
  {{ with .Values.service.loadBalancerSourceRanges }}
{{ toYaml . | indent 4 }}
{{- end }}
{{- end }}
  {{- if (and (eq .Values.service.type "LoadBalancer") (not (empty .Values.service.loadBalancerIP))) }}
  loadBalancerIP: {{ .Values.service.loadBalancerIP }}
  {{- end }}
  ports:
  - name: epmd
    port: 4369
    targetPort: epmd
    {{- if (eq .Values.service.type "ClusterIP") }}
    nodePort: null
    {{- end }}
  - name: amqp
    port: {{ .Values.service.port }}
    targetPort: amqp
    {{- if (eq .Values.service.type "ClusterIP") }}
    nodePort: null
    {{- else if (and (eq .Values.service.type "NodePort") (not (empty .Values.service.nodePort))) }}
    nodePort: {{ .Values.service.nodePort }}
    {{- end }}
  {{- if .Values.rabbitmq.tls.enabled }}
  - name: amqp-ssl
    port: {{ .Values.service.tlsPort }}
    targetPort: amqp-ssl
    {{- if (and (eq .Values.service.type "NodePort") (not (empty .Values.service.nodeTlsPort))) }}
    nodePort: {{ .Values.service.nodeTlsPort }}
    {{- end }}
  {{- end }}
  - name: dist
    port: {{ .Values.service.distPort }}
    targetPort: dist
    {{- if (eq .Values.service.type "ClusterIP") }}
    nodePort: null
    {{- end }}
  - name: stats
    port: {{ .Values.service.managerPort }}
    targetPort: stats
    {{- if (eq .Values.service.type "ClusterIP") }}
    nodePort: null
    {{- end }}
{{- if .Values.metrics.enabled }}
  - name: metrics
    port: {{ .Values.metrics.port }}
    targetPort: metrics
    {{- if (eq .Values.service.type "ClusterIP") }}
    nodePort: null
    {{- end }}
{{- end }}
{{- if .Values.service.extraPorts }}
{{ toYaml .Values.service.extraPorts | indent 2 }}
{{- end }}
  selector:
    app: {{ template "rabbitmq.name" . }}
    release: "{{ .Release.Name }}"
