apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: blocky-valkey
  labels:
    helm.sh/chart: valkey-0.9.4
    app.kubernetes.io/name: valkey
    app.kubernetes.io/instance: blocky
    app.kubernetes.io/version: "9.1.0"
    app.kubernetes.io/managed-by: Helm
spec:
  serviceName: blocky-valkey-headless
  replicas: 3
  podManagementPolicy: OrderedReady
  selector:
    matchLabels:
      app.kubernetes.io/name: valkey
      app.kubernetes.io/instance: blocky
  volumeClaimTemplates:
    - metadata:
        name: valkey-data
      spec:
        accessModes:
          - ReadWriteOnce
        storageClassName: "ceph-block"
        resources:
          requests:
            storage: "1Gi"
  template:
    metadata:
      labels:
        app.kubernetes.io/name: valkey
        app.kubernetes.io/instance: blocky
      annotations:
        checksum/initconfig: "d89a61a6fa94396539148ff4a7abae20"
    spec:
      automountServiceAccountToken: false
      serviceAccountName: blocky-valkey
      securityContext:
        fsGroup: 1000
        runAsGroup: 1000
        runAsUser: 1000
        seccompProfile:
          type: RuntimeDefault
      initContainers:
        - name: blocky-valkey-init
          image: docker.io/valkey/valkey:9.1.0@sha256:4963247afc4cd33c7d3b2d2816b9f7f8eeebab148d29056c2ca4d7cbc966f2d9
          imagePullPolicy: IfNotPresent
          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              drop:
                - ALL
            readOnlyRootFilesystem: true
            runAsNonRoot: true
            runAsUser: 1000
          command: ["/scripts/init.sh"]
          env:
            - name: POD_INDEX
              valueFrom:
                fieldRef:
                  fieldPath: metadata.labels['apps.kubernetes.io/pod-index']
          volumeMounts:
            - name: valkey-data
              mountPath: /data
            - name: scripts
              mountPath: /scripts
      containers:
        - name: blocky-valkey
          image: docker.io/valkey/valkey:9.1.0@sha256:4963247afc4cd33c7d3b2d2816b9f7f8eeebab148d29056c2ca4d7cbc966f2d9
          imagePullPolicy: IfNotPresent
          command: ["valkey-server"]
          args: ["/data/conf/valkey.conf"]
          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              drop:
                - ALL
            readOnlyRootFilesystem: true
            runAsNonRoot: true
            runAsUser: 1000
          env:
            - name: POD_INDEX
              valueFrom:
                fieldRef:
                  fieldPath: metadata.labels['apps.kubernetes.io/pod-index']
            - name: VALKEY_LOGLEVEL
              value: "notice"
          ports:
            - name: tcp
              containerPort: 6379
              protocol: TCP
          startupProbe:
            exec:
              command: ["sh", "-c", "valkey-cli ping"]
          livenessProbe:
            exec:
              command: ["sh", "-c", "valkey-cli ping"]
          resources:
            requests:
              cpu: 10m
              memory: 20Mi
          volumeMounts:
            - name: valkey-data
              mountPath: /data
        - name: metrics
          image: ghcr.io/oliver006/redis_exporter:v1.84.0@sha256:7ef8e9c26638158fa4e7ad60df8c7e53d1919986753d6c1d2d1876b6ec38d87b
          imagePullPolicy: "IfNotPresent"
          ports:
            - name: metrics
              containerPort: 9121
          startupProbe:
            tcpSocket:
              port: metrics
          livenessProbe:
            tcpSocket:
              port: metrics
          readinessProbe:
            httpGet:
              path: /
              port: metrics
          resources:
            requests:
              cpu: 1m
              memory: 10M
          env:
            - name: REDIS_ALIAS
              value: blocky-valkey
      volumes:
        - name: scripts
          configMap:
            name: blocky-valkey-init-scripts
            defaultMode: 0555
