#cloud-config

write_files:
  - path: /etc/cloud.conf
    permissions: 0600
    content: |
      [Global]
      auth-url = OS_AUTH_URL
      username = OS_USERNAME
      api-key = OS_PASSWORD
      tenant-id = OS_TENANT_NAME
      region = OS_REGION_NAME
      [LoadBalancer]
      subnet-id = 11111111-1111-1111-1111-111111111111
  - path: /opt/bin/git-kubernetes-nginx.sh
    permissions: 0755
    content: |
      #!/bin/bash
      git clone https://github.com/thommay/kubernetes_nginx /opt/kubernetes_nginx
      /usr/bin/cp /opt/.kubernetes_auth /opt/kubernetes_nginx/.kubernetes_auth
      /opt/kubernetes_nginx/git-kubernetes-nginx.sh
  - path: /opt/bin/download-release.sh
    permissions: 0755
    content: |
      #!/bin/bash
      # This temp URL is only good for the length of time specified at cluster creation time.
      # Afterward, it will result in a 403.
      OBJECT_URL="CLOUD_FILES_URL"
      if [ ! -s /opt/kubernetes.tar.gz ]
      then
        echo "Downloading release ($OBJECT_URL)"
        wget "${OBJECT_URL}" -O /opt/kubernetes.tar.gz
        echo "Unpacking release"
        rm -rf /opt/kubernetes || false
        tar xzf /opt/kubernetes.tar.gz -C /opt/
      else
        echo "kubernetes release found. Skipping download."
      fi
  - path: /opt/.kubernetes_auth
    permissions: 0600
    content: |
      KUBE_USER:KUBE_PASSWORD

coreos:
  etcd:
    name: kubernetes-master
    discovery: https://discovery.etcd.io/DISCOVERY_ID
    addr: $private_ipv4:4001
    peer-addr: $private_ipv4:7001
    peer-bind-addr: $private_ipv4:7001

  fleet:
    public-ip: $private_ipv4
    metadata: kubernetes_role=master

  update:
    reboot-strategy: etcd-lock

  units:
    - name: etcd.service
      command: start
    - name: fleet.service
      command: start
    - name: download-release.service
      command: start
      content: |
        [Unit]
        Description=Downloads Kubernetes Release
        After=network-online.target
        Requires=network-online.target
        [Service]
        Type=oneshot
        RemainAfterExit=yes
        ExecStart=/usr/bin/bash /opt/bin/download-release.sh
    - name: kube-apiserver.service
      command: start
      content: |
        [Unit]
        Description=Kubernetes API Server
        Documentation=https://github.com/GoogleCloudPlatform/kubernetes
        After=network-online.target
        Requires=network-online.target
        After=download-release.service
        Requires=download-release.service
        [Service]
        ExecStartPre=/usr/bin/ln -sf /opt/kubernetes/server/bin/kube-apiserver /opt/bin/kube-apiserver
        ExecStartPre=/usr/bin/mkdir -p /var/lib/kube-apiserver
        ExecStart=/opt/bin/kube-apiserver \
        --address=127.0.0.1 \
        --cloud-provider=rackspace \
        --cloud-config=/etc/cloud.conf \
        --etcd-servers=http://127.0.0.1:4001 \
        --logtostderr=true \
        --port=8080 \
        --service-cluster-ip-range=SERVICE_CLUSTER_IP_RANGE \
        --token-auth-file=/var/lib/kube-apiserver/known_tokens.csv \
        --v=2
        Restart=always
        RestartSec=5
    - name: apiserver-advertiser.service
      command: start
      content: |
        [Unit]
        Description=Kubernetes Apiserver Advertiser
        After=etcd.service
        Requires=etcd.service
        After=master-apiserver.service
        [Service]
        ExecStart=/bin/sh -c 'etcdctl set /corekube/apiservers/$public_ipv4 $public_ipv4'
        Restart=always
        RestartSec=120
    - name: kube-controller-manager.service
      command: start
      content: |
        [Unit]
        Description=Kubernetes Controller Manager
        Documentation=https://github.com/GoogleCloudPlatform/kubernetes
        After=network-online.target
        Requires=network-online.target
        After=kube-apiserver.service
        Requires=kube-apiserver.service
        [Service]
        ExecStartPre=/usr/bin/ln -sf /opt/kubernetes/server/bin/kube-controller-manager /opt/bin/kube-controller-manager
        ExecStart=/opt/bin/kube-controller-manager \
        --cloud-provider=rackspace \
        --cloud-config=/etc/cloud.conf \
        --logtostderr=true \
        --master=127.0.0.1:8080 \
        --v=2
        Restart=always
        RestartSec=5
    - name: kube-scheduler.service
      command: start
      content: |
        [Unit]
        Description=Kubernetes Scheduler
        Documentation=https://github.com/GoogleCloudPlatform/kubernetes
        After=network-online.target
        Requires=network-online.target
        After=kube-apiserver.service
        Requires=kube-apiserver.service
        [Service]
        ExecStartPre=/usr/bin/ln -sf /opt/kubernetes/server/bin/kube-scheduler /opt/bin/kube-scheduler
        ExecStart=/opt/bin/kube-scheduler \
        --logtostderr=true \
        --master=127.0.0.1:8080
        Restart=always
        RestartSec=5
    #Running nginx service with --net="host" is a necessary evil until running all k8s services in docker.
    - name: kubernetes-nginx.service
      command: start
      content: |
        [Unit]
        Description=Kubernetes Nginx Service
        After=network-online.target
        Requires=network-online.target
        After=docker.service
        Requires=docker.service
        [Service]
        ExecStartPre=/opt/bin/git-kubernetes-nginx.sh
        ExecStartPre=-/usr/bin/docker rm kubernetes_nginx
        ExecStart=/usr/bin/docker run --rm --net="host" -p "443:443" -t --name "kubernetes_nginx" kubernetes_nginx
        ExecStop=/usr/bin/docker stop kubernetes_nginx
        Restart=always
        RestartSec=15
