{{- $root := . -}}
{{- if .Values.statefulset.enabled }}
apiVersion: {{ include "common.capabilities.statefulset.apiVersion" . }}
kind: StatefulSet
{{- else }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
{{- end }}
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
spec:
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
  replicas: {{ .Values.replicaCount }}
  {{- if .Values.statefulset.enabled }}
  serviceName: {{ include "common.names.fullname" . }}-headless
  podManagementPolicy: {{ .Values.podManagementPolicy }}
  {{- end }}
  {{- if .Values.updateStrategy }}
  strategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        {{- if .Values.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podLabels "context" $) | nindent 8 }}
        {{- end }}
      annotations:
        {{- if .Values.podAnnotations }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
        {{- if and .Values.metrics.enabled .Values.metrics.podAnnotations }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.metrics.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      automountServiceAccountToken: {{ .Values.serviceAccount.autoMount }}
      shareProcessNamespace: {{ .Values.sidecarSingleProcessNamespace }}
      serviceAccountName: {{ template "nacos.serviceAccountName" . }}
      {{- if .Values.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAffinityPreset "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.nodeAffinityPreset.type "key" .Values.nodeAffinityPreset.key "values" .Values.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      hostNetwork: {{ .Values.hostNetwork }}
      hostIPC: {{ .Values.hostIPC }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.dnsPolicy }} 
      dnsPolicy: {{ .Values.dnsPolicy | quote }}
      {{- end }}
      {{- include "nacos.imagePullSecrets" . | nindent 6 }}
      initContainers:
        - name: peer-finder-plugin-install
          image: nacos/nacos-peer-finder-plugin:latest
          imagePullPolicy: Always
          volumeMounts:
            {{- if .Values.persistence.mountPaths }}
            {{- toYaml .Values.persistence.mountPaths | nindent 12 }}
            {{- end }}
            {{- if .Values.extraVolumeMounts }}
            {{- toYaml .Values.extraVolumeMounts | nindent 12 }}
            {{- end }}
        {{- if .Values.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | nindent 8 }}
          volumeMounts:
            {{- if .Values.persistence.mountPaths }}
            {{ toYaml .Values.persistence.mountPaths | nindent 12 }}
            {{- end }}
            {{- if .Values.extraVolumeMounts }}
            {{ toYaml .Values.extraVolumeMounts | nindent 12 }}
            {{- end }}
        {{- end }}
      containers:
        - name: {{ include "common.names.name" . }}
          image: {{ template "nacos.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.lifecycle }}
          lifecycle:
            {{- toYaml .Values.lifecycle | nindent 12 }}
          {{- end }}
          {{- if .Values.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.args "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: NACOS_REPLICAS
              value: "{{ .Values.replicaCount }}"
            - name: NACOS_SERVERS
              value: {{ range $i, $e := until (int $.Values.replicaCount) -}}
                     {{- $nacosPodName := (printf "%s-%d.%s-headless" (include "common.names.fullname" $root) $i (include "common.names.fullname" $root)) -}}
                     {{- $nacosPodName -}}:8848{{ printf " " }}
                     {{- end }}
            - name: DOMAIN_NAME
              value: {{ .Values.clusterDomain | quote }}
            - name: SERVICE_NAME
              value: {{ include "common.names.fullname" . }}-headless
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.namespace
            - name: MYSQL_SERVICE_HOST
              valueFrom:
                secretKeyRef:
                  name: {{ include "common.names.fullname" . }}
                  key: mysqlMasterHost
            - name: MYSQL_SERVICE_DB_NAME
              valueFrom:
                secretKeyRef:
                  name: {{ include "common.names.fullname" . }}
                  key: mysqlDatabase
            - name: MYSQL_SERVICE_PORT
              valueFrom:
                secretKeyRef:
                  name: {{ include "common.names.fullname" . }}
                  key: mysqlMasterPort
            - name: MYSQL_SERVICE_USER
              valueFrom:
                secretKeyRef:
                  name: {{ include "common.names.fullname" . }}
                  key: mysqlMasterUser
            - name: MYSQL_SERVICE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "common.names.fullname" . }}
                  key: mysqlMasterPassword
            - name: NACOS_SERVER_PORT
              value: "8848"
            - name: NACOS_APPLICATION_PORT
              value: "8848"	  
            {{- if .Values.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- end }}
          ports:
            {{- range $key, $value := .Values.service.ports }}
            - name: {{ $key }}
              containerPort: {{ $value.port }}
              protocol: {{ $value.protocol }}
            {{- end }}
          {{- if .Values.healthCheck.livenessProbe.enabled }}
          livenessProbe:
            {{- if eq .Values.healthCheck.type "http" }}
            httpGet:
              path: {{ .Values.healthCheck.livenessProbe.httpPath }}
              port: {{ .Values.healthCheck.port }}
            {{- else }}
            tcpSocket:
              port: {{ .Values.healthCheck.port }}
            {{- end }}
            initialDelaySeconds: {{ .Values.healthCheck.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.healthCheck.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.healthCheck.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.healthCheck.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.healthCheck.livenessProbe.failureThreshold }}
          {{- else if .Values.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.healthCheck.readinessProbe.enabled }}
          readinessProbe:
            {{- if eq .Values.healthCheck.type "http" }}
            httpGet:
              path: {{ .Values.healthCheck.readinessProbe.httpPath }}
              port: {{ .Values.healthCheck.port }}
            {{- else }}
            tcpSocket:
              port: {{ .Values.healthCheck.port }}
            {{- end }}
            initialDelaySeconds: {{ .Values.healthCheck.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.healthCheck.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.healthCheck.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.healthCheck.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.healthCheck.readinessProbe.failureThreshold }}
          {{- else if .Values.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- if .Values.config.enabled }}
            - name: {{ include "common.names.name" . }}-conf
              mountPath: {{ .Values.config.mountPath }}
              subPath: {{ .Values.config.subPath }}
              readOnly: {{ .Values.config.readOnly }}
            {{- end }}
            {{- if .Values.existConfig.enabled }}
            - name: {{ include "common.names.name" . }}-exist-conf
              mountPath: {{ .Values.existConfig.mountPath }}
              subPath: {{ .Values.existConfig.subPath }}
              readOnly: {{ .Values.existConfig.readOnly }}
            {{- end }}
            {{- if .Values.secret.enabled }}
            - name: {{ include "common.names.name" . }}-secret
              mountPath: {{ .Values.secret.mountPath }}
              subPath: {{ .Values.secret.subPath }}
              readOnly: {{ .Values.secret.readOnly }}
            {{- end }}
            {{- if .Values.existSecret.enabled }}
            - name: {{ include "common.names.name" . }}-exist-secret
              mountPath: {{ .Values.existSecret.mountPath }}
              subPath: {{ .Values.existSecret.subPath }}
              readOnly: {{ .Values.existSecret.readOnly }}
            {{- end }}
            {{- if .Values.persistence.mountPaths }}
            {{- toYaml .Values.persistence.mountPaths | nindent 12 }}
            {{- end }}
            {{- if .Values.extraVolumeMounts }}
            {{- toYaml .Values.extraVolumeMounts | nindent 12 }}
            {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        {{- if .Values.config.enabled }}
        - name: {{ include "common.names.name" . }}-conf
          configMap:
            name: {{ include "common.names.fullname" . }}
        {{- end }}
        {{- if .Values.existConfig.enabled }}
        - name: {{ include "common.names.name" . }}-exist-conf
          configMap:
            name: {{ .Values.existConfig.name }}
        {{- end }}
        {{- if .Values.secret.enabled }}
        - name: {{ include "common.names.name" . }}-secret
          secret:
            secretName: {{ include "common.names.fullname" . }}
        {{- end }}
        {{- if .Values.existSecret.enabled }}
        - name: {{ include "common.names.name" . }}-exist-secret
          secret:
            secretName: {{ .Values.existSecret.name }}
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- toYaml .Values.extraVolumes | nindent 8 }}
        {{- end }}
{{- if not .Values.statefulset.enabled }}
        {{- if .Values.persistence.enabled }}
        - name: data-storage
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim | default (include "common.names.fullname" .) }}
        {{- else }}
        - name: data-storage
          emptyDir: {}
        {{- end }}
{{- else }}
  {{- if .Values.persistence.enabled }}
  volumeClaimTemplates:
  - metadata:
      name: data-storage
      {{- if .Values.persistence.annotations }}
      annotations: {{- toYaml .Values.persistence.annotations | nindent 10 }}
      {{- end }}
    spec:
      accessModes:
        - {{ .Values.persistence.accessMode | quote }}
      annotations:
      {{- range $key, $value := $.Values.persistence.annotations }}
        {{ $key }}: {{ $value }}
      {{- end }}
      resources:
        requests:
          storage: {{ .Values.persistence.size }}
          {{- if .Values.persistence.storageClass }}
            {{- if (eq "-" .Values.persistence.storageClass) }}
      storageClassName: ""
            {{- else }}
      storageClassName: "{{ .Values.persistence.storageClass }}"
            {{- end }}
          {{- end }}
  {{- else }}
        - name: data-storage
          emptyDir: {}
  {{- end }}
{{- end -}}
