apiVersion: v1
kind: ConfigMap
metadata:
  name: kv-store-config
  namespace: storage
  labels:
    app.kubernetes.io/name: kv-store
    app.kubernetes.io/component: database
data:
  config.yaml: |
    cluster:
      name: production-kv
      node_id: "${POD_NAME}"
      discovery:
        provider: dns
        endpoints:
          - kv-store-headless.storage.svc.cluster.local
      replication_factor: 3
      minimum_quorum: 2

    server:
      client_address: "0.0.0.0:2379"
      peer_address: "0.0.0.0:2380"
      metrics_address: "0.0.0.0:9090"
      request_timeout: 5s
      max_request_bytes: 1572864

    storage:
      engine: rocksdb
      data_directory: /var/lib/kv-store
      sync_writes: true
      cache_size: 512MiB
      compaction:
        strategy: leveled
        interval: 10m
      snapshots:
        enabled: true
        interval: 30m
        retain: 5

    consensus:
      protocol: raft
      election_timeout: 1500ms
      heartbeat_interval: 100ms
      max_entries_per_append: 256

    security:
      tls:
        enabled: true
        certificate_file: /etc/kv-store/tls/tls.crt
        private_key_file: /etc/kv-store/tls/tls.key
        trusted_ca_file: /etc/kv-store/tls/ca.crt
        require_client_certificate: true
      authentication:
        enabled: true
        token_ttl: 15m

    observability:
      logging:
        level: info
        format: json
      metrics:
        enabled: true
        path: /metrics
      tracing:
        enabled: false

---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: kv-store
  namespace: storage
spec:
  serviceName: kv-store-headless
  replicas: 3
  selector:
    matchLabels:
      app: kv-store
  template:
    metadata:
      labels:
        app: kv-store
    spec:
      terminationGracePeriodSeconds: 60
      containers:
        - name: kv-store
          image: example.com/kv-store:2.4.1
          args:
            - --config=/etc/kv-store/config.yaml
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
          ports:
            - name: client
              containerPort: 2379
            - name: peer
              containerPort: 2380
            - name: metrics
              containerPort: 9090
          readinessProbe:
            httpGet:
              path: /ready
              port: metrics
            initialDelaySeconds: 10
            periodSeconds: 5
          livenessProbe:
            httpGet:
              path: /health
              port: metrics
            initialDelaySeconds: 30
            periodSeconds: 10
          resources:
            requests:
              cpu: 500m
              memory: 1Gi
            limits:
              cpu: "2"
              memory: 4Gi
          volumeMounts:
            - name: data
              mountPath: /var/lib/kv-store
            - name: config
              mountPath: /etc/kv-store/config.yaml
              subPath: config.yaml
            - name: tls
              mountPath: /etc/kv-store/tls
              readOnly: true
      volumes:
        - name: config
          configMap:
            name: kv-store-config
        - name: tls
          secret:
            secretName: kv-store-tls
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
          - ReadWriteOnce
        storageClassName: fast-ssd
        resources:
          requests:
            storage: 100Gi

---
apiVersion: v1
kind: Service
metadata:
  name: kv-store-headless
  namespace: storage
spec:
  clusterIP: None
  publishNotReadyAddresses: true
  selector:
    app: kv-store
  ports:
    - name: client
      port: 2379
      targetPort: client
    - name: peer
      port: 2380
      targetPort: peer

---
apiVersion: v1
kind: Service
metadata:
  name: kv-store
  namespace: storage
spec:
  type: ClusterIP
  selector:
    app: kv-store
  ports:
    - name: client
      port: 2379
      targetPort: client
    - name: metrics
      port: 9090
      targetPort: metrics