apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
  namespace: edoc2
  labels:
    app: redis
spec:
  selector:
    matchLabels:
      app: redis
  serviceName: redis-svc
  replicas: 3
  template:
    metadata:
      labels:
        app: redis
    spec:
      dnsPolicy: ClusterFirst
      initContainers:
      - name: increase-somaxconn-map
        image: registry.edoc2.com:5000/library/busybox:1.28
        command: ["sysctl", "-w", "net.core.somaxconn=20000"]
        securityContext:
          privileged: true
      - name: init-redis
        image: registry.edoc2.com:5000/edoc2v5/redis:6.2.5-v2
        env:
        - name: REDIS_PASSWORD
          valueFrom:
            secretKeyRef:
              name: redis-secret
              key: redisPassword
        - name: POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        command: 
        - bash
        - "-c"
        - |
          set -ex
          ordinal=`hostname | awk -F"-" '{print $2}'`
          if [ ${ordinal} -eq 0 ]; then
              cp /mnt/config-map/master.conf /conf/redis.conf ; cp /mnt/config-map/master.conf /conf/master.conf ; cp /mnt/config-map/slave.conf /conf/slave.conf ; podip=$(hostname -i) ; sed -i "s/.Env.POD_IP/$podip/g" /conf/*.conf
          else
              cp /mnt/config-map/slave.conf /conf/redis.conf ; cp /mnt/config-map/master.conf /conf/master.conf ; cp /mnt/config-map/slave.conf /conf/slave.conf ; podip=$(hostname -i) ; sed -i "s/.Env.POD_IP/$podip/g" /conf/*.conf
          fi
          sed -i "s/redisPassword/${REDIS_PASSWORD}/g"  /conf/*.conf
        volumeMounts:
        - name: rconf
          mountPath: /conf
        - name: config-map
          mountPath: /mnt/config-map
      containers:
      - name: redis
        image: registry.edoc2.com:5000/edoc2v5/redis:6.2.5-v2
        imagePullPolicy: IfNotPresent
#        livenessProbe:
#          exec:
#            command:
#            - rabbitmq-diagnostics
#            - status
#          initialDelaySeconds: 60
#          periodSeconds: 30
#          timeoutSeconds: 15
        #command: ["redis-server"]
        #args:
        #  - "/conf/redis.conf"
        command:
        - bash
        - "-c"
        - |
          sleep 15
          set -ex
          sleep 15
          while true; do
            curl kubernetes.default.svc.cluster.local:443 > /tmp/calico-status.pid
            sleep 30
            if grep -q "server" /tmp/calico-status.pid; then
              ordinal=`hostname | awk -F"-" '{print $2}'`
              if [ ${ordinal} -eq 0 ]; then
                  sleep 3
                  redis-cli -h redis-1.redis-svc -a ZgDSvOoY5Q ping | grep PONG > /tmp/arping.pid &
                  redis-cli -h redis-2.redis-svc -a ZgDSvOoY5Q ping | grep PONG > /tmp/brping.pid &
                  redis-cli -h redis-1.redis-svc -a ZgDSvOoY5Q info | grep role | awk -F ":" '{print $2}' > /tmp/redis1.pid &
                  redis-cli -h redis-2.redis-svc -a ZgDSvOoY5Q info | grep role | awk -F ":" '{print $2}' > /tmp/redis2.pid &
                  sleep 5
                  if grep -q "PONG" /tmp/arping.pid || grep -q "PONG" /tmp/brping.pid; then
                      if grep -q "master" /tmp/redis1.pid; then
                          \cp /conf/slave.conf /conf/redis.conf ; sed -i "s#redis-0.redis-svc#redis-1.redis-svc#g" /conf/redis.conf
                      elif grep -q "master" /tmp/redis2.pid; then
                          \cp /conf/slave.conf /conf/redis.conf ; sed -i "s#redis-0.redis-svc#redis-2.redis-svc#g" /conf/redis.conf
                      else
                          \cp /conf/master.conf /conf/redis.conf
                      fi
                  else
                      \cp /conf/master.conf /conf/redis.conf
                  fi
              else
                  \cp /conf/slave.conf /conf/redis.conf
              fi
              if [ ${ordinal} -eq 1 ]; then
                  sleep 2
                  redis-cli -h redis-0.redis-svc -a ZgDSvOoY5Q ping | grep PONG > /tmp/ping.pid &
                  redis-cli -h redis-2.redis-svc -a ZgDSvOoY5Q ping | grep PONG > /tmp/brping.pid &
                  redis-cli -h redis-0.redis-svc -a ZgDSvOoY5Q info | grep role | awk -F ":" '{print $2}' > /tmp/redis0.pid &
                  redis-cli -h redis-2.redis-svc -a ZgDSvOoY5Q info | grep role | awk -F ":" '{print $2}' > /tmp/redis2.pid &
                  sleep 3
                  if grep -q "PONG" /tmp/ping.pid || grep -q "PONG" /tmp/brping.pid; then
                      if grep -q "master" /tmp/redis0.pid; then
                          \cp /conf/slave.conf /conf/redis.conf
                      elif grep -q "master" /tmp/redis2.pid; then
                          \cp /conf/slave.conf /conf/redis.conf ; sed -i "s#redis-0.redis-svc#redis-2.redis-svc#g" /conf/redis.conf
                      else
                          \cp /conf/slave.conf /conf/redis.conf
                      fi
                  else
                      \cp /conf/slave.conf /conf/redis.conf
                  fi
              else
                  sleep 1
              fi
              if [ ${ordinal} -eq 2 ]; then
                  sleep 2
                  redis-cli -h redis-0.redis-svc -a ZgDSvOoY5Q ping | grep PONG > /tmp/ping.pid &
                  redis-cli -h redis-1.redis-svc -a ZgDSvOoY5Q ping | grep PONG > /tmp/arping.pid &
                  redis-cli -h redis-0.redis-svc -a ZgDSvOoY5Q info | grep role | awk -F ":" '{print $2}' > /tmp/redis0.pid &
                  redis-cli -h redis-1.redis-svc -a ZgDSvOoY5Q info | grep role | awk -F ":" '{print $2}' > /tmp/redis1.pid &
                  sleep 3
                  if grep -q "PONG" /tmp/ping.pid || grep -q "PONG" /tmp/arping.pid; then
                      if grep -q "master" /tmp/redis0.pid; then
                          \cp /conf/slave.conf /conf/redis.conf
                      elif grep -q "master" /tmp/redis1.pid; then
                          \cp /conf/slave.conf /conf/redis.conf ; sed -i "s#redis-0.redis-svc#redis-1.redis-svc#g" /conf/redis.conf
                      else
                          \cp /conf/slave.conf /conf/redis.conf
                      fi
                  else
                      \cp /conf/slave.conf /conf/redis.conf
                  fi
              else
                  sleep 1;
              fi
              #redis-server /conf/redis.conf
              break;
            else
                sleep 1;
            fi
          done
          redis-server /conf/redis.conf
        ports:
        - name: redis
          containerPort: 6379
        volumeMounts:
        - name: rdata
          mountPath: /data
        - name: rconf
          mountPath: /conf
        resources:
          requests:
            cpu: 1500m
            memory: 4Gi
      volumes:
      - name: config-map
        configMap:
          name: redis
      - name: rdata
        hostPath:
          path: /home/data/redis/
      - name: rconf
        hostPath:
          path: /home/data/redis-conf/
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - redis-0
                - redis-1
                - redis-2
            topologyKey: "kubernetes.io/hostname"
      nodeSelector:
        "node.labels.nodelabels": Middleware
#  volumeClaimTemplates:
#  - metadata:
#      name: rdata
#    spec:
#      accessModes:
#      - ReadWriteOnce
#      storageClassName: "local-redis6-storage"
#      resources:
#        requests:
#          storage: 50Gi
#  - metadata: 
#      name: rconf
#    spec:
#      accessModes:
#      - ReadWriteOnce
#      storageClassName: "local-rconf-storage"
#      resources:
#        requests:
#          storage: 200Mi
