apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ template "cp-zookeeper.fullname" . }}
  labels:
    app: {{ template "cp-zookeeper.name" . }}
    chart: {{ template "cp-zookeeper.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  selector:
    matchLabels:
      app: {{ template "cp-zookeeper.name" . }}
      release: {{ .Release.Name }}
  serviceName: {{ template "cp-zookeeper.fullname" . }}-headless
  podManagementPolicy: {{ .Values.podManagementPolicy }}
  replicas: {{ default 3 .Values.servers }}
  updateStrategy:
    type: {{ .Values.updateStrategy }}
  template:
    metadata:
      labels:
        app: {{ template "cp-zookeeper.name" . }}
        release: {{ .Release.Name }}
      {{- if or .Values.podAnnotations .Values.prometheus.jmx.enabled }}
      annotations:
      {{- range $key, $value := .Values.podAnnotations }}
        {{ $key }}: {{ $value | quote }}
      {{- end }}
      {{- if .Values.prometheus.jmx.enabled }}
        prometheus.io/scrape: "true"
        prometheus.io/port: {{ .Values.prometheus.jmx.port | quote }}
      {{- end }}
      {{- end }}
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 1
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                  - key: "app"
                    operator: In
                    values:
                    - {{ template "cp-zookeeper.name" . }}
                  - key: "release"
                    operator: In
                    values:
                    - {{ .Release.Name }}
              topologyKey: "kubernetes.io/hostname"
      containers:
      {{- if .Values.prometheus.jmx.enabled }}
      - name: prometheus-jmx-exporter
        image: "{{ .Values.prometheus.jmx.image }}:{{ .Values.prometheus.jmx.imageTag }}"
        imagePullPolicy: "{{ .Values.prometheus.jmx.imagePullPolicy }}"
        command:
        - java
        - -XX:+UnlockExperimentalVMOptions
        - -XX:+UseCGroupMemoryLimitForHeap
        - -XX:MaxRAMFraction=1
        - -XshowSettings:vm
        - -jar
        - jmx_prometheus_httpserver.jar
        - {{ .Values.prometheus.jmx.port | quote }}
        - /etc/jmx-zookeeper/jmx-zookeeper-prometheus.yml
        ports:
        - containerPort: {{ .Values.prometheus.jmx.port }}
        resources:
{{ toYaml .Values.prometheus.jmx.resources | indent 10 }}
        volumeMounts:
        - name: jmx-config
          mountPath: /etc/jmx-zookeeper
      {{- end }}
      - name: {{ template "cp-zookeeper.name" . }}-server
        image: "{{ .Values.image }}:{{ .Values.imageTag }}"
        imagePullPolicy: "{{ .Values.imagePullPolicy }}"
        ports:
        - containerPort: {{ .Values.clientPort }}
          name: client
        - containerPort: {{ .Values.serverPort }}
          name: server
        - containerPort: {{ .Values.leaderElectionPort }}
          name: leader-election
        {{- if .Values.prometheus.jmx.enabled }}
        - containerPort: {{ .Values.jmx.port }}
          name: jmx
        {{- end }}
        resources:
{{ toYaml .Values.resources | indent 10 }}
        env:
        - name : KAFKA_HEAP_OPTS
          value: "{{ .Values.heapOptions }}"
        {{- if .Values.jmx.port }}
        - name : KAFKA_JMX_PORT
          value: "{{ .Values.jmx.port }}"
        {{- end }}
        - name : ZOOKEEPER_TICK_TIME
          value: "{{ .Values.tickTime }}"
        - name : ZOOKEEPER_SYNC_LIMIT
          value: "{{ .Values.syncLimit }}"
        - name : ZOOKEEPER_INIT_LIMIT
          value: "{{ .Values.initLimit }}"
        - name : ZOOKEEPER_MAX_CLIENT_CNXNS
          value: "{{ .Values.maxClientCnxns }}"
        - name : ZOOKEEPER_AUTOPURGE_SNAP_RETAIN_COUNT
          value: "{{ .Values.autoPurgeSnapRetainCount}}"
        - name : ZOOKEEPER_AUTOPURGE_PURGE_INTERVAL
          value: "{{ .Values.autoPurgePurgeInterval}}"
        - name: ZOOKEEPER_CLIENT_PORT
          value: "{{ .Values.clientPort }}"
        - name : ZOOKEEPER_SERVERS
          value: {{ template "cp-zookeeper.serverlist" . }}
        # ZOOKEEPER_SERVER_ID is required just to pass cp-zookeeper ensure script for env check,
        # the value(metadata.mame) is not used and will be overwritten in command part
        - name: ZOOKEEPER_SERVER_ID
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        command:
        - "bash"
        - "-c"
        - |
          ZK_FIX_HOST_REGEX="s/${HOSTNAME}\.[^:]*:/0.0.0.0:/g"
          ZOOKEEPER_SERVER_ID=$((${HOSTNAME##*-}+1)) \
          ZOOKEEPER_SERVERS=`echo $ZOOKEEPER_SERVERS | sed -e "$ZK_FIX_HOST_REGEX"` \
          /etc/confluent/docker/run
        volumeMounts:
        - name: datadir
          mountPath: /var/lib/zookeeper/data
        - name: datalogdir
          mountPath: /var/lib/zookeeper/log
      {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.imagePullSecrets | indent 8 }}
      {{- end }}
      volumes:
      {{ if not .Values.persistence.enabled }}
      - name: datadir
        emptyDir: {}
      - name: datalogdir
        emptyDir: {}
      {{- end }}
      {{- if .Values.prometheus.jmx.enabled }}
      - name: jmx-config
        configMap:
          name: {{ template "cp-zookeeper.fullname" . }}-jmx-configmap
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
      {{- end }}
  {{- if .Values.persistence.enabled }}
  volumeClaimTemplates:
  - metadata:
      name: datadir
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: "{{ .Values.persistence.dataDirSize }}"
      {{- if .Values.persistence.dataDirStorageClass }}
      {{- if (eq "-" .Values.persistence.dataDirStorageClass) }}
      storageClassName: ""
      {{- else }}
      storageClassName: "{{ .Values.persistence.dataDirStorageClass }}"
      {{- end }}
      {{- end }}
  - metadata:
      name: datalogdir
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: "{{ .Values.persistence.dataLogDirSize }}"
      {{- if .Values.persistence.dataLogDirStorageClass }}
      {{- if (eq "-" .Values.persistence.dataLogDirStorageClass) }}
      storageClassName: ""
      {{- else }}
      storageClassName: "{{ .Values.persistence.dataLogDirStorageClass }}"
      {{- end }}
      {{- end }}
  {{- end }}
