# kubectl apply -f docs/snippets/tutorials/coredns/etcd.yaml
# kubectl delete -f docs/snippets/tutorials/coredns/etcd.yaml
---
apiVersion: v1
kind: Service
metadata:
  name: etcd
  namespace: default
spec:
  type: ClusterIP
  clusterIP: None
  ports:
  - name: etcd-client
    port: 2379
  - name: etcd-server
    port: 2380
  - name: etcd-metrics
    port: 8080
  selector:
    app: etcd
---
apiVersion: v1
kind: Service
metadata:
  name: etcd-nodeport-external
  namespace: default
spec:
  type: NodePort
  ports:
    - port: 2379
      targetPort: 2379
      nodePort: 32379 # must match kind config port mapping
  selector:
    app: etcd
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: etcd
  namespace: default
spec:
  serviceName: etcd
  replicas: 1
  selector:
    matchLabels:
      app: etcd
  template:
    metadata:
      labels:
        app: etcd
      annotations:
        serviceName: etcd
    spec:
      containers:
      - name: etcd
        image: quay.io/coreos/etcd:v3.5.15
        command:
        - /usr/local/bin/etcd
        - --name=$(HOSTNAME)
        - --listen-peer-urls=$(URI_SCHEME)://0.0.0.0:2380
        - --listen-client-urls=$(URI_SCHEME)://0.0.0.0:2379
        - --advertise-client-urls=$(URI_SCHEME)://$(HOSTNAME).$(SERVICE_NAME):2379
        - --data-dir=/var/lib/etcd
        ports:
        - containerPort: 2379
        volumeMounts:
        - name: data
          mountPath: /var/lib/etcd
        env:
        - name: K8S_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: HOSTNAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: SERVICE_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.annotations['serviceName']
        - name: ETCDCTL_ENDPOINTS
          value: $(HOSTNAME).$(SERVICE_NAME):2379
        - name: URI_SCHEME
          value: "http"
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 50Mi
