---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: consul
  namespace: consul
spec:
  selector:
      matchLabels:
        app: consul
  serviceName: consul
  replicas: 3
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: consul
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: app
                    operator: In
                    values:
                      - consul
              topologyKey: kubernetes.io/hostname
      terminationGracePeriodSeconds: 10
      serviceAccountName: consul
      securityContext:
        fsGroup: 1000
      containers:
        - name: consul-exporter
          args:
          - --consul.server=127.0.0.1:8500
          - --consul.ca-file=/etc/tls/ca.pem
          - --consul.cert-file=/etc/tls/consul.pem
          - --consul.key-file=/etc/tls/consul-key.pem
          image: prom/consul-exporter:v0.4.0
          ports:
          - name: metrics
            containerPort: 9107
          volumeMounts:
          - mountPath: /etc/tls
            name: tls
        - name: consul
          image: consul:1.6.1
          env:
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: GOSSIP_ENCRYPTION_KEY
              valueFrom:
                secretKeyRef:
                  name: consul
                  key: gossip-encryption-key
            - name: NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
          args:
            - "consul"
            - "agent"
            - "-advertise=$(POD_IP)"
            - "-bind=0.0.0.0"
            - "-bootstrap-expect=3"
            - "-retry-join"
            - "provider=k8s namespace=$(NAMESPACE) label_selector=\"app=consul\""
            - "-client=0.0.0.0"
            - "-config-file=/consul/myconfig/config.json"
            - "-datacenter=dc1"
            - "-data-dir=/consul/data"
            - "-domain=cluster.local"
            - "-encrypt=$(GOSSIP_ENCRYPTION_KEY)"
            - "-server"
            - "-ui"
            - "-disable-host-node-id"
          readinessProbe:
            # NOTE(mitchellh): when our HTTP status endpoints support the
            # proper status codes, we should switch to that. This is temporary.
            exec:
              command:
                - "/bin/sh"
                - "-ec"
                - |
                  curl http://127.0.0.1:8500/v1/status/leader 2>/dev/null | \
                  grep -E '".+"'
            failureThreshold: 2
            initialDelaySeconds: 120
            periodSeconds: 4
            successThreshold: 2
            timeoutSeconds: 5
          livenessProbe:
            exec:
              command:
              - consul
              - members
              - -http-addr=http://127.0.0.1:8500
            initialDelaySeconds: 300
            timeoutSeconds: 5
          volumeMounts:
            - name: data
              mountPath: /consul/data
            - name: tls
              mountPath: /etc/tls
            - name: config
              mountPath: /consul/myconfig
          lifecycle:
            preStop:
              exec:
                command: [
                    "sh", "-c",
                    "consul leave"
                  ]
          ports:
            - containerPort: 8500
              name: ui-port
            - containerPort: 8400
              name: alt-port
            - containerPort: 53
              name: udp-port
            - containerPort: 8443
              name: https-port
            - containerPort: 8080
              name: http-port
            - containerPort: 8301
              name: serflan
            - containerPort: 8302
              name: serfwan
            - containerPort: 8600
              name: consuldns
            - containerPort: 8300
              name: server
      volumes:
        - name: tls
          secret:
            secretName: consul
        - name: config
          configMap:
            name: consul
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        storageClassName: rook-ceph-block
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 700Mi
