---
# Source: valkey/templates/serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: valkey
  labels:
    helm.sh/chart: valkey-0.11.0
    app.kubernetes.io/name: valkey
    app.kubernetes.io/instance: valkey
    app.kubernetes.io/version: "9.1.1"
    app.kubernetes.io/managed-by: Helm
automountServiceAccountToken: false
---
# Source: valkey/templates/init_config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: valkey-init-scripts
  labels:
    helm.sh/chart: valkey-0.11.0
    app.kubernetes.io/name: valkey
    app.kubernetes.io/instance: valkey
    app.kubernetes.io/version: "9.1.1"
    app.kubernetes.io/managed-by: Helm
data:
  init.sh: |-
    #!/bin/sh
    set -eu

    # Default config paths
    VALKEY_CONFIG=${VALKEY_CONFIG_PATH:-/data/conf/valkey.conf}

    LOGFILE="/data/init.log"
    DATA_DIR="/data/conf"

    # Logging function (outputs to stderr and file)
    log() {
      echo "$(date) $1" | tee -a "$LOGFILE" >&2
    }

    # Clean old log if requested
    if [ "${KEEP_OLD_LOGS:-false}" != "true" ]; then
      rm -f "$LOGFILE"
    fi

    if [ -f "$LOGFILE" ]; then
      log "Detected restart of this instance ($HOSTNAME)"
    fi

    log "Creating configuration in $DATA_DIR..."
    mkdir -p "$DATA_DIR"
    rm -f "$VALKEY_CONFIG"


    # Base valkey.conf
    log "Generating base valkey.conf"
    {
      echo "port 6379"
      echo "protected-mode no"
      echo "bind * -::*"
      echo "dir /data"
    } >>"$VALKEY_CONFIG"

    # Append extra configs if present
    if [ -f /usr/local/etc/valkey/valkey.conf ]; then
      log "Appending /usr/local/etc/valkey/valkey.conf"
      cat /usr/local/etc/valkey/valkey.conf >>"$VALKEY_CONFIG"
    fi
    if [ -d /extravalkeyconfigs ]; then
      log "Appending files in /extravalkeyconfigs/"
      cat /extravalkeyconfigs/* >>"$VALKEY_CONFIG"
    fi
---
# Source: valkey/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: valkey
  labels:
    helm.sh/chart: valkey-0.11.0
    app.kubernetes.io/name: valkey
    app.kubernetes.io/instance: valkey
    app.kubernetes.io/version: "9.1.1"
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/component: primary
spec:
  type: ClusterIP
  ports:
    - port: 6379
      targetPort: tcp
      protocol: TCP
      name: tcp
  selector:
    app.kubernetes.io/name: valkey
    app.kubernetes.io/instance: valkey
---
# Source: valkey/templates/deploy_valkey.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: valkey
  labels:
    helm.sh/chart: valkey-0.11.0
    app.kubernetes.io/name: valkey
    app.kubernetes.io/instance: valkey
    app.kubernetes.io/version: "9.1.1"
    app.kubernetes.io/managed-by: Helm
spec:
  replicas: 1
  strategy:
    type: RollingUpdate
  selector:
    matchLabels:
      app.kubernetes.io/name: valkey
      app.kubernetes.io/instance: valkey
  template:
    metadata:
      labels:
        app.kubernetes.io/name: valkey
        app.kubernetes.io/instance: valkey
      annotations:
        checksum/initconfig: c92cb648011b5c355c5ee9b91a62b894
    spec:

      automountServiceAccountToken: false
      serviceAccountName: valkey
      securityContext:
        fsGroup: 1000
        runAsGroup: 1000
        runAsUser: 1000
        seccompProfile:
          type: RuntimeDefault
      initContainers:
        - name: valkey-init
          image: docker.io/valkey/valkey:9.1.1
          imagePullPolicy: IfNotPresent
          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              drop:
              - ALL
            readOnlyRootFilesystem: true
            runAsNonRoot: true
            runAsUser: 1000
          command: [ "/scripts/init.sh" ]
          volumeMounts:
            - name: valkey-data
              mountPath: /data
            - name: scripts
              mountPath: /scripts
      containers:
        - name: valkey
          image: docker.io/valkey/valkey:9.1.1
          imagePullPolicy: IfNotPresent
          command: [ "valkey-server" ]
          args: [ "/data/conf/valkey.conf" ]
          securityContext:
            allowPrivilegeEscalation: false
            capabilities:
              drop:
              - ALL
            readOnlyRootFilesystem: true
            runAsNonRoot: true
            runAsUser: 1000
          env:
            - name: VALKEY_LOG_LEVEL
              value: "notice"
          ports:
            - name: tcp
              containerPort: 6379
              protocol: TCP
          livenessProbe:
            exec:
              command:
              - valkey-cli
              - ping
            failureThreshold: 3
            initialDelaySeconds: 0
            periodSeconds: 10
            timeoutSeconds: 1
          startupProbe:
            exec:
              command:
              - valkey-cli
              - ping
            failureThreshold: 3
            initialDelaySeconds: 0
            periodSeconds: 10
            timeoutSeconds: 1
          resources:
            {}
          volumeMounts:
            - name: valkey-data
              mountPath: /data
      volumes:
        - name: scripts
          configMap:
            name: valkey-init-scripts
            defaultMode: 0555
        - name: valkey-data
          emptyDir: {}
