apiVersion: v1
kind: Service
metadata:
  name: {{ include "emqx.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/name: {{ include "emqx.name" . }}
    helm.sh/chart: {{ include "emqx.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
  {{- if .Values.service.labels }}
    {{- toYaml .Values.service.labels | nindent 4 }}
  {{- end }}
  {{- if .Values.service.annotations }}
  annotations:
    {{- toYaml .Values.service.annotations | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.service.type }}
  {{- if or (eq .Values.service.type "LoadBalancer") (eq .Values.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.service.externalTrafficPolicy | default "Cluster" }}
  {{- end }}
  {{- if eq .Values.service.type "LoadBalancer" }}
  {{- if .Values.service.loadBalancerClass }}
  loadBalancerClass: {{ .Values.service.loadBalancerClass }}
  {{- end }}
  {{- if .Values.service.loadBalancerIP }}
  loadBalancerIP: {{ .Values.service.loadBalancerIP }}
  {{- end }}
  {{- if .Values.service.loadBalancerSourceRanges }}
  loadBalancerSourceRanges: {{- toYaml .Values.service.loadBalancerSourceRanges | nindent 4 }}
  {{- end }}
  {{- if .Values.service.externalIPs }}
  externalIPs: {{- toYaml .Values.service.externalIPs | nindent 4 }}
  {{- end }}
  {{- end }}
  ports:
  - name: mqtt
    port: {{ .Values.service.mqtt | default 1883 }}
    protocol: TCP
    targetPort: mqtt
    {{- if and (or (eq .Values.service.type "NodePort") (eq .Values.service.type "LoadBalancer")) (not (empty .Values.service.nodePorts.mqtt)) }}
    nodePort: {{ .Values.service.nodePorts.mqtt }}
    {{- else if eq .Values.service.type "ClusterIP" }}
    nodePort: null
    {{- end }}
  - name: mqttssl
    port: {{ .Values.service.mqttssl | default 8883 }}
    protocol: TCP
    targetPort: mqttssl
    {{- if and (or (eq .Values.service.type "NodePort") (eq .Values.service.type "LoadBalancer")) (not (empty .Values.service.nodePorts.mqttssl)) }}
    nodePort: {{ .Values.service.nodePorts.mqttssl }}
    {{- else if eq .Values.service.type "ClusterIP" }}
    nodePort: null
    {{- end }}
  - name: ws
    port: {{ .Values.service.ws | default 8083 }}
    protocol: TCP
    targetPort: ws
    {{- if and (or (eq .Values.service.type "NodePort") (eq .Values.service.type "LoadBalancer")) (not (empty .Values.service.nodePorts.ws)) }}
    nodePort: {{ .Values.service.nodePorts.ws }}
    {{- else if eq .Values.service.type "ClusterIP" }}
    nodePort: null
    {{- end }}
  - name: wss
    port: {{ .Values.service.wss | default 8084 }}
    protocol: TCP
    targetPort: wss
    {{- if and (or (eq .Values.service.type "NodePort") (eq .Values.service.type "LoadBalancer")) (not (empty .Values.service.nodePorts.wss)) }}
    nodePort: {{ .Values.service.nodePorts.wss }}
    {{- else if eq .Values.service.type "ClusterIP" }}
    nodePort: null
    {{- end }}
  - name: dashboard
    port: {{ .Values.service.dashboard | default 18083 }}
    protocol: TCP
    targetPort: dashboard
    {{- if and (or (eq .Values.service.type "NodePort") (eq .Values.service.type "LoadBalancer")) (not (empty .Values.service.nodePorts.dashboard)) }}
    nodePort: {{ .Values.service.nodePorts.dashboard }}
    {{- else if eq .Values.service.type "ClusterIP" }}
    nodePort: null
    {{- end }}
  {{- if not (empty .Values.service.dashboardtls) }}
  - name: dashboardtls
    port: {{ .Values.service.dashboardtls }}
    protocol: TCP
    targetPort: dashboardtls
    {{- if and (or (eq .Values.service.type "NodePort") (eq .Values.service.type "LoadBalancer")) (not (empty .Values.service.nodePorts.dashboardtls)) }}
    nodePort: {{ .Values.service.nodePorts.dashboardtls }}
    {{- else if eq .Values.service.type "ClusterIP" }}
    nodePort: null
    {{- end }}
  {{- end }}
  selector:
    app.kubernetes.io/name: {{ include "emqx.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}

---
apiVersion: v1
kind: Service
metadata:
  name: {{ include "emqx.fullname" . }}-headless
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/name: {{ include "emqx.name" . }}
    helm.sh/chart: {{ include "emqx.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  type: ClusterIP
  sessionAffinity: None
  clusterIP: {{ .Values.service.clusterIP | default "None" }}
  publishNotReadyAddresses: true
  ports:
  - name: mqtt
    port: {{ .Values.service.mqtt | default 1883 }}
    protocol: TCP
    targetPort: mqtt
  - name: mqttssl
    port: {{ .Values.service.mqttssl | default 8883 }}
    protocol: TCP
    targetPort: mqttssl
  - name: ws
    port: {{ .Values.service.ws | default 8083 }}
    protocol: TCP
    targetPort: ws
  - name: wss
    port: {{ .Values.service.wss | default 8084 }}
    protocol: TCP
    targetPort: wss
  - name: dashboard
    port: {{ .Values.service.dashboard | default 18083 }}
    protocol: TCP
    targetPort: dashboard
  - name: ekka
    port: 4370
    protocol: TCP
    targetPort: ekka
  selector:
    app.kubernetes.io/name: {{ include "emqx.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
