apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: my-ping-app
spec:
  selector:
    matchLabels:
      app: my-ping-app
  serviceName: pinger
  replicas: 3 # this should match env.name=REPLICAS for the initContainer
  template:
    metadata:
      labels:
        app: my-ping-app
    spec:
      initContainers:
        - name: generate-config
          image: ubuntu:18.04
          command:
            - bash
            - "-c"
            - |
              echo "---" > /mnt/config/pinger.yml
              echo "port: 8080" >> /mnt/config/pinger.yml
              arg=""
              for (( i=0; i<${REPLICAS}; i++ ))
              do
                n="my-ping-app-${i}"
                if [ "${n}" != "${HOSTNAME}" ]
                then
                  arg+="http://${n}.pinger:8080,"
                fi
              done
              echo "address: [${arg}]" >> /mnt/config/pinger.yml
          volumeMounts:
            - name: conf
              mountPath: /mnt/config
          env:
            - name: REPLICAS
              value: "3" # this should match spec.replicas
      containers:
        - name: my-ping-app
          image: voelzmo/pinger:2.6
          args: ["--config-path", "/mnt/config/pinger.yml"]
          volumeMounts:
            - name: conf
              mountPath: /mnt/config
      volumes:
        - name: conf
          emptyDir: {}

