# helm_charts_discovery-svc.yaml
{{ if not .Values.unifiedService.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "unifi.fullname" . }}-discovery
  labels:
    app.kubernetes.io/name: {{ include "unifi.name" . }}
    helm.sh/chart: {{ include "unifi.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .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.kubernetes.io/name: {{ include "unifi.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
{{ end }}