{{- if .Values.redis.enabled -}}
apiVersion: dragonflydb.io/v1alpha1
kind: Dragonfly
metadata:
  name: {{ include "Plume.fullname" . }}-redis
  namespace: {{ .Release.Namespace }}
  annotations:
    linkerd.io/inject: disabled
  labels:
    app.kubernetes.io/created-by: dragonfly-operator
    app.kubernetes.io/instance: dragonfly
    {{- include "Plume.labels" . | nindent 4 }}
spec:
  args:
    - --maxmemory=$(MAX_MEMORY)Mi # Graceful memory management (90% of limit)
    - --proactor_threads=1 # Auto-detect CPU cores (optimal threading)
    - --cluster_mode=emulated
  env:
    - name: MAX_MEMORY
      valueFrom:
        resourceFieldRef:
          resource: limits.memory
          divisor: 1Mi
  replicas: {{ .Values.redis.replicas | default "1" }}
  resources:
    requests:
      cpu: 150m
    limits:
      memory: 256Mi
  authentication:
    passwordFromSecret:
      name: {{ .Values.redis.secret.name | quote }}
      key: {{ .Values.redis.secret.key | quote }}
  metrics:
    enabled: {{ .Values.redis.metrics.enabled | default false }}
    port: 6379
  storage:
    requests:
      storage: {{ .Values.redis.size | default "1Gi" }}
  {{- if .Values.redis.backup.enabled }}
  snapshot:
    dir: /data # Change to s3://redis/prod-atlantis-redis
    cron: "0 3 * * *" # Default: every day at 03:00
    enableOnMasterOnly: false
    persistentVolumeClaimSpec:
      accessModes:
      - ReadWriteOnce
      storageClass: {{ .Values.redis.storageClass | default "managed-nfs-storage" }}
      resources:
        requests:
          storage: {{ .Values.redis.size | default "1Gi" }}
  {{- end }}
  podSecurityContext:
    runAsUser: 1000
    fsGroup: 1000
{{- end}}
