#
# 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.
#


apiVersion: v1
kind: ConfigMap
metadata:
    name: zookeeper-config
data:
    PULSAR_MEM: "\" -Xms100m -Xmx256m \""
    PULSAR_GC: "\" -XX:+UseG1GC -XX:MaxGCPauseMillis=10\""
---

## Define a disruption budget to ensure there are at least
## 2 ZK servers running all the time
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
    name: zk-budget
spec:
    selector:
        matchLabels:
            app: zk
    minAvailable: 2
---

## Define a StatefulSet for ZK servers
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
    name: zk
    labels:
        app: pulsar
        component: zookeeper
spec:
    serviceName: zookeeper
    replicas: 3
    template:
        metadata:
            labels:
                app: pulsar
                component: zookeeper
                cluster: us-central
            annotations:
                pod.alpha.kubernetes.io/initialized: "true"
                prometheus.io/scrape: "true"
                prometheus.io/port: "8080"

        spec:
            # Make sure multiple pods of ZK don't get scheduled on the
            # same node, unless there are no other available nodes
            affinity:
                podAntiAffinity:
                    preferredDuringSchedulingIgnoredDuringExecution:
                      - weight: 1
                        podAffinityTerm:
                            labelSelector:
                                matchExpressions:
                                  - key: "app"
                                    operator: In
                                    values:
                                      - zookeeper
                            topologyKey: "kubernetes.io/hostname"
            containers:
              - name: zookeeper
                image: apachepulsar/pulsar-all:latest
                command: ["sh", "-c"]
                args:
                  - >
                    bin/apply-config-from-env.py conf/zookeeper.conf &&
                    bin/apply-config-from-env.py conf/pulsar_env.sh &&
                    bin/generate-zookeeper-config.sh conf/zookeeper.conf &&
                    bin/pulsar zookeeper
                ports:
                  - containerPort: 2181
                    name: client
                  - containerPort: 2888
                    name: server
                  - containerPort: 3888
                    name: leader-election
                env:
                  - name: ZOOKEEPER_SERVERS
                    value: zk-0,zk-1,zk-2
                envFrom:
                  - configMapRef:
                        name: zookeeper-config

                readinessProbe:
                    exec:
                        command:
                          - "bin/pulsar-zookeeper-ruok.sh"
                    initialDelaySeconds: 5
                    timeoutSeconds: 5

                livenessProbe:
                    exec:
                        command:
                          - "bin/pulsar-zookeeper-ruok.sh"
                    initialDelaySeconds: 15
                    timeoutSeconds: 5

                volumeMounts:
                  - name: datadir
                    mountPath: /pulsar/data
            volumes:
              - name: datadir
                emptyDir: {}

---

##
## Define the ZooKeeper headless service
##
apiVersion: v1
kind: Service
metadata:
    annotations:
        service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
    name: zookeeper
    labels:
        app: pulsar
        component: zookeeper
spec:
    ports:
      - port: 2888
        name: server
      - port: 3888
        name: leader-election
    clusterIP: None
    selector:
        app: pulsar
        component: zookeeper
