x-defaults: &defaults
  restart: unless-stopped
  logging:
    driver: json-file
    options:
      max-size: 100m
      max-file: '3'

services:
  etcd:
    <<: *defaults
    image: quay.io/coreos/etcd:${ETCD_VERSION:-v3.6.0}
    ports:
      - '${ETCD_CLIENT_PORT_OVERRIDE:-2379}:2379'
      - '${ETCD_PEER_PORT_OVERRIDE:-2380}:2380'
    volumes:
      - etcd_data:/etcd-data
    environment:
      - TZ=${TZ:-UTC}
      - ETCD_NAME=${ETCD_NAME:-etcd-node}
      - ETCD_DATA_DIR=/etcd-data
      - ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
      - ETCD_ADVERTISE_CLIENT_URLS=http://localhost:2379
      - ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
      - ETCD_INITIAL_ADVERTISE_PEER_URLS=http://localhost:2380
      - ETCD_INITIAL_CLUSTER=${ETCD_INITIAL_CLUSTER:-etcd-node=http://localhost:2380}
      - ETCD_INITIAL_CLUSTER_STATE=${ETCD_INITIAL_CLUSTER_STATE:-new}
      - ETCD_INITIAL_CLUSTER_TOKEN=${ETCD_INITIAL_CLUSTER_TOKEN:-etcd-cluster}
      - ETCD_AUTO_COMPACTION_RETENTION=${ETCD_AUTO_COMPACTION_RETENTION:-1}
      - ETCD_QUOTA_BACKEND_BYTES=${ETCD_QUOTA_BACKEND_BYTES:-2147483648}
      - ETCD_HEARTBEAT_INTERVAL=${ETCD_HEARTBEAT_INTERVAL:-100}
      - ETCD_ELECTION_TIMEOUT=${ETCD_ELECTION_TIMEOUT:-1000}
      - ETCD_ENABLE_V2=${ETCD_ENABLE_V2:-false}
    command:
      - etcd
      - --name=${ETCD_NAME:-etcd-node}
      - --data-dir=/etcd-data
      - --listen-client-urls=http://0.0.0.0:2379
      - --advertise-client-urls=http://localhost:2379
      - --listen-peer-urls=http://0.0.0.0:2380
      - --initial-advertise-peer-urls=http://localhost:2380
      - --initial-cluster=${ETCD_INITIAL_CLUSTER:-etcd-node=http://localhost:2380}
      - --initial-cluster-state=${ETCD_INITIAL_CLUSTER_STATE:-new}
      - --initial-cluster-token=${ETCD_INITIAL_CLUSTER_TOKEN:-etcd-cluster}
      - --auto-compaction-retention=${ETCD_AUTO_COMPACTION_RETENTION:-1}
      - --quota-backend-bytes=${ETCD_QUOTA_BACKEND_BYTES:-2147483648}
      - --heartbeat-interval=${ETCD_HEARTBEAT_INTERVAL:-100}
      - --election-timeout=${ETCD_ELECTION_TIMEOUT:-1000}
      - --enable-v2=${ETCD_ENABLE_V2:-false}
    deploy:
      resources:
        limits:
          cpus: ${ETCD_CPU_LIMIT:-1.0}
          memory: ${ETCD_MEMORY_LIMIT:-1G}
        reservations:
          cpus: ${ETCD_CPU_RESERVATION:-0.1}
          memory: ${ETCD_MEMORY_RESERVATION:-256M}
    healthcheck:
      test: [CMD, etcdctl, endpoint, health]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s

volumes:
  etcd_data:
