# Copyright 2019 Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Values.name }}-bootstrap
  namespace: {{ .Release.Namespace }}
  labels:
    app: etcd
data:
  bootstrap.sh: |-
    #!/bin/sh
    if [ ! -d /var/etcd/data/new.etcd ]; then
        mkdir /var/etcd/data/new.etcd
    fi

    while true;
    do
      wget http://localhost:8080/initialization/status -S -O status;
      STATUS=`cat status`;
      case $STATUS in
      "New")
            wget http://localhost:8080/initialization/start -S -O - ;;
      "Progress")
            sleep 1;
            continue;;
      "Failed")
            continue;;
      "Successful")
            exec etcd --config-file /bootstrap/etcd.conf.yml
            ;;
      esac;
    done
  etcd.conf.yml: |-
      # This is the configuration file for the etcd server.

      # Human-readable name for this member.
      name: etcd

      client-transport-security:
        # Path to the client server TLS cert file.
        cert-file: /var/etcd/ssl/server/tls.crt

        # Path to the client server TLS key file.
        key-file: /var/etcd/ssl/server/tls.key

        # Enable client cert authentication.
        client-cert-auth: true

        # Path to the client server TLS trusted CA cert file.
        trusted-ca-file: /var/etcd/ssl/ca/ca.crt

        # Client TLS using generated certificates
        auto-tls: false

      # Path to the data directory.
      data-dir: /var/etcd/data/new.etcd

      # List of this member's client URLs to advertise to the public.
      # The URLs needed to be a comma-separated list.
      advertise-client-urls: https://0.0.0.0:2379

      # List of comma separated URLs to listen on for client traffic.
      listen-client-urls: https://0.0.0.0:2379

      # Initial cluster token for the etcd cluster during bootstrap.
      initial-cluster-token: 'new'

      # Initial cluster state ('new' or 'existing').
      initial-cluster-state: 'new'

      # Number of committed transactions to trigger a snapshot to disk.
      snapshot-count: 75000

      # Raise alarms when backend size exceeds the given quota. 0 means use the
      # default quota.
      quota-backend-bytes: 8589934592
