apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "kuiper.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "kuiper.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  serviceName: {{ include "kuiper.fullname" . }}-headless
  {{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) }}
  volumeClaimTemplates:
    - metadata:
        name: kuiper-data
        namespace: {{ .Release.Namespace }}
        labels:
          {{- include "kuiper.labels" . | nindent 4 }}
        annotations:
        {{- if .Values.persistence.storageClass }}
          volume.beta.kubernetes.io/storage-class: {{ .Values.persistence.storageClass | quote }}
        {{- else }}
          volume.alpha.kubernetes.io/storage-class: default
        {{- end }}
      spec:
        accessModes:
          - {{ .Values.persistence.accessMode | quote }}
        resources:
         requests:
           storage: {{ .Values.persistence.size | quote }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "kuiper.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "kuiper.selectorLabels" . | nindent 8 }}
    spec:
      volumes:
      {{- if not .Values.persistence.enabled }}
      - name: kuiper-data
        emptyDir: {}
      {{- else if .Values.persistence.existingClaim }}
      - name: kuiper-data
        persistentVolumeClaim:
        {{- with .Values.persistence.existingClaim }}
          claimName: {{ tpl . $ }}
        {{- end }}
      {{- end }}
      - name: kuiper-config
        configMap:
          name: {{ include "kuiper.fullname" . }}
          items:
          - key: mqtt_source.yaml
            path: mqtt_source.yaml
          - key: kuiper.yaml
            path: kuiper.yaml
          - key: client.yaml
            path: client.yaml
          - key: edgex.yaml
            path: edgex.yaml
          - key: random.yaml
            path: random.yaml
          - key: zmq.yaml
            path: zmq.yaml
      {{ $certificationSecretName := index .Values "kuiperConfig" "mqtt_source.yaml" "default" "certificationSecretName" }}
      {{- if $certificationSecretName }}
      - name: kuiper-certification
        secret:
          secretName: {{ $certificationSecretName }}
      {{- end }}
      {{ $privateKeySecretName := index .Values "kuiperConfig" "mqtt_source.yaml" "default" "privateKeySecretName" }}
      {{- if $privateKeySecretName }}
      - name: kuiper-private-key
        secret:
          secretName: {{ $privateKeySecretName }}
      {{- end }}
      containers:
        - name: kuiper
          image: "{{ .Values.image.repository }}:{{ .Chart.AppVersion }}-alpine"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: ["./bin/server"]
          ports:
          {{ $restPort := index .Values "kuiperConfig" "kuiper.yaml" "basic" "restPort" }}
          - containerPort: {{ $restPort | default 9081 }}
          {{ $port := index .Values "kuiperConfig" "kuiper.yaml" "basic" "port" }}
          - containerPort: {{ $port | default 20498 }}
          volumeMounts:
          - name: kuiper-data
            mountPath: "/kuiper/data"
          - name: kuiper-config
            mountPath: "/kuiper/etc/mqtt_source.yaml"
            subPath: "mqtt_source.yaml"
          - name: kuiper-config
            mountPath: "/kuiper/etc/kuiper.yaml"
            subPath: "kuiper.yaml"
          - name: kuiper-config
            mountPath: "/kuiper/etc/client.yaml"
            subPath: "client.yaml"
          - name: kuiper-config
            mountPath: "/kuiper/etc/sources/edgex.yaml"
            subPath: "edgex.yaml"
          - name: kuiper-config
            mountPath: "/kuiper/etc/sources/random.yaml"
            subPath: "random.yaml"
          - name: kuiper-config
            mountPath: "/kuiper/etc/sources/zmq.yaml"
            subPath: "zmq.yaml"
          readinessProbe:
            httpGet:
              {{ $restPort := index .Values "kuiperConfig" "kuiper.yaml" "basic" "restPort" }}
              port: {{ $restPort | default 9081 }}
            initialDelaySeconds: 5
            periodSeconds: 5
          {{ $certificationSecretName := index .Values "kuiperConfig" "mqtt_source.yaml" "default" "certificationSecretName" }}
          {{- if $certificationSecretName }}
          - name: kuiper-certification
            mountPath: {{ index .Values "kuiperConfig" "mqtt_source.yaml" "default" "certificationPath" | default "/var/kuiper/certificate.pem" }}
            readOnly: true
          {{ end }}
          {{ $privateKeySecretName := index .Values "kuiperConfig" "mqtt_source.yaml" "default" "privateKeySecretName" }}
          {{- if $privateKeySecretName }}
          - name: kuiper-private-key
            mountPath: {{ index .Values "kuiperConfig" "mqtt_source.yaml" "default" "privateKeyPath" | default "/var/kuiper/private.pem.key" }}
            readOnly: true
          {{ end }}
          resources:
          {{- toYaml .Values.resources | nindent 12 }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
