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

x-valkey-node: &valkey-node
  <<: *defaults
  image: ${GLOBAL_REGISTRY:-}valkey/valkey:${VALKEY_VERSION:-8.0-alpine}
  environment:
    - TZ=${TZ:-UTC}
  command:
    - valkey-server
    - --cluster-enabled
    - yes
    - --cluster-config-file
    - nodes.conf
    - --cluster-node-timeout
    - '5000'
    - --appendonly
    - yes
    - --requirepass
    - ${VALKEY_PASSWORD:-passw0rd}
    - --masterauth
    - ${VALKEY_PASSWORD:-passw0rd}
  deploy:
    resources:
      limits:
        cpus: ${VALKEY_CLUSTER_CPU_LIMIT:-0.50}
        memory: ${VALKEY_CLUSTER_MEMORY_LIMIT:-512M}
      reservations:
        cpus: ${VALKEY_CLUSTER_CPU_RESERVATION:-0.25}
        memory: ${VALKEY_CLUSTER_MEMORY_RESERVATION:-256M}
  healthcheck:
    test: [CMD, valkey-cli, -a, '${VALKEY_PASSWORD:-passw0rd}', ping]
    interval: 10s
    timeout: 3s
    retries: 3
    start_period: 5s

services:
  valkey-node-1:
    <<: *valkey-node
    ports:
      - '${VALKEY_PORT_1:-7001}:6379'
      - '${VALKEY_BUS_PORT_1:-17001}:16379'
    volumes:
      - valkey_data_1:/data

  valkey-node-2:
    <<: *valkey-node
    ports:
      - '${VALKEY_PORT_2:-7002}:6379'
      - '${VALKEY_BUS_PORT_2:-17002}:16379'
    volumes:
      - valkey_data_2:/data

  valkey-node-3:
    <<: *valkey-node
    ports:
      - '${VALKEY_PORT_3:-7003}:6379'
      - '${VALKEY_BUS_PORT_3:-17003}:16379'
    volumes:
      - valkey_data_3:/data

  valkey-node-4:
    <<: *valkey-node
    ports:
      - '${VALKEY_PORT_4:-7004}:6379'
      - '${VALKEY_BUS_PORT_4:-17004}:16379'
    volumes:
      - valkey_data_4:/data

  valkey-node-5:
    <<: *valkey-node
    ports:
      - '${VALKEY_PORT_5:-7005}:6379'
      - '${VALKEY_BUS_PORT_5:-17005}:16379'
    volumes:
      - valkey_data_5:/data

  valkey-node-6:
    <<: *valkey-node
    ports:
      - '${VALKEY_PORT_6:-7006}:6379'
      - '${VALKEY_BUS_PORT_6:-17006}:16379'
    volumes:
      - valkey_data_6:/data

  valkey-cluster-init:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}valkey/valkey:${VALKEY_VERSION:-8.0-alpine}
    environment:
      - TZ=${TZ:-UTC}
    depends_on:
      valkey-node-1:
        condition: service_healthy
      valkey-node-2:
        condition: service_healthy
      valkey-node-3:
        condition: service_healthy
      valkey-node-4:
        condition: service_healthy
      valkey-node-5:
        condition: service_healthy
      valkey-node-6:
        condition: service_healthy
    command:
      - sh
      - -c
      - |
        sleep 10
        valkey-cli -a ${VALKEY_PASSWORD:-passw0rd} --cluster create \
          valkey-node-1:6379 \
          valkey-node-2:6379 \
          valkey-node-3:6379 \
          valkey-node-4:6379 \
          valkey-node-5:6379 \
          valkey-node-6:6379 \
          --cluster-replicas 1 \
          --cluster-yes
    restart: no
    deploy:
      resources:
        limits:
          cpus: ${VALKEY_CLUSTER_INIT_CPU_LIMIT:-0.10}
          memory: ${VALKEY_CLUSTER_INIT_MEMORY_LIMIT:-128M}

volumes:
  valkey_data_1:
  valkey_data_2:
  valkey_data_3:
  valkey_data_4:
  valkey_data_5:
  valkey_data_6:
