#!/bin/sh

apt-get update
apt-get install -y vim

cat << EOF | tee /etc/modules-load.d/k8s.conf

overlay
br_netfilter

EOF

modprobe overlay
modprobe br_netfilter

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf

net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward                 = 1

EOF

sysctl --system

cd /tmp

test -f containerd-1.7.11-linux-amd64.tar.gz || wget https://github.com/containerd/containerd/releases/download/v1.7.11/containerd-1.7.11-linux-amd64.tar.gz
tar Cxzvf /usr/local containerd-1.7.11-linux-amd64.tar.gz

mkdir -p /etc/containerd
containerd config default > config.toml
cp config.toml /etc/containerd

test -f containerd.service || wget https://raw.githubusercontent.com/containerd/containerd/main/containerd.service
cp containerd.service /etc/systemd/system/
systemctl daemon-reload
systemctl enable --now containerd

test -f runc.amd64 || wget https://github.com/opencontainers/runc/releases/download/v1.1.10/runc.amd64
install -m 755 runc.amd64 /usr/local/sbin/runc

test -f cni-plugins-linux-amd64-v1.4.0.tgz || wget https://github.com/containernetworking/plugins/releases/download/v1.4.0/cni-plugins-linux-amd64-v1.4.0.tgz
mkdir -p /opt/cni/bin
tar Cxzvf /opt/cni/bin cni-plugins-linux-amd64-v1.4.0.tgz

sed -i.bak "s/SystemdCgroup.*$/SystemdCgroup = true/g" /etc/containerd/config.toml

systemctl restart containerd

apt-get update
apt-get install -y apt-transport-https ca-certificates curl gpg

mkdir -p /etc/apt/keyrings
test -f /etc/apt/keyrings/kubernetes-apt-keyring.gpg || ( curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg )

echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
apt-get update
apt-get install -y kubelet=1.28.4-1.1 kubeadm=1.28.4-1.1 kubectl=1.28.4-1.1

swapoff -a

sed -i.bak -E "s/^.*swap.*//g" /etc/fstab

apt-get install -y libnss-mdns
sed -i.bak 's/#DNS=.*$/DNS=8.8.8.8 8.8.4.4/g' /etc/systemd/resolved.conf
systemctl enable systemd-resolved
systemctl start  systemd-resolved
mv /etc/resolv.conf /etc/resolv.conf.old
ln -s /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf
