---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: zookeeper
  namespace: default
  labels:
    name: zookeeper
    part-of: tno
    version: 1.0.0
    component: zookeeper
    managed-by: kustomize
    created-by: jeremy.foster
spec:
  podManagementPolicy: OrderedReady
  replicas: 3
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      statefulset: zookeeper-cluster
  serviceName: zookeeper-headless
  updateStrategy:
    type: RollingUpdate
  volumeClaimTemplates:
    - metadata:
        name: zookeeper-data
        annotations:
          volume.beta.kubernetes.io/storage-class: netapp-block-standard
        labels:
          name: zookeeper-data
          part-of: tno
          component: zookeeper
      spec:
        storageClassName: netapp-block-standard
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 1Gi
  template:
    metadata:
      labels:
        part-of: tno
        component: zookeeper
        statefulset: zookeeper-cluster
    spec:
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      serviceAccountName: tno
      terminationGracePeriodSeconds: 0
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: statefulset
                    operator: In
                    values:
                      - zookeeper-cluster
              topologyKey: "kubernetes.io/hostname"
      containers:
        - name: zookeeper
          image: image-registry.openshift-image-registry.svc:5000/9b301c-tools/zookeeper:dev
          imagePullPolicy: Always
          volumeMounts:
            - name: zookeeper-data
              mountPath: /var/lib/zookeeper/data
          resources:
            requests:
              cpu: 25m
              memory: 125Mi
            limits:
              cpu: 50m
              memory: 1Gi
          ports:
            - containerPort: 2181
              name: client
            - containerPort: 2888
              name: server
            - containerPort: 3888
              name: leader-election
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          # lifecycle:
          #   postStart:
          #     exec:
          #       command:
          #         - "/bin/sh"
          #         - "-c"
          #         - >
          #           export ZOOKEEPER_SERVER_ID=${POD_NAME##*-} &&
          #           echo ${POD_NAME##*-} > ${ZOOKEEPER_DATA_DIR}/myid &&
          env:
            - name: POD_IP
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: status.podIP
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.namespace
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.name
            # This is supposed to extract the ordinal value from the pod name, but it doesn't currently work.  Instead the lifecycle hook will do the trick above.
            # - name: ZOOKEEPER_SERVER_ID
            #   valueFrom:
            #     fieldRef:
            #       apiVersion: v1
            #       fieldPath: metadata.annotations['spec.pod.beta.kubernetes.io/statefulset-index']
            - name: ZOOKEEPER_TICK_TIME
              value: "2000"
            - name: ZOOKEEPER_CLIENT_PORT
              value: "2181"
            - name: ZOOKEEPER_ELECTION_PORT
              value: "3888"
            - name: ZOOKEEPER_SERVER_PORT
              value: "2888"
            - name: ZOOKEEPER_SYNC_LIMIT
              value: "2"
            - name: ZOOKEEPER_LOG_LEVEL
              value: "DEBUG"
            - name: ZOOKEEPER_DATA_DIR
              value: /var/lib/zookeeper/data
            - name: ZOOKEEPER_SERVERS
              value: zookeeper-0.zookeeper-headless:2888:3888;zookeeper-1.zookeeper-headless:2888:3888;zookeeper-2.zookeeper-headless:2888:3888
            - name: KAFKA_OPTS
              value: -Dzookeeper.4lw.commands.whitelist=ruok
          # livenessProbe:
          #   exec:
          #     command:
          #       [
          #         "/bin/bash",
          #         "-c",
          #         'echo "ruok" | nc -w 2 localhost 2181 | grep imok',
          #       ]
          #     initialDelaySeconds: 300
          #     periodSeconds: 10
          #     failureThreshold: 6
          #     successThreshold: 1
          # readinessProbe:
          #   exec:
          #     command:
          #       [
          #         "/bin/bash",
          #         "-c",
          #         "echo 'ruok' | nc -w 2 localhost 2181 | grep imok",
          #       ]
          #     periodSeconds: 10
          #     failureThreshold: 6
          #     successThreshold: 1
          #     initialDelaySeconds: 300
          # readinessProbe:
          #   exec:
          #     command: ['/bin/bash', '-c', 'echo srvr | nc zookeeper 2181 || exit 1']
          #     initialDelaySeconds: 5
          #     periodSeconds: 10
          #     timeoutSeconds: 5
          #     failureThreshold: 6
          #     successThreshold: 1
