# --------
# REDIS
# --------
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis-{{ id }}
  namespace: {{ namespace }}
  labels:
    crawl: {{ id }}
    role: redis

spec:
  selector:
    matchLabels:
      crawl: {{ id }}
      role: redis

  serviceName: redis-{{ id }}
  replicas: 1
  podManagementPolicy: Parallel

  # not yet supported
  #persistentVolumeClaimRetentionPolicy:
  #  whenDeleted: Delete
  #  whenScaled: Delete

  volumeClaimTemplates:
    - metadata:
        name: redis-data
        labels:
          crawl: {{ id }}
          role: redis

      spec:
        accessModes:
          - ReadWriteOnce

        resources:
          requests:
            storage: 1Gi

        {% if volume_storage_class %}
        storageClassName: {{ volume_storage_class }}
        {% endif %}

  template:
    metadata:
      labels:
        crawl: {{ id }}
        role: redis

    spec:
      terminationGracePeriodSeconds: 10
      #nodeSelector: {{ crawl_node_selector }}
      volumes:
        - name: shared-redis-conf
          configMap:
            name: shared-redis-conf
            items:
              - key: redis.conf
                path: redis.conf

      affinity:
        nodeAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 1
              preference:
                matchExpressions:
                - key: nodeType
                  operator: In
                  values:
                    - "{{ redis_node_type }}"

        podAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 2
              podAffinityTerm:
                topologyKey: "failure-domain.beta.kubernetes.io/zone"
                labelSelector:
                  matchLabels:
                    job-name: job-{{ id }}
                    crawl: {{ id }}

      tolerations:
        - key: "nodeType"
          operator: "Equal"
          value: "crawling"
          effect: "NoSchedule"

      containers:
        - name: redis
          image: {{ redis_image }}
          imagePullPolicy: {{ redis_image_pull_policy }}

          args: ["/redis-conf/redis.conf", "--appendonly", "yes"]
          volumeMounts:
            - name: redis-data
              mountPath: /data

            - name: shared-redis-conf
              mountPath: /redis-conf

          resources:
            limits:
              cpu: {{ redis_limits_cpu }}
              memory: {{ redis_limits_memory }}

            requests:
              cpu: {{ redis_requests_cpu }}
              memory: {{ redis_requests_memory }}

          readinessProbe:
            exec:
              command:
                - redis-cli
                - ping

---
apiVersion: v1
kind: Service
metadata:
  name: redis-{{ id }}
  labels:
    crawl: {{ id }}
    role: redis
 
spec:
  clusterIP: None
  selector:
    crawl: {{ id }}
    role: redis

  ports:
    - protocol: TCP
      port: 6379
      name: redis
