#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

# deploy zookeeper only when `components.zookeeper` is true
{{- if .Values.components.zookeeper }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: "{{ template "pulsar.fullname" . }}-{{ .Values.zookeeper.component }}"
  namespace: {{ template "pulsar.namespace" . }}
  labels:
    {{- include "pulsar.standardLabels" . | nindent 4 }}
    component: {{ .Values.zookeeper.component }}
spec:
  serviceName: "{{ template "pulsar.fullname" . }}-{{ .Values.zookeeper.component }}"
  replicas: {{ .Values.zookeeper.replicaCount }}
  selector:
    matchLabels:
      {{- include "pulsar.matchLabels" . | nindent 6 }}
      component: {{ .Values.zookeeper.component }}
  updateStrategy:
{{ toYaml .Values.zookeeper.updateStrategy | indent 4 }}
  podManagementPolicy: {{ .Values.zookeeper.podManagementPolicy }}
  template:
    metadata:
      labels:
        {{- include "pulsar.template.labels" . | nindent 8 }}
        component: {{ .Values.zookeeper.component }}
      annotations:
        {{- if .Values.zookeeper.restartPodsOnConfigMapChange }}
        checksum/config: {{ include (print $.Template.BasePath "/zookeeper-configmap.yaml") . | sha256sum }}
        {{- end }}
{{ toYaml .Values.zookeeper.annotations | indent 8 }}
    spec:
    {{- if .Values.zookeeper.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.zookeeper.nodeSelector | indent 8 }}
    {{- end }}
    {{- if .Values.zookeeper.tolerations }}
      tolerations:
{{ toYaml .Values.zookeeper.tolerations | indent 8 }}
    {{- end }}
      affinity:
        {{- if and .Values.affinity.anti_affinity .Values.zookeeper.affinity.anti_affinity}}
        podAntiAffinity:
          {{ if eq .Values.zookeeper.affinity.type "requiredDuringSchedulingIgnoredDuringExecution"}}
          {{ .Values.zookeeper.affinity.type }}:
          - labelSelector:
              matchExpressions:
              - key: "app"
                operator: In
                values:
                - "{{ template "pulsar.name" . }}"
              - key: "release"
                operator: In
                values:
                - {{ .Release.Name }}
              - key: "component"
                operator: In
                values:
                - {{ .Values.zookeeper.component }}
            topologyKey: "kubernetes.io/hostname"
        {{ else }}
          {{ .Values.zookeeper.affinity.type }}:
            - weight: 100
              podAffinityTerm:
                labelSelector:
                  matchExpressions:
                    - key: "app"
                      operator: In
                      values:
                      - "{{ template "pulsar.name" . }}"
                    - key: "release"
                      operator: In
                      values:
                      - {{ .Release.Name }}
                    - key: "component" 
                      operator: In
                      values:
                      - {{ .Values.zookeeper.component }}
                topologyKey: "kubernetes.io/hostname"
        {{ end }}
        {{- end }}
      terminationGracePeriodSeconds: {{ .Values.zookeeper.gracePeriod }}
    {{- if and .Values.rbac.enabled .Values.rbac.psp }}
      serviceAccountName: "{{ template "pulsar.fullname" . }}-{{ .Values.zookeeper.component }}"
    {{- end }}
      containers:
      - name: "{{ template "pulsar.fullname" . }}-{{ .Values.zookeeper.component }}"
        image: "{{ .Values.images.zookeeper.repository }}:{{ .Values.images.zookeeper.tag }}"
        imagePullPolicy: {{ .Values.images.zookeeper.pullPolicy }}
      {{- if .Values.zookeeper.resources }}
        resources:
{{ toYaml .Values.zookeeper.resources | indent 10 }}
      {{- end }}
        command: ["sh", "-c"]
        args:
        - >
          bin/apply-config-from-env.py conf/zookeeper.conf;
          {{- include "pulsar.zookeeper.tls.settings" . | nindent 10 }}
          bin/generate-zookeeper-config.sh conf/zookeeper.conf;
          OPTS="${OPTS} -Dlog4j2.formatMsgNoLookups=true" exec bin/pulsar zookeeper;
        ports:
        # prometheus needs to access /metrics endpoint
        - name: http
          containerPort: {{ .Values.zookeeper.ports.http }}
        - name: client
          containerPort: {{ .Values.zookeeper.ports.client }}
        - name: follower
          containerPort: {{ .Values.zookeeper.ports.follower }}
        - name: leader-election
          containerPort: {{ .Values.zookeeper.ports.leaderElection }}
        {{- if and .Values.tls.enabled .Values.tls.zookeeper.enabled }}
        - name: client-tls
          containerPort: {{ .Values.zookeeper.ports.clientTls }}
        {{- end }}
        env:
        - name: ZOOKEEPER_SERVERS
          value:
            {{- $global := . }}
            {{ range $i, $e := until (.Values.zookeeper.replicaCount | int) }}{{ if ne $i 0 }},{{ end }}{{ template "pulsar.fullname" $global }}-{{ $global.Values.zookeeper.component }}-{{ printf "%d" $i }}{{ end }}
        envFrom:
        - configMapRef:
            name: "{{ template "pulsar.fullname" . }}-{{ .Values.zookeeper.component }}"
        {{- if .Values.zookeeper.probe.readiness.enabled }}
        {{- if and .Values.rbac.enabled .Values.rbac.psp }}
        securityContext:
          readOnlyRootFilesystem: false
        {{- end}}
        readinessProbe:
          exec:
            command:
            - bin/pulsar-zookeeper-ruok.sh
          initialDelaySeconds: {{ .Values.zookeeper.probe.readiness.initialDelaySeconds }}
          periodSeconds: {{ .Values.zookeeper.probe.readiness.periodSeconds }}
          timeoutSeconds: {{ .Values.zookeeper.probe.readiness.timeoutSeconds }}
          failureThreshold: {{ .Values.zookeeper.probe.readiness.failureThreshold }}
        {{- end }}
        {{- if .Values.zookeeper.probe.liveness.enabled }}
        livenessProbe:
          exec:
            command:
            - bin/pulsar-zookeeper-ruok.sh
          initialDelaySeconds: {{ .Values.zookeeper.probe.liveness.initialDelaySeconds }}
          periodSeconds: {{ .Values.zookeeper.probe.liveness.periodSeconds }}
          timeoutSeconds: {{ .Values.zookeeper.probe.liveness.timeoutSeconds }}
          failureThreshold: {{ .Values.zookeeper.probe.liveness.failureThreshold }}
        {{- end }}
        {{- if .Values.zookeeper.probe.startup.enabled }}
        startupProbe:
          exec:
            command:
            - bin/pulsar-zookeeper-ruok.sh
          initialDelaySeconds: {{ .Values.zookeeper.probe.startup.initialDelaySeconds }}
          periodSeconds: {{ .Values.zookeeper.probe.startup.periodSeconds }}
          timeoutSeconds: {{ .Values.zookeeper.probe.startup.timeoutSeconds }}
          failureThreshold: {{ .Values.zookeeper.probe.startup.failureThreshold }}
        {{- end }}
        volumeMounts:
        - name: "{{ template "pulsar.fullname" . }}-{{ .Values.zookeeper.component }}-{{ .Values.zookeeper.volumes.data.name }}"
          mountPath: /pulsar/data
        {{- if and .Values.tls.enabled .Values.tls.zookeeper.enabled }}
        - mountPath: "/pulsar/certs/zookeeper"
          name: zookeeper-certs
          readOnly: true
        - mountPath: "/pulsar/certs/ca"
          name: ca
          readOnly: true
        - name: keytool
          mountPath: "/pulsar/keytool/keytool.sh"
          subPath: keytool.sh
        {{- end }}
        {{- if .Values.zookeeper.extraVolumeMounts }}
{{ toYaml .Values.zookeeper.extraVolumeMounts | indent 8 }}
        {{- end }}
      volumes:
      {{- if not (and (and .Values.volumes.persistence .Values.volumes.persistence) .Values.zookeeper.volumes.persistence) }}
      - name: "{{ template "pulsar.fullname" . }}-{{ .Values.zookeeper.component }}-{{ .Values.zookeeper.volumes.data.name }}"
        emptyDir: {}
      {{- end }}
      {{- if .Values.zookeeper.extraVolumes }}
{{ toYaml .Values.zookeeper.extraVolumes | indent 6 }}
      {{- end }}
      {{- if and .Values.tls.enabled .Values.tls.zookeeper.enabled }}
      - name: zookeeper-certs
        secret:
          secretName: "{{ .Release.Name }}-{{ .Values.tls.zookeeper.cert_name }}"
          items:
            - key: tls.crt
              path: tls.crt
            - key: tls.key
              path: tls.key
      - name: ca
        secret:
          secretName: "{{ .Release.Name }}-{{ .Values.tls.ca_suffix }}"
          items:
            - key: ca.crt
              path: ca.crt
      - name: keytool
        configMap:
          name: "{{ template "pulsar.fullname" . }}-keytool-configmap"
          defaultMode: 0755
      {{- end}}
{{- if and (and .Values.persistence .Values.volumes.persistence) .Values.zookeeper.volumes.persistence }}
  volumeClaimTemplates:
  - metadata:
      name: "{{ template "pulsar.fullname" . }}-{{ .Values.zookeeper.component }}-{{ .Values.zookeeper.volumes.data.name }}"
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: {{ .Values.zookeeper.volumes.data.size }}
    {{- if .Values.zookeeper.volumes.data.storageClassName }}
      storageClassName: "{{ .Values.zookeeper.volumes.data.storageClassName }}"
    {{- else if and (not (and .Values.volumes.local_storage .Values.zookeeper.volumes.data.local_storage)) .Values.zookeeper.volumes.data.storageClass }}
      storageClassName: "{{ template "pulsar.fullname" . }}-{{ .Values.zookeeper.component }}-{{ .Values.zookeeper.volumes.data.name }}"
    {{- else if and .Values.volumes.local_storage .Values.zookeeper.volumes.data.local_storage }}
      storageClassName: "local-storage"
    {{- end }}
{{- end }}
{{- end }}
