apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis-enterprise
spec:
  serviceName: redis-enterprise
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/hostname: docker-desktop
  template:
    metadata:
      labels:
        app.kubernetes.io/hostname: docker-desktop
    spec:
      nodeSelector:
        dedicated: redis-enterprise
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: app
                    operator: In
                    values:
                      - docker-desktop
              topologyKey: kubernetes.io/hostname
      tolerations:
        - key: dedicated
          operator: "Equal"
          value: redis-enterprise
          effect: "NoSchedule"
      containers:
        - name: redis-enterprise
          image: redislabs/redis:4.5.0-22
          env:
            - name: NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: REDIS_CLUSTER_NAME
              valueFrom:
                configMapKeyRef:
                  name: redis-enterprise
                  key: name
            - name: REDIS_CLUSTER_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: redis-enterprise
                  key: password
            - name: REDIS_CLUSTER_USERNAME
              valueFrom:
                secretKeyRef:
                  name: redis-enterprise
                  key: username
            - name: STATEFULSET_NAME
              value: redis-enterprise
          lifecycle:
            postStart:
              exec:
                command:
                  - "/bin/bash"
                  - "-c"
                  - |
                    # Create a new cluster.
                    sleep 45
                    chown redislabs:redislabs -R /var/opt/redislabs/flash
                    POD_INDEX=$(echo ${HOSTNAME} | cut -d '-' -f 3)
                    if [ "${POD_INDEX}" == "0" ] && [ ! -f /opt/redislabs/config/done ]
                    then
                      /opt/redislabs/bin/rladmin cluster create \
                        name ${REDIS_CLUSTER_NAME} \
                       # license_file /opt/redislabs/config/license.key \
                        username ${REDIS_CLUSTER_USERNAME} \
                        password ${REDIS_CLUSTER_PASSWORD} \
                        flash_enabled \
                        flash_path /var/opt/redislabs/flash
                      touch /opt/redislabs/config/done
                      exit
                    fi
                    # Join an existing cluster.
                    MASTER_IP_ADDRESS=$(getent hosts \
                      "${STATEFULSET_NAME}-0.${STATEFULSET_NAME}.${NAMESPACE}.svc.cluster.local" \
                      | cut -d " " -f 1)
                    NODE_ID=$((${POD_INDEX} + 1))
                    /opt/redislabs/bin/rladmin cluster join \
                      nodes ${MASTER_IP_ADDRESS} \
                      username ${REDIS_CLUSTER_USERNAME} \
                      password ${REDIS_CLUSTER_PASSWORD} \
                      flash_enabled \
                      flash_path /var/opt/redislabs/flash \
                      replace_node ${NODE_ID}
                    exit
          ports:
            - name: discovery
              containerPort: 8001
            - name: ui
              containerPort: 8443
            - name: redis
              containerPort: 26379
          resources:
            requests:
              memory: "2Gi"
              cpu: "500m"
          securityContext:
            capabilities:
              add:
                - SYS_RESOURCE
          volumeMounts:
            #- name: "secrets"
            #  mountPath: "/opt/redislabs/config/license.key"
            #  subPath: license.key
            - name: "config"
              mountPath: "/opt/redislabs/config/ccs-redis.conf"
              subPath: ccs-redis.conf
            - name: "flash"
              mountPath: "/var/opt/redislabs/flash"
      volumes:
        - name: config
          configMap:
            name: redis-enterprise
        - name: secrets
          secret:
            secretName: redis-enterprise
        - name: flash
          hostPath:
            path: "/Users/karan/ssd0"
