{{- if and .Values.enabled.standaloneMode (not .Values.enabled.clusterMode) }}
{{- $namespace := .Release.Namespace -}}
{{- $applicationName := .Values.appName.standaloneMode -}}
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ $applicationName }}
  namespace: {{ $namespace }}
  labels:
    {{- include "uavcloud-middleware.emqx.labels.standard" . | nindent 4 }}
spec:
  serviceName: {{ $applicationName }}
  replicas: {{ .Values.replicas.standaloneMode }}
  selector:
    matchLabels:
      {{- include "uavcloud-middleware.emqx.labels.matchLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "uavcloud-middleware.emqx.labels.standard" . | nindent 8 }}
      annotations:
        pod.alpha.kubernetes.io/initialized: "true"
    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 }}
      containers:
        - name: {{ $applicationName }}
          image: "{{ .Values.global.image.repository | default .Values.image.repository }}/emqx:{{ .Values.image.tag }}"
          securityContext:
            privileged: true
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          ports:
            - containerPort: {{ .Values.containerPort.mqtt }}
              name: mqtt
              protocol: TCP
            - containerPort: {{ .Values.containerPort.dashboard }}
              name: dashboard
              protocol: TCP
            - containerPort: {{ .Values.containerPort.websocket }}
              name: mqtt-websocket
              protocol: TCP
          volumeMounts:
            - name: {{ $applicationName }}-plugins
              mountPath: /opt/emqx/data/loaded_plugins
              subPath: loaded_plugins
            - name: {{ $applicationName }}-auth
              mountPath: /opt/emqx/etc/plugins/emqx_auth_username.conf
              subPath: emqx_auth_username.conf
            - name: emqx-data
              mountPath: /opt/emqx/data/emqx_erl_pipes
              readOnly: false
              subPath: {{ $namespace }}/{{ $applicationName }}/data
            - name: emqx-data
              mountPath: /opt/emqx/log
              readOnly: false
              subPath: {{ $namespace }}/{{ $applicationName }}/log
      volumes:
        - name: emqx-data
          persistentVolumeClaim:
            claimName: helm-emqxs
        - name: {{ $applicationName }}-plugins
          configMap:
            name: {{ $applicationName }}-plugins
            items:
              - key: loaded_plugins
                path: loaded_plugins
        - name: {{ $applicationName }}-auth
          configMap:
            name: {{ $applicationName }}-auth
            items:
              - key: emqx_auth_username.conf
                path: emqx_auth_username.conf
---
{{- end }}
