apiVersion: v1
kind: Service
metadata:
  name: {{ template "unifi.fullname" . }}-discovery
  labels:
    app: {{ template "unifi.name" . }}
    chart: {{ template "unifi.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
{{- if .Values.discoveryService.labels }}
{{ toYaml .Values.discoveryService.labels | indent 4 }}
{{- end }}
{{- with .Values.discoveryService.annotations }}
  annotations:
{{ toYaml . | indent 4 }}
{{- end }}
spec:
{{- if (or (eq .Values.discoveryService.type "ClusterIP") (empty .Values.discoveryService.type)) }}
  type: ClusterIP
  {{- if .Values.discoveryService.clusterIP }}
  clusterIP: {{ .Values.discoveryService.clusterIP }}
  {{end}}
{{- else if eq .Values.discoveryService.type "LoadBalancer" }}
  type: {{ .Values.discoveryService.type }}
  {{- if .Values.discoveryService.loadBalancerIP }}
  loadBalancerIP: {{ .Values.discoveryService.loadBalancerIP }}
  {{- end }}
  {{- if .Values.discoveryService.loadBalancerSourceRanges }}
  loadBalancerSourceRanges:
{{ toYaml .Values.discoveryService.loadBalancerSourceRanges | indent 4 }}
  {{- end -}}
{{- else }}
  type: {{ .Values.discoveryService.type }}
{{- end }}
{{- if .Values.discoveryService.externalIPs }}
  externalIPs:
{{ toYaml .Values.discoveryService.externalIPs | indent 4 }}
{{- end }}
  {{- if .Values.discoveryService.externalTrafficPolicy }}
  externalTrafficPolicy: {{ .Values.discoveryService.externalTrafficPolicy }}
  {{- end }}
  ports:
    - port: {{ .Values.discoveryService.port }}
      targetPort: discovery
      protocol: UDP
      name: discovery
{{ if (and (eq .Values.discoveryService.type "NodePort") (not (empty .Values.discoveryService.nodePort))) }}
      nodePort: {{.Values.discoveryService.nodePort}}
{{ end }}
  selector:
    app: {{ template "unifi.name" . }}
    release: {{ .Release.Name }}
