#cloud-config
---
write_files:
  # Kubeconfig file.
  - path: /etc/kubernetes/worker-kubeconfig.yaml
    owner: root
    permissions: 0755
    content: |
      apiVersion: v1
      kind: Config
      clusters:
      - name: local
        cluster:
          server: http://172.18.18.101:8080
      users:
      - name: kubelet
      contexts:
      - context:
          cluster: local
          user: kubelet
        name: kubelet-context
      current-context: kubelet-context

coreos:
  update:
    reboot-strategy: off
  units:
    - name: etcd-member.service
      command: start
      drop-ins:
        - name: 10-etcd.conf
          content: |
            [Service]
            Environment="ETCD_OPTS=--name \"etcdserver\" --listen-client-urls=http://0.0.0.0:2379 --advertise-client-urls=http://$private_ipv4:2379 --initial-cluster=etcdserver=http://$private_ipv4:2380 --initial-advertise-peer-urls=http://$private_ipv4:2380 --listen-client-urls=http://0.0.0.0:2379,http://0.0.0.0:4001 --listen-peer-urls=http://0.0.0.0:2380"
            Environment="ETCD_IMAGE_TAG=v3.0.12"

    - name: kube-apiserver.service
      command: start
      content: |
        [Unit]
        Description=Kubernetes API Server
        Documentation=https://github.com/GoogleCloudPlatform/kubernetes
        Requires=etcd-member.service
        After=etcd-member.service
        [Service]
        TimeoutStartSec=1800
        ExecStartPre=/usr/bin/wget -N -P /opt/bin https://storage.googleapis.com/kubernetes-release/release/v1.9.6/bin/linux/amd64/kubectl
        ExecStartPre=/usr/bin/wget -N -P /opt/bin https://storage.googleapis.com/kubernetes-release/release/v1.9.6/bin/linux/amd64/kube-apiserver
        ExecStartPre=/usr/bin/chmod +x /opt/bin/kubectl
        ExecStartPre=/usr/bin/chmod +x /opt/bin/kube-apiserver
        ExecStart=/opt/bin/kube-apiserver \
        --allow-privileged=true \
        --etcd-servers=http://$private_ipv4:2379 \
        --runtime-config=extensions/v1beta1=true,extensions/v1beta1/networkpolicies=true,admissionregistration.k8s.io/v1beta1=true \
        --insecure-bind-address=0.0.0.0 \
        --bind-address=172.18.18.101 \
        --admission-control=NamespaceLifecycle,LimitRanger,ServiceAccount,PersistentVolumeLabel,DefaultStorageClass,DefaultTolerationSeconds,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota \
        --advertise-address=$private_ipv4 \
        --service-cluster-ip-range=10.100.0.0/24 \
        --basic-auth-file=/etc/kubernetes/k8s-passwd.csv \
        --authorization-mode=RBAC \
        --tls-cert-file=/etc/kubernetes/apiserver.crt \
        --tls-private-key-file=/etc/kubernetes/apiserver.key \
        --service-account-key-file=/etc/kubernetes/apiserver.crt \
        --audit-log-path=-
        
        Restart=always
        RestartSec=10

    - name: kube-controller-manager.service
      command: start
      content: |
        [Unit]
        Description=Kubernetes Controller Manager
        Documentation=https://github.com/GoogleCloudPlatform/kubernetes
        Requires=kube-apiserver.service
        After=kube-apiserver.service
        [Service]
        TimeoutStartSec=1800
        ExecStartPre=/usr/bin/wget -N -P /opt/bin https://storage.googleapis.com/kubernetes-release/release/v1.9.6/bin/linux/amd64/kube-controller-manager
        ExecStartPre=/usr/bin/chmod +x /opt/bin/kube-controller-manager
        # --cluster-cidr must match the IP Pool defined in the manifest
        ExecStart=/opt/bin/kube-controller-manager \
        --master=$private_ipv4:8080 \
        --service-account-private-key-file=/etc/kubernetes/apiserver.key \
        --allocate-node-cidrs=true \
        --cluster-cidr="192.168.128.0/18" \
        --root-ca-file=/etc/kubernetes/apiserver.crt \
        --cluster-signing-cert-file=/etc/kubernetes/apiserver.crt \
        --cluster-signing-key-file=/etc/kubernetes/apiserver.key
        Restart=always
        RestartSec=10

    - name: kube-scheduler.service
      command: start
      content: |
        [Unit]
        Description=Kubernetes Scheduler
        Documentation=https://github.com/GoogleCloudPlatform/kubernetes
        Requires=kube-apiserver.service
        After=kube-apiserver.service
        [Service]
        TimeoutStartSec=1800
        ExecStartPre=/usr/bin/wget -N -P /opt/bin https://storage.googleapis.com/kubernetes-release/release/v1.9.6/bin/linux/amd64/kube-scheduler
        ExecStartPre=/usr/bin/chmod +x /opt/bin/kube-scheduler
        ExecStart=/opt/bin/kube-scheduler --master=$private_ipv4:8080
        Restart=always
        RestartSec=10

    - name: kubelet.service
      runtime: true
      command: start
      content: |
        [Unit]
        Description=Kubernetes Kubelet
        Documentation=https://github.com/kubernetes/kubernetes
        After=docker.service
        Requires=docker.service

        [Service]
        TimeoutStartSec=1800
        ExecStartPre=/usr/bin/wget -N -P /opt/bin https://storage.googleapis.com/kubernetes-release/release/v1.9.6/bin/linux/amd64/kubelet
        ExecStartPre=/usr/bin/chmod +x /opt/bin/kubelet
        ExecStart=/opt/bin/kubelet \
        --address=0.0.0.0 \
        --allow-privileged=true \
        --kubeconfig /etc/kubernetes/worker-kubeconfig.yaml \
        --cluster-dns=10.100.0.10 \
        --cluster-domain=cluster.local \
        --hostname-override=$private_ipv4 \
        --register-node=true \
        --register-schedulable=false \
        --volume-plugin-dir=/opt/bin/volume-plugins
        Restart=always
        RestartSec=10

        [Install]
        WantedBy=multi-user.target

    - name: kube-proxy.service
      command: start
      content: |
        [Unit]
        Description=Kubernetes Proxy
        Documentation=https://github.com/GoogleCloudPlatform/kubernetes
        Requires=kubelet.service
        After=kubelet.service
        [Service]
        TimeoutStartSec=1800
        ExecStartPre=/usr/bin/wget -N -P /opt/bin  https://storage.googleapis.com/kubernetes-release/release/v1.9.6/bin/linux/amd64/kube-proxy
        ExecStartPre=/usr/bin/chmod +x /opt/bin/kube-proxy
        # --cluster-cidr must match the IP Pool defined in the manifest
        ExecStart=/opt/bin/kube-proxy \
        --master=http://$private_ipv4:8080 \
        --cluster-cidr="192.168.128.0/18" \
        --proxy-mode=iptables \
        Restart=always
        RestartSec=10

    - name: docker-registry.service
      command: start
      content: |
        [Unit]
        Description=Docker Registry
        Documentation=https://docs.docker.com/registry/
        Requires=docker.service
        After=docker.service
        [Service]
        TimeoutStartSec=1800
        ExecStart=/usr/bin/docker run -p 5000:5000 \
        -v /etc/kubernetes:/certs \
        -e REGISTRY_HTTP_ADDR=0.0.0.0:5000 \
        -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/apiserver.crt \
        -e REGISTRY_HTTP_TLS_KEY=/certs/apiserver.key \
        registry:2
        Restart=always
        RestartSec=10
