#!/bin/bash
#
# Copyright (c) 2018 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#

set -o errexit
set -o nounset
set -o pipefail

cidir=$(dirname "$0")
source "${cidir}/lib.sh"
KATA_HYPERVISOR="${KATA_HYPERVISOR:-qemu}"

echo "Install Kubernetes components"

cidir=$(dirname "$0")
source /etc/os-release || source /usr/lib/os-release
kubernetes_version=$(get_version "externals.kubernetes.version")
ARCH=$("${cidir}"/kata-arch.sh -d)

if [ "$ID" == "ubuntu" ]; then
	if [ "$(command -v kubelet)" != "" ]; then
		sudo -E apt purge kubelet -y
	fi
	sudo bash -c "cat <<EOF > /etc/apt/sources.list.d/kubernetes.list
	deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /
EOF"
	chronic sudo -E sed -i 's/^[ \t]*//' /etc/apt/sources.list.d/kubernetes.list
	sudo mkdir -p /etc/apt/keyrings/ # For Ubuntu < 20.04
	curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --batch --yes --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg -
	chronic sudo -E apt update
	chronic sudo -E apt install --allow-downgrades -y kubelet kubeadm kubectl
elif [[ "$ID" =~ ^(alinux|centos|fedora|rhel)$ ]]; then
	if [ "$(command -v kubelet)" != "" ]; then
		sudo -E yum autoremove kubelet -y
	fi
	url="https://packages.cloud.google.com/yum/repos/kubernetes-el7-${ARCH}"
	echo "Install ${url} for ${ARCH}"
	sudo bash -c "cat <<EOF > /etc/yum.repos.d/kubernetes.repo
	[kubernetes]
	name=Kubernetes
	baseurl=${url}
	enabled=1
	gpgcheck=1
	repo_gpgcheck=1
	gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF"

	chronic sudo -E sed -i 's/^[ \t]*//' /etc/yum.repos.d/kubernetes.repo
	install_kubernetes_version=$(echo $kubernetes_version | cut -d'-' -f1)
	chronic sudo -E yum install -y kubelet-"$install_kubernetes_version" kubeadm-"$install_kubernetes_version" kubectl-"$install_kubernetes_version" --disableexcludes=kubernetes

	# Disable selinux
	if  [ "$(getenforce)" != "Disabled" ]; then
		chronic sudo -E setenforce 0
		chronic sudo -E sed -i 's/^SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
	fi

	# Packets traversing the bridge should be sent to iptables for processing
	echo br_netfilter | sudo -E tee /etc/modules-load.d/k8s.conf
	sudo -E modprobe -i br_netfilter
	sudo -E bash -c 'echo "net.bridge.bridge-nf-call-ip6tables = 1" > /etc/sysctl.d/k8s.conf'
	sudo -E bash -c 'echo "net.bridge.bridge-nf-call-iptables = 1" >> /etc/sysctl.d/k8s.conf'
	sudo -E bash -c 'echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.d/k8s.conf'
	sudo -E sysctl --system

	sudo -E systemctl enable kubelet
else
	die "Kubernetes configuration not done on $ID"
fi
