apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "proglog.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels: {{ include "proglog.labels" . | nindent 4 }}
spec:
  selector:
    matchLabels: {{ include "proglog.selectorLabels" . | nindent 6 }}
  serviceName: {{ include "proglog.fullname" . }}
  replicas: {{ .Values.replicas }}
  template:
    metadata:
      name: {{ include "proglog.fullname" . }}
      labels: {{ include "proglog.labels" . | nindent 8 }}
    spec:
      # initContainers...
      initContainers:
      - name: {{ include "proglog.fullname" . }}-config-init
        image: busybox
        imagePullPolicy: IfNotPresent
        command:          
          - /bin/sh
          - -c
          - |-
            ID=$(echo $HOSTNAME | rev | cut -d- -f1 | rev)
            cat > /var/run/proglog/config.yaml <<EOD
            data-dir: /var/run/proglog/data
            rpc-port: {{.Values.rpcPort}}
            # Make sure the following three key-values are on one line each in
            # your code. I split them across multiple lines to fit them in
            # for the book.
            bind-addr: \
              "$HOSTNAME.proglog.{{.Release.Namespace}}.\svc.cluster.local:\
                {{.Values.serfPort}}"
            bootstrap: $([ $ID = 0 ] && echo true || echo false)
            $([ $ID != 0 ] && echo 'start-join-addrs: \
              "proglog-0.proglog.{{.Release.Namespace}}.svc.cluster.local:\
                {{.Values.serfPort}}"')
            EOD   
        volumeMounts:
        - name: datadir
          mountPath: /var/run/proglog
      # containers...
      containers:
      - name: {{ include "proglog.fullname" . }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        ports:
        - containerPort: {{ .Values.rpcPort }}
          name: rpc
        - containerPort: {{ .Values.serfPort }}
          name: serf
        args:
          - --config-file=/var/run/proglog/config.yaml
        # probes...
        readinessProbe:
          exec:
            command: ["/bin/grpc_health_probe", "-addr=:{{ .Values.rpcPort }}"]
          initialDelaySeconds: 10
        livenessProbe:
          exec:
            command: ["/bin/grpc_health_probe", "-addr=:{{ .Values.rpcPort }}"]
          initialDelaySeconds: 10
        volumeMounts:
        - name: datadir
          mountPath: /var/run/proglog
  volumeClaimTemplates:
  - metadata:
      name: datadir
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: {{ .Values.storage }}
