apiVersion: v1
kind: Namespace
metadata:
  name: kv-store
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: etcd-cluster-config
  namespace: kv-store
data:
  ETCD_INITIAL_CLUSTER: etcd-0=http://etcd-0.etcd-peer.kv-store.svc.cluster.local:2380,etcd-1=http://etcd-1.etcd-peer.kv-store.svc.cluster.local:2380,etcd-2=http://etcd-2.etcd-peer.kv-store.svc.cluster.local:2380
---
apiVersion: v1
kind: Service
metadata:
  name: etcd-peer
  namespace: kv-store
spec:
  clusterIP: None
  publishNotReadyAddresses: true
  selector:
    app: etcd
  ports:
    - name: peer
      port: 2380
      targetPort: 2380
---
apiVersion: v1
kind: Service
metadata:
  name: etcd-client
  namespace: kv-store
spec:
  type: ClusterIP
  selector:
    app: etcd
  ports:
    - name: client
      port: 2379
      targetPort: 2379
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: etcd
  namespace: kv-store
spec:
  serviceName: etcd-peer
  replicas: 3
  selector:
    matchLabels:
      app: etcd
  template:
    metadata:
      labels:
        app: etcd
    spec:
      terminationGracePeriodSeconds: 30
      containers:
        - name: etcd
          image: quay.io/coreos/etcd:v3.5.15
          imagePullPolicy: IfNotPresent
          ports:
            - name: client
              containerPort: 2379
            - name: peer
              containerPort: 2380
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: ETCD_INITIAL_CLUSTER
              valueFrom:
                configMapKeyRef:
                  name: etcd-cluster-config
                  key: ETCD_INITIAL_CLUSTER
          command:
            - /usr/local/bin/etcd
          args:
            - --name=$(POD_NAME)
            - --data-dir=/var/lib/etcd
            - --listen-client-urls=http://0.0.0.0:2379
            - --advertise-client-urls=http://$(POD_NAME).etcd-peer.kv-store.svc.cluster.local:2379
            - --listen-peer-urls=http://0.0.0.0:2380
            - --initial-advertise-peer-urls=http://$(POD_NAME).etcd-peer.kv-store.svc.cluster.local:2380
            - --initial-cluster=$(ETCD_INITIAL_CLUSTER)
            - --initial-cluster-state=new
            - --initial-cluster-token=kvstore-prod-2026-q3
            - --quota-backend-bytes=8589934592
            - --auto-compaction-mode=periodic
            - --auto-compaction-retention=1h
          readinessProbe:
            exec:
              command:
                - /bin/sh
                - -ec
                - etcdctl --endpoints=http://127.0.0.1:2379 endpoint health
            initialDelaySeconds: 10
            periodSeconds: 5
            timeoutSeconds: 3
            failureThreshold: 6
          livenessProbe:
            exec:
              command:
                - /bin/sh
                - -ec
                - etcdctl --endpoints=http://127.0.0.1:2379 endpoint health
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3
          resources:
            requests:
              cpu: 500m
              memory: 1Gi
            limits:
              cpu: "2"
              memory: 4Gi
          volumeMounts:
            - name: data
              mountPath: /var/lib/etcd
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
          - ReadWriteOnce
        storageClassName: fast-ssd
        resources:
          requests:
            storage: 50Gi
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: etcd-quorum
  namespace: kv-store
spec:
  minAvailable: 2
  selector:
    matchLabels:
      app: etcd