{{- $nacos := .Values -}}
{{- $ncs := .Values.nacos }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nacos
  namespace: {{ .Release.Namespace }}
  labels:
    app: nacos
    dubbo.io/rev: {{ default "default" | quote }}
    install.operator.dubbo.io/owning-resource: {{ default "unknown" }}
    operator.dubbo.io/component: "Nacos"
    dubbo: nacos
    release: {{ .Release.Name }}
    app.kubernetes.io/name: "register"
spec:
  replicas: {{ $nacos.replicas }}
  {{- if eq $nacos.mode "cluster" }}
  serviceName: nacos-headless
  {{- else }}
  serviceName: nacos
  {{- end }}
  selector:
    matchLabels:
      app: nacos
      dubbo.io/rev: {{ default "default" | quote }}
      dubbo: nacos
  template:
    metadata:
      labels:
        app: nacos
        dubbo.io/rev: {{ default "default" | quote }}
        install.operator.dubbo.io/owning-resource: {{ default "unknown" }}
        operator.dubbo.io/component: "Nacos"
        dubbo: nacos
        release: {{ .Release.Name }}
        app.kubernetes.io/name: "register"
    spec:
      {{- if eq $nacos.storage.type "mysql" }}
      initContainers:
        - name: wait-init-mysql
          image: busybox
          command:
          - "/bin/sh"
          - "-c"
          - "sleep 45"
      {{- end }}
      containers:
      - name: nacos
        {{- if contains "/" $nacos.image }}
        image: {{ $nacos.image }}:v2.5.1
        {{- end }}
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 8848
        - name: clientgen-rpc
          containerPort: {{ add 8848 1000 }}
        - name: raft-rpc
          containerPort: {{ add 8848 1001 }}
        resources:
{{ toYaml $nacos.resources | trim | indent 10 }}
        env:
        - name: NACOS_SERVER_PORT
          value: "8848"
        - name: NACOS_APPLICATION_PORT
          value: "8848"
        - name: PREFER_HOST_MODE
          value: hostname
        {{- if eq $nacos.mode "cluster" | default $ncs.mode }}
        - name: MODE
          value: "cluster"
        - name: SERVICE_NAME
          value: "nacos-headless"
        - name: DOMAIN_NAME
          value: "cluster.local"
        {{- else }}
        - name: MODE
          value: "standalone"
        {{- end }}
        {{- if eq $nacos.storage.type "mysql" }}
        - name: SPRING_DATASOURCE_PLATFORM
          value: "mysql"
        - name: MYSQL_SERVICE_HOST
          valueFrom:
            configMapKeyRef:
              name: nacos-db-config
              key: mysql.db.host
        - name: MYSQL_SERVICE_DB_NAME
          valueFrom:
            configMapKeyRef:
              name: nacos-db-config
              key: mysql.db.name
        - name: MYSQL_SERVICE_PORT
          valueFrom:
            configMapKeyRef:
              name: nacos-db-config
              key: mysql.port
        - name: MYSQL_SERVICE_USER
          valueFrom:
            configMapKeyRef:
              name: nacos-db-config
              key: mysql.user
        - name: MYSQL_SERVICE_PASSWORD
          valueFrom:
            configMapKeyRef:
              name: nacos-db-config
              key: mysql.password
        - name: MYSQL_SERVICE_DB_PARAM
          valueFrom:
            configMapKeyRef:
              name: nacos-db-config
              key: mysql.param
        {{- else }}
        - name: EMBEDDED_STORAGE
          value: embedded
        - name: SPRING_DATASOURCE_PLATFORM
          value: ""
        {{- end }}
        volumeMounts:
        - name: data
          mountPath: /home/nacos/data
          subPath: data
        - name: data
          mountPath: /home/nacos/logs
          subPath: logs
      volumes:
      - name: data
        emptyDir: {}