{{- if .Values.enabled }}
{{- $namespace := .Release.Namespace -}}
{{- $applicationName := .Values.appName -}}
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ $applicationName }}
  namespace: {{ $namespace }}
  labels:
    {{- include "uavcloud-middleware.nacos.labels.standard" . | nindent 4 }}
spec:
  serviceName: {{ $applicationName }}
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "uavcloud-middleware.nacos.labels.matchLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "uavcloud-middleware.nacos.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: nacos-server
          image: "{{ .Values.global.image.repository | default .Values.image.repository }}/nacos-server:{{ .Values.image.tag }}"
          ports:
            - containerPort: {{ .Values.containerPort.dashboard }}
              name: dashboard
          env:
            - name: NACOS_AUTH_ENABLE
              value: "true"
            - name: NACOS_REPLICAS
              value: "1"
            - name: MYSQL_SERVICE_DB_NAME
              valueFrom:
                configMapKeyRef:
                  name: {{ $applicationName }}-cm
                  key: mysql.db.name
            - name: MYSQL_SERVICE_PORT
              valueFrom:
                configMapKeyRef:
                  name: {{ $applicationName }}-cm
                  key: mysql.port
            - name: MYSQL_SERVICE_USER
              valueFrom:
                configMapKeyRef:
                  name: {{ $applicationName }}-cm
                  key: mysql.user
            - name: MYSQL_SERVICE_PASSWORD
              valueFrom:
                configMapKeyRef:
                  name: {{ $applicationName }}-cm
                  key: mysql.password
            - name: MYSQL_SERVICE_HOST
              valueFrom:
                configMapKeyRef:
                  name: {{ $applicationName }}-cm
                  key: mysql.db.host
            - name: NACOS_SERVER_PORT
              value: "{{ .Values.containerPort.dashboard }}"
            - name: NACOS_APPLICATION_PORT
              value: "{{ .Values.containerPort.dashboard }}"
            - name: PREFER_HOST_MODE
              value: "hostname"
            - name: MODE
              value: standalone
            - name: SPRING_DATASOURCE_PLATFORM
              value: mysql
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
---
{{- end }}
