#!/bin/bash
set -x
sudo apt-get update && sudo apt-get install -y apt-transport-https curl \
  ca-certificates software-properties-common

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

sudo apt-get update
sudo apt-get install -y -o Options::=--force-confdef \
  -o Dpkg::Options::=--force-confnew kubelet kubeadm kubectl docker-ce \
  containerd.io docker-ce-cli

sudo apt-mark hold kubelet kubeadm kubectl

# if you wish to use a private docker repo uncomment this the line below
# and add it to daemon.json
# "insecure-registries" : ["192.168.1.4:5000"]

sudo tee /etc/docker/daemon.json >/dev/null <<EOF
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
EOF

sudo mkdir -p /etc/systemd/system/docker.service.d

# Restart docker.
sudo systemctl daemon-reload
sudo systemctl restart docker

sudo tee /etc/modules-load.d/containerd.conf >/dev/null <<EOF
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

# Setup required sysctl params, these persist across reboots.
sudo sysctl --system
sudo mkdir -p /etc/containerd
sudo containerd config default | sudo tee /etc/containerd/config.toml

# Rebooting a node does not kill the containers they are running resulting
# in a vary long timeout before the system moves forward.
#
# There are various bugs upstream detailing this and it supposed to be fixed a few weeks ago (June)
# This is *not* that fix rather a work around. The containers will not shut down "cleanly".
sudo mkdir -p /etc/systemd/system/containerd.service.d
sudo tee /etc/sysctl.d/system/containerd.service.d/override.conf >/dev/null <<EOF
[Service]
KillMode=mixed
EOF


# Restart containerd
sudo systemctl restart containerd
