{{- if .Values.enabled.clusterMode  }}
{{- $namespace := .Release.Namespace -}}
{{- $applicationName := .Values.appName.clusterMode -}}
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ $applicationName }}
  namespace: {{ $namespace }}
  labels:
    {{- include "uavcloud-middleware.emqx.labels.standard" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicas.clusterMode }}
  serviceName: {{ $applicationName }}-headless
  updateStrategy:
    type: RollingUpdate
  selector:
    matchLabels:
      {{- include "uavcloud-middleware.emqx.labels.matchLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "uavcloud-middleware.emqx.labels.standard" . | nindent 8 }}
    spec:
      {{- if .Values.global.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.global.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        {{- /*        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.global.podAffinityPreset "context" $) | nindent 10 -}}*/}}
        {{- /*        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.global.podAntiAffinityPreset "context" $) | nindent 10 }}*/}}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.global.nodeAffinityPreset.type "key" .Values.global.nodeAffinityPreset.key "values" .Values.global.nodeAffinityPreset.values) | nindent 10 -}}
      {{- end }}
      serviceAccountName: {{ $applicationName }}
      containers:
        - name: {{ $applicationName }}
          image: "{{ .Values.global.image.repository | default .Values.image.repository }}/emqx:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.global.image.pullPolicy }}
          ports:
            - name: mqtt
              containerPort: {{ .Values.containerPort.mqtt }}
            - name: mqttssl
              containerPort: {{ .Values.containerPort.mqttssl }}
            - name: mgmt
              containerPort: {{ .Values.containerPort.mgmt }}
            - name: ws
              containerPort: {{ .Values.containerPort.websocket }}
            - name: wss
              containerPort: {{ .Values.containerPort.wss }}
            - name: dashboard
              containerPort: {{ .Values.containerPort.dashboard }}
            - name: ekka
              containerPort: 4370
          envFrom:
            - configMapRef:
                name: {{ $applicationName }}-env
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          readinessProbe:
            httpGet:
              path: /status
              port: {{ .Values.containerPort.mgmt | default 8081 }}
            initialDelaySeconds: 5
            periodSeconds: 5
          volumeMounts:
            - name: emqx-data
              mountPath: "/opt/emqx/data/mnesia"
              readOnly: false
            - name: {{ $applicationName }}-cm
              mountPath: "/opt/emqx/etc/plugins/emqx_auth_username.conf"
              subPath: emqx_auth_username.conf
              readOnly: false
            - name: {{ $applicationName }}-cm
              mountPath: "/opt/emqx/etc/acl.conf"
              subPath: "acl.conf"
              readOnly: false
            - name: {{ $applicationName }}-cm
              mountPath: "/opt/emqx/data/loaded_plugins"
              subPath: loaded_plugins
              readOnly: false
      volumes:
        - name: emqx-data
          persistentVolumeClaim:
            claimName: helm-emqxs
        - name: {{ $applicationName }}-cm
          configMap:
            name: {{ $applicationName }}-cm
            items:
              - key: emqx_auth_username.conf
                path: emqx_auth_username.conf
              - key: acl.conf
                path: acl.conf
              - key: loaded_plugins
                path: loaded_plugins
{{- end }}
