apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: consul
    app.kubernetes.io/managed-by: Helmraiser
    chart: consul-helm
    component: server
    heritage: Helm
    release: enterprise-metrics
  name: enterprise-metrics-consul-server
  namespace: enterprise-metrics
spec:
  podManagementPolicy: Parallel
  replicas: 1
  selector:
    matchLabels:
      app: consul
      chart: consul-helm
      component: server
      hasDNS: "true"
      release: enterprise-metrics
  serviceName: enterprise-metrics-consul-server
  template:
    metadata:
      annotations:
        consul.hashicorp.com/config-checksum: ca3d163bab055381827226140568f3bef7eaac187cebd76878e0b63e9e442356
        consul.hashicorp.com/connect-inject: "false"
      labels:
        app: consul
        chart: consul-helm
        component: server
        hasDNS: "true"
        release: enterprise-metrics
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchLabels:
                app: consul
                component: server
                release: enterprise-metrics
            topologyKey: kubernetes.io/hostname
      containers:
      - command:
        - /bin/sh
        - -ec
        - |
          CONSUL_FULLNAME="enterprise-metrics-consul"

          exec /bin/consul agent \
            -advertise="${POD_IP}" \
            -bind=0.0.0.0 \
            -bootstrap-expect=1 \
            -client=0.0.0.0 \
            -config-dir=/consul/config \
            -datacenter=dc1 \
            -data-dir=/consul/data \
            -domain=consul \
            -hcl="connect { enabled = true }" \
            -ui \
            -retry-join=${CONSUL_FULLNAME}-server-0.${CONSUL_FULLNAME}-server.${NAMESPACE}.svc \
            -server
        env:
        - name: POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        image: consul:1.8.4
        lifecycle:
          preStop:
            exec:
              command:
              - /bin/sh
              - -c
              - consul leave
        name: consul
        ports:
        - containerPort: 8500
          name: http
        - containerPort: 8301
          name: serflan
        - containerPort: 8302
          name: serfwan
        - containerPort: 8300
          name: server
        - containerPort: 8600
          name: dns-tcp
          protocol: TCP
        - containerPort: 8600
          name: dns-udp
          protocol: UDP
        readinessProbe:
          exec:
            command:
            - /bin/sh
            - -ec
            - |
              curl http://127.0.0.1:8500/v1/status/leader \
              2>/dev/null | grep -E '".+"'
          failureThreshold: 2
          initialDelaySeconds: 5
          periodSeconds: 3
          successThreshold: 1
          timeoutSeconds: 5
        resources:
          limits:
            cpu: 100m
            memory: 100Mi
          requests:
            cpu: 100m
            memory: 100Mi
        volumeMounts:
        - mountPath: /consul/data
          name: data-enterprise-metrics
        - mountPath: /consul/config
          name: config
      securityContext:
        fsGroup: 1000
      serviceAccountName: enterprise-metrics-consul-server
      terminationGracePeriodSeconds: 30
      volumes:
      - configMap:
          name: enterprise-metrics-consul-server-config
        name: config
  volumeClaimTemplates:
  - metadata:
      name: data-enterprise-metrics
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 10Gi
