{{ if .Values.unifiedService.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "unifi.fullname" . }}
  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.unifiedService.labels }}
{{ toYaml .Values.unifiedService.labels | indent 4 }}
{{- end }}
{{- with .Values.unifiedService.annotations }}
  annotations:
{{ toYaml . | indent 4 }}
{{- end }}
spec:
{{- if (or (eq .Values.unifiedService.type "ClusterIP") (empty .Values.unifiedService.type)) }}
  type: ClusterIP
  {{- if .Values.unifiedService.clusterIP }}
  clusterIP: {{ .Values.unifiedService.clusterIP }}
  {{end}}
{{- else if eq .Values.unifiedService.type "LoadBalancer" }}
  type: {{ .Values.unifiedService.type }}
  {{- if .Values.unifiedService.loadBalancerIP }}
  loadBalancerIP: {{ .Values.unifiedService.loadBalancerIP }}
  {{- end }}
  {{- if .Values.unifiedService.loadBalancerSourceRanges }}
  loadBalancerSourceRanges:
{{ toYaml .Values.unifiedService.loadBalancerSourceRanges | indent 4 }}
  {{- end -}}
{{- else }}
  type: {{ .Values.unifiedService.type }}
{{- end }}
{{- if .Values.unifiedService.externalIPs }}
  externalIPs:
{{ toYaml .Values.unifiedService.externalIPs | indent 4 }}
{{- end }}
  {{- if .Values.unifiedService.externalTrafficPolicy }}
  externalTrafficPolicy: {{ .Values.unifiedService.externalTrafficPolicy }}
  {{- end }}
  ports:
    - port: {{ .Values.controllerService.port }}
      targetPort: controller
      protocol: TCP
      name: controller
{{ if (and (eq .Values.unifiedService.type "NodePort") (not (empty .Values.controllerService.nodePort))) }}
      nodePort: {{.Values.controllerService.nodePort}}
{{ end }}
    - port: {{ .Values.discoveryService.port }}
      targetPort: discovery
      protocol: UDP
      name: discovery
{{ if (and (eq .Values.unifiedService.type "NodePort") (not (empty .Values.discoveryService.nodePort))) }}
      nodePort: {{.Values.discoveryService.nodePort}}
{{ end }}
    - port: {{ .Values.stunService.port }}
      targetPort: stun
      protocol: UDP
      name: stun
{{ if (and (eq .Values.unifiedService.type "NodePort") (not (empty .Values.stunService.nodePort))) }}
      nodePort: {{.Values.stunService.nodePort}}
{{ end }}
    - name: https-gui
      port: {{ .Values.guiService.port }}
      protocol: TCP
      targetPort: https-gui
{{ if (and (eq .Values.unifiedService.type "NodePort") (not (empty .Values.guiService.nodePort))) }}
      nodePort: {{.Values.guiService.nodePort}}
{{ end }}
{{ if .Values.captivePortalService.enabled }}
    - name: captive-http
      port: {{ .Values.captivePortalService.http }}
      protocol: TCP
      targetPort: captive-http
{{ if (and (eq .Values.unifiedService.type "NodePort") (not (empty .Values.captivePortalService.http))) }}
      nodePort: {{.Values.captivePortalService.http}}
{{ end }}
    - name: captive-https
      port: {{ .Values.captivePortalService.https }}
      protocol: TCP
      targetPort: captive-https
{{ if (and (eq .Values.unifiedService.type "NodePort") (not (empty .Values.captivePortalService.https))) }}
      nodePort: {{.Values.captivePortalService.https}}
{{ end }}
{{ end }}

  selector:
    app.kubernetes.io/name: {{ include "unifi.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
{{ end }}

