apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    service: cubestore-workers
  name: cubestore-workers
spec:
  replicas: 3 # If you edit this, make sure to edit the CUBESTORE_WORKERS as well
  serviceName: cubestore-workers
  selector:
    matchLabels:
      service: cubestore-workers
  template:
    metadata:
      labels:
        service: cubestore-workers
    spec:
      containers:
        - env: # Refer to https://cube.dev/docs/reference/environment-variables for more options
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: CUBESTORE_META_ADDR
              value: cubestore-router:9999
            - name: CUBESTORE_REMOTE_DIR
              value: /cube/data
              # The $(POD_NAME) is a way of referencing dependent env vars
              # It will resolve to "cubestore-workers-n" where n is the replica number
            - name: CUBESTORE_SERVER_NAME
              value: "$(POD_NAME).cubestore-workers:10000"
              # With 3 replicas the POD_NAMEs will be cubestore-workers-0,cubestore-workers-1, and cubestore-workers-2
              # These will then be needed in the CUBESTORE_WORKERS env var as an array
              # We use "cubestore-workers-0.cubestore-workers:10000" as it points to the headless service
              # See also cubestore-router-statefulset.yaml
            - name: CUBESTORE_WORKERS
              value: cubestore-workers-0.cubestore-workers:10000,cubestore-workers-1.cubestore-workers:10000,cubestore-workers-2.cubestore-workers:10000
            - name: CUBESTORE_WORKER_PORT
              value: "10000"
          image: cubejs/cubestore:v0.28.14
          name: cubestore-workers
          ports:
            - containerPort: 10000
          volumeMounts:
            - mountPath: /cube/data
              name: cubestore-pv
      restartPolicy: Always
      initContainers:
        - name: init-router
          image: busybox
          command: ['sh', '-c', 'until nc -vz cubestore-router:9999; do echo "Waiting for router"; sleep 2; done;']
      volumes:
        - name: cubestore-pv
          persistentVolumeClaim:
            claimName: cubestore-pvc
