apiVersion: v1
kind: ReplicationController
metadata:
  name: etcd-{{cell}}
spec:
  replicas: {{replicas}}
  template:
    metadata:
      labels:
        component: etcd
        cell: {{cell}}
        app: vitess
    spec:
      volumes:
        - name: certs
          hostPath: {path: /etc/ssl/certs}
      containers:
        - name: etcd
          image: vitess/etcd:v2.0.13-lite
          volumeMounts:
            - name: certs
              readOnly: true
              mountPath: /etc/ssl/certs
          resources:
            limits:
              memory: "128Mi"
              cpu: "100m"
          command:
            - bash
            - "-c"
            - >-
              ipaddr=$(hostname -i)

              global_etcd=$ETCD_GLOBAL_SERVICE_HOST:$ETCD_GLOBAL_SERVICE_PORT

              cell="{{cell}}" &&
              local_etcd_host_var="ETCD_${cell^^}_SERVICE_HOST" &&
              local_etcd_port_var="ETCD_${cell^^}_SERVICE_PORT" &&
              local_etcd=${!local_etcd_host_var}:${!local_etcd_port_var}

              if [ "{{cell}}" != "global" ]; then
              until etcdctl -C "http://$global_etcd"
              set "/vt/cells/{{cell}}" "http://$local_etcd"; do
              echo "[$(date)] waiting for global etcd to register cell '{{cell}}'";
              sleep 1;
              done;
              fi

              etcd -name $HOSTNAME -discovery {{discovery}}
              -advertise-client-urls http://$ipaddr:4001
              -initial-advertise-peer-urls http://$ipaddr:7001
              -listen-client-urls http://$ipaddr:4001
              -listen-peer-urls http://$ipaddr:7001

