{% if not embedded -%}
{% include "bash.sh.j2" %}
{% endif %}

{% macro containerd(cache_only=False) -%}
# https://github.com/containerd/containerd/blob/main/docs/getting-started.md#option-1-from-the-official-binaries
# https://github.com/containerd/containerd/releases
CONTAINERD_RELEASE_VERSION="1.6.17"
ARCH="amd64"
wget --quiet --timestamping https://github.com/containerd/containerd/releases/download/v${CONTAINERD_RELEASE_VERSION}/containerd-${CONTAINERD_RELEASE_VERSION}-linux-${ARCH}.tar.gz
tar -f archive.tar -r containerd-${CONTAINERD_RELEASE_VERSION}-linux-${ARCH}.tar.gz
{% if not cache_only -%}
sudo tar Cxzvf /usr/local containerd-${CONTAINERD_RELEASE_VERSION}-linux-${ARCH}.tar.gz
sudo mkdir -p /usr/local/lib/systemd/system/
curl -sSL https://raw.githubusercontent.com/containerd/containerd/main/containerd.service | sudo tee /usr/local/lib/systemd/system/containerd.service

# https://kubernetes.io/docs/setup/production-environment/container-runtimes/#containerd-systemd
sudo mkdir -p /etc/containerd/
containerd config default | sudo tee /etc/containerd/config.toml
head /etc/containerd/config.toml

tmp=$(mktemp -d /tmp/config.XXXX)
cat /etc/containerd/config.toml | yj -i -tt >$tmp/config.toml.1
cat $tmp/config.toml.1 | yj -ty | yq '.plugins["io.containerd.grpc.v1.cri"].containerd.runtimes.runc.options.SystemdCgroup = true' | yj -i -yt >$tmp/config.toml.2
yq '.plugins["io.containerd.grpc.v1.cri"].containerd.runtimes.runc.options.SystemdCgroup' $tmp/config.toml.1 $tmp/config.toml.2 # show that old is false and new is true
set +e
diff -uw $tmp/config.toml.1 $tmp/config.toml.2
set -e
cat $tmp/config.toml.2 | sudo tee /etc/containerd/config.toml

sudo systemctl daemon-reload
sudo systemctl enable --now containerd
systemctl is-active containerd
{% endif %}

CNI_PLUGINS_VERSION="v1.1.1"
ARCH="amd64"
{% if not cache_only %}
DEST="/opt/cni/bin"
sudo mkdir -p "$DEST"
{% endif -%}
wget --quiet --timestamping "https://github.com/containernetworking/plugins/releases/download/${CNI_PLUGINS_VERSION}/cni-plugins-linux-${ARCH}-${CNI_PLUGINS_VERSION}.tgz"
tar -f archive.tar -r cni-plugins-linux-${ARCH}-${CNI_PLUGINS_VERSION}.tgz
{% if not cache_only -%}
cat cni-plugins-linux-${ARCH}-${CNI_PLUGINS_VERSION}.tgz | sudo tar xz -C "$DEST"
{% endif %}

{% if not cache_only %}
DOWNLOAD_DIR="/usr/local/bin"
sudo mkdir -p "$DOWNLOAD_DIR"
{% endif %}

CRICTL_VERSION="v1.25.0"
ARCH="amd64"
wget --quiet --timestamping "https://github.com/kubernetes-sigs/cri-tools/releases/download/${CRICTL_VERSION}/crictl-${CRICTL_VERSION}-linux-${ARCH}.tar.gz"
tar -f archive.tar -r crictl-${CRICTL_VERSION}-linux-${ARCH}.tar.gz
{% if not cache_only -%}
cat crictl-${CRICTL_VERSION}-linux-${ARCH}.tar.gz | sudo tar xz -C $DOWNLOAD_DIR
{% endif %}

###############################
# install kubeadm and kubelet
###############################
RELEASE="$(curl -sSL https://dl.k8s.io/release/stable.txt)"
ARCH="amd64"
wget --quiet --timestamping https://dl.k8s.io/release/${RELEASE}/bin/linux/${ARCH}/kubeadm
tar -f archive.tar -r kubeadm
wget --quiet --timestamping https://dl.k8s.io/release/${RELEASE}/bin/linux/${ARCH}/kubelet
tar -f archive.tar -r kubelet
{% if not cache_only %}
sudo install --owner root --group root --mode 0755 kubeadm $DOWNLOAD_DIR/kubeadm
sudo install --owner root --group root --mode 0755 kubelet $DOWNLOAD_DIR/kubelet
{% endif %}

{% if not cache_only %}
RELEASE_VERSION="v0.4.0"
sudo mkdir -p /etc/systemd/system/kubelet.service.d
curl -sSL "https://raw.githubusercontent.com/kubernetes/release/${RELEASE_VERSION}/cmd/kubepkg/templates/latest/deb/kubelet/lib/systemd/system/kubelet.service" | sed "s:/usr/bin:${DOWNLOAD_DIR}:g" | sudo tee /etc/systemd/system/kubelet.service
curl -sSL "https://raw.githubusercontent.com/kubernetes/release/${RELEASE_VERSION}/cmd/kubepkg/templates/latest/deb/kubeadm/10-kubeadm.conf" | sed "s:/usr/bin:${DOWNLOAD_DIR}:g" | sudo tee /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
{% endif %}

{% if not cache_only %}
sudo systemctl enable --now kubelet
{% endif %}
{% endmacro %}

{% block content %}{{ containerd(cache_only) }}{% endblock %}
