# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
  namespace: ${namespace}
spec:
  serviceName: "redis-cluster"
  replicas: ${statefulset_config.replicas}
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
        appCluster: redis-cluster
    spec:
      terminationGracePeriodSeconds: 20
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - redis
              topologyKey: kubernetes.io/hostname
      containers:
      - name: redis
        image: ${image}
        command:
          - "redis-server"
        args:
          - "/conf/redis.conf"
          - "--protected-mode"
          - "no"
        resources:
          requests:
            cpu: ${statefulset_config.resource_requests.cpu}
            ephemeral-storage: ${statefulset_config.volume_claim_size}
            memory: ${statefulset_config.resource_requests.memory}
        ports:
            - name: redis
              containerPort: 6379
              protocol: "TCP"
            - name: cluster
              containerPort: 16379
              protocol: "TCP"
        startupProbe:
          periodSeconds: 5
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 20
          tcpSocket:
            port: redis
        livenessProbe:
          periodSeconds: 5
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 5
          exec:
            command: ["sh", "-c", "/probes/liveness.sh"]
        readinessProbe:
          periodSeconds: 5
          timeoutSeconds: 1
          successThreshold: 1
          failureThreshold: 5
          exec:
            command: ["sh", "-c", "/probes/readiness.sh"]
        volumeMounts:
        - name: conf
          mountPath: /conf
        - name: data
          mountPath: /data
        - name: probes
          mountPath: /probes
          readOnly: true
      volumes:
      - name: conf
        configMap:
          name: redis-cluster
          defaultMode: 493
      - name: probes
        configMap:
          name: redis-probes
          defaultMode: 365
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: ${statefulset_config.volume_claim_size}
