#!/bin/bash

echo "[+] Setting sysctl ..."
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.swappiness=0
EOF
modprobe br_netfilter && sysctl -p /etc/sysctl.d/k8s.conf
echo "[+] Settings sysctl OK"

echo "[+] Diabling firewall ..."
systemctl stop firewalld && systemctl disable firewalld
echo "[+] Diabling firewall OK"

echo "[+] Disabling swap ..."
swapoff -a && sed -ri 's/.*swap.*/#&/' /etc/fstab
echo "[+] Disabling swap OK"

echo "[+] Disable SELinux ..."
setenforce 0 && sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
echo "[+] Disable SELinux OK"

echo "[+] Setting Tsinghua repo and installing kubelet kubeadm kubectl containerd ..."
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.tuna.tsinghua.edu.cn/kubernetes/core:/stable:/v1.31/rpm/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.tuna.tsinghua.edu.cn/kubernetes/core:/stable:/v1.31/rpm/repodata/repomd.xml.key
exclude=kubelet kubeadm kubectl cri-tools kubernetes-cni
EOF
dnf clean all -q && dnf makecache -q && dnf install -q -y kubelet kubeadm kubectl --disableexcludes=kubernetes && dnf install -y -q containerd vim tar
echo "[+] Setting Tsinghua repo and installing kubelet kubeadm kubectl containerd OK"


echo "[+] Setting kubelet ..."
systemctl enable kubelet.service
echo 'KUBELET_EXTRA_ARGS="--runtime-cgroups=/systemd/system.slice --kubelet-cgroups=/systemd/system.slice"'  > /etc/sysconfig/kubelet
systemctl restart kubelet
echo "[+] Setting kubelet OK"

echo "[+] Setting up containerd ..."
containerd_conf="/etc/containerd/config.toml"
mkdir -p /etc/containerd
containerd config default > "${containerd_conf}"
pause_img=$(kubeadm config images list | grep pause | tail -1)
sed -i "/sandbox_image/s#\".*\"#\"${pause_img}\"#" "${containerd_conf}" 
sed -i "/SystemdCgroup/s/=.*/= true/" "${containerd_conf}"
sed -i '/plugins."io.containerd.grpc.v1.cri".registry.configs/a\[plugins."io.containerd.grpc.v1.cri".registry.configs."registry.k8s.io".tls]\n  insecure_skip_verify = true' /etc/containerd/config.toml
server_path="/etc/systemd/system/containerd.service.d"
mkdir -p "${server_path}"
cat > "${server_path}"/http-proxy.conf << EOF
[Service]
Environment="HTTP_PROXY=http://172.168.18.1:18893"
Environment="HTTPS_PROXY=http://172.168.18.1:18893"
Environment="NO_PROXY=http://172.168.18.1:18893"
EOF
systemctl daemon-reload && systemctl enable containerd && systemctl start containerd
echo "[+] Setting up containerd OK"

echo "[+] Setting crictl ..."
crictl config runtime-endpoint unix:///run/containerd/containerd.sock
crictl config image-endpoint unix:///run/containerd/containerd.sock
echo "[+] Setting crictl OK"

echo "[+] Setting up cni ..."
mkdir -p /opt/cni/bin && wget --no-check-certificate -O /tmp/cni.tgz https://mirrors.yacloud.net/k8s/cni-plugins-linux-amd64-v1.6.1.tgz && tar -xzvf /tmp/cni.tgz -C /opt/cni/bin
wget -O /opt/cni/kube-flannel.yml https://mirrors.yacloud.net/k8s/kube-flannel.yml
echo "[+] Setting up cni OK"
