apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app.kubernetes.io/name: keyspace-etcd-group-0
  name: keyspace-etcd-group-0
  namespace: tidb-serverless
spec:
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      partition: 3
  podManagementPolicy: OrderedReady
  replicas: 3
  selector:
    matchLabels:
      app.kubernetes.io/name: keyspace-etcd-group-0
  serviceName: keyspace-etcd-group-0-service
  template:
    metadata:
      labels:
        app.kubernetes.io/name: keyspace-etcd-group-0
        zr-evicter.tidbcloud.com/ignored: "true"
      annotations:
        cluster-autoscaler.kubernetes.io/safe-to-evict: "false"
    spec:
      nodeSelector:
        serverless.tidbcloud.com/node: critical-services
      tolerations:
        - key: serverless.tidbcloud.com/node
          operator: Equal
          value: critical-services
          effect: NoSchedule
        - key: status-taint.cluster-autoscaler.kubernetes.io/zr-evicter
          operator: Exists
          effect: NoSchedule
      topologySpreadConstraints:
        - labelSelector:
            matchLabels:
              app.kubernetes.io/name: keyspace-etcd-group-0
          maxSkew: 1
          topologyKey: topology.kubernetes.io/zone
          whenUnsatisfiable: DoNotSchedule
      initContainers:
        - name: fix-permissions
          image: busybox
          command: [ 'sh', '-c', 'chmod 700 /var/lib/etcd' ]
          volumeMounts:
            - name: datadir
              mountPath: /var/lib/etcd
      containers:
        - command: ["/bin/sh", "-ec"]
          args:
            - |
              INITIAL_CLUSTER="${GROUP_NAME}-0=https://${GROUP_NAME}-0.${SERVICE_NAME}.${CLUSTER_NAMESPACE}:2380,"
              INITIAL_CLUSTER=${INITIAL_CLUSTER}"${GROUP_NAME}-1=https://${GROUP_NAME}-1.${SERVICE_NAME}.${CLUSTER_NAMESPACE}:2380,"
              INITIAL_CLUSTER=${INITIAL_CLUSTER}"${GROUP_NAME}-2=https://${GROUP_NAME}-2.${SERVICE_NAME}.${CLUSTER_NAMESPACE}:2380"
              echo "Generated INITIAL_CLUSTER: $INITIAL_CLUSTER"

              etcd --data-dir=/var/lib/etcd --name=${MY_POD_NAME} \
                --listen-peer-urls=https://0.0.0.0:2380 \
                --listen-client-urls=https://0.0.0.0:2379 \
                --advertise-client-urls=https://${MY_POD_NAME}.${SERVICE_NAME}.${CLUSTER_NAMESPACE}:2379 \
                --initial-advertise-peer-urls=https://${MY_POD_NAME}.${SERVICE_NAME}.${CLUSTER_NAMESPACE}:2380 \
                --initial-cluster-state='new' \
                --initial-cluster-token=${GROUP_NAME} \
                --initial-cluster=${INITIAL_CLUSTER} \
                --auto-compaction-retention=1 \
                --auto-compaction-mode='periodic' \
                --quota-backend-bytes=8589934592 \
                --peer-trusted-ca-file=/var/lib/etcd-tls/ca.crt \
                --peer-cert-file=/var/lib/etcd-tls/tls.crt \
                --peer-key-file=/var/lib/etcd-tls/tls.key \
                --trusted-ca-file=/var/lib/etcd-tls/ca.crt \
                --cert-file=/var/lib/etcd-tls/tls.crt \
                --key-file=/var/lib/etcd-tls/tls.key
          env:
            - name: MY_POD_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.name
            - name: CLUSTER_NAMESPACE
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.namespace
          image: gcr.io/etcd-development/etcd:v3.5.4
          imagePullPolicy: IfNotPresent
          name: etcd
          ports:
            - containerPort: 2380
              name: peer
              protocol: TCP
            - containerPort: 2379
              name: client
              protocol: TCP
          resources:
            limits:
              memory: "8Gi"
              cpu: "4"
            requests:
              memory: "8Gi"
              cpu: "4"
          volumeMounts:
            - name: datadir
              mountPath: /var/lib/etcd
            - name: etcd-tls
              mountPath: /var/lib/etcd-tls
      volumes:
        - name: etcd-tls
          secret:
            secretName: keyspace-etcd-group-0-cluster-secret
  volumeClaimTemplates:
    - metadata:
        name: datadir
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: "20Gi"
        storageClassName: "ebs-sc"
