#!/bin/bash
:<<EOU
bash -c "$(curl -fsSL http://k8s.ruitong.cn:8080/K8s/install.sh)"
EOU
# defaine var
export USER_PASS=vagrant
export GITHUB_FILE=http://k8s.ruitong.cn:8080/K8s
export LIB_FILE=k8s.lib
export LIB_SRV=${GITHUB_FILE}/INSTALL
export REGISTRY_MIRROR=registry.aliyuncs.com/google_containers
export POD_CIDR=172.16.1.0/16
export SERVICE_CIDR=172.17.1.0/18

until curl -fsSL ${LIB_SRV}/${LIB_FILE} -o /tmp/${LIB_FILE} 2>/dev/null; do
	sleep 1
done
source /tmp/${LIB_FILE}

## Main Area
case $(hostname -s) in
	k8s-master|k8s-worker1|k8s-worker2)
		NODE_NAME=$(hostname -s)
		;;
	*)
		select NODE_NAME in k8s-master k8s-worker{1,2} Quit; do
			if [ "${NODE_NAME}" == "Quit" ]; then exit; fi
			break
		done
		;;
esac

arrow_blue 'Set up a K8s cluster'
	check_env
arrow_blue 'Execute a command as another user'
	user_sudo
arrow_blue 'Specifies whether root can log in using ssh'
	passwd_root

arrow_blue 'Running `hostnamectl hostname '${NODE_NAME}'`...'
	set_system_hostname
arrow_blue 'Convert the current dynamic IP address to a static IP address'
	ip_setting

arrow_blue 'Checks which daemons need to be restarted after library upgrades'
	service_restart_accept
arrow_blue 'List of configured APT data sources'
	sources_list
	software_env

arrow_blue 'Running `timedatectl set-timezone Asia/Shanghai`...'
	timezone_change

arrow_blue 'MUST disable swap'
	swap_off
arrow_blue 'Add space to a logical volume'
	lv_extend

arrow_blue 'Installing a container runtime and Configuring a cgroup driver'
	install_runtime_containerd

arrow_blue 'Installing kubeadm, kubelet and kubectl'
	k8s_install
	kubeadm_tab
	crictl_config

arrow_blue 'Configuring module and kernel for kubeadm preflight'
	kubeadm_preflight

arrow_blue 'Updating /etc/hosts'
	hosts_update

case "$(hostname -s)" in
	*master*)
		arrow_blue 'Initializes a Kubernetes control-plane node'
		kubeadm_init_cmd
			arrow_blue 'Organizing Cluster Access Using kubeconfig Files'
			client_config
				arrow_blue 'Deploy a pod network to the cluster'
					cni_calico_install
			arrow_blue 'Set up autocomplete in bash for kubectl'
				client_tab
			arrow_blue 'use a shorthand alias for kubectl that also works with completion'
				client_alias
		;;
	*worker*)
		arrow_blue 'Initializes a Kubernetes worker node and joins it to the cluster'
			kubeadm_join
		;;
esac

exec bash
