#!/bin/bash
#***************************************************************#
# ScriptName: k8sconfig.sh
# Author: xhy
# Create Date: 2025-02-23 00:27
# Modify Author: xhy
# Modify Date: 2025-02-23 00:27
# Version: v1
#***************************************************************#

#######################################
# set variables below to create the config files, all files will create at ./kubeadm directory
#######################################

echo -e "\n\n\033[33m[INFO] Define correlation variables ...\033[0m"

export K8SVERSION=v1.32.2
export CALICOVERSION=v3.29.2

# master keepalived virtual ip address
export K8SHA_VIP=172.24.8.170

# master01 ip address
export K8SHA_IP1=172.24.8.171

# master02 ip address
export K8SHA_IP2=172.24.8.172

# master03 ip address
export K8SHA_IP3=172.24.8.173

# master01 hostname
export K8SHA_HOST1=master01

# master02 hostname
export K8SHA_HOST2=master02

# master03 hostname
export K8SHA_HOST3=master03

# master01 network interface name
export K8SHA_NETINF1=eth0

# master02 network interface name
export K8SHA_NETINF2=eth0

# master03 network interface name
export K8SHA_NETINF3=eth0

# keepalived auth_pass config
export K8SHA_KEEPALIVED_AUTH=ilovek8s

# kubernetes CIDR pod subnet
export K8SHA_PODCIDR=10.10.0.0/16

# kubernetes CIDR svc subnet
export K8SHA_SVCCIDR=10.20.0.0/16

# kubernetes CIDR pod mtu
export K8SHA_PODMTU=1450

##############################
# please do not modify anything below
##############################

echo -e "\n\n\033[33m[INFO] Create the necessary directory ...\033[0m"
sleep 1

mkdir -p kubeadm/${K8SHA_HOST1}/{keepalived,haproxy}
mkdir -p kubeadm/${K8SHA_HOST2}/{keepalived,haproxy}
mkdir -p kubeadm/${K8SHA_HOST3}/{keepalived,haproxy}
mkdir -p kubeadm/keepalived
mkdir -p kubeadm/haproxy

echo -e "\n\n\033[32m[SUCC] Create directory files success ...\033[0m"
sleep 1

# wget all files

echo -e "\n\n\033[33m[INFO] Download the main configuration file ...\033[0m"
sleep 1

wget -c -P kubeadm/keepalived/ http://down.linuxsb.com/mydeploy/k8s/common/k8s-keepalived.conf.tpl
wget -c -P kubeadm/keepalived/ http://down.linuxsb.com/mydeploy/k8s/common/check_apiserver.sh
wget -c -P kubeadm/haproxy/ http://down.linuxsb.com/mydeploy/k8s/common/k8s-haproxy.cfg.tpl
wget -c -P kubeadm/haproxy/ http://down.linuxsb.com/mydeploy/k8s/common/k8s-haproxy.service
wget -c -P kubeadm/ http://down.linuxsb.com/mydeploy/k8s/${K8SVERSION}/k8simage.sh
wget -c -P kubeadm/ http://down.linuxsb.com/mydeploy/k8s/${K8SVERSION}/kubeadm-config.yaml.tpl
wget -c -P kubeadm/calico/ http://down.linuxsb.com/mydeploy/k8s/calico/${CALICOVERSION}/calico.yaml.tpl

echo -e "\n\n\033[32m[SUCC] Download configuration file successful ...\033[0m"
sleep 1

# create all kubeadm-config.yaml files
echo -e "\n\n\033[33m[INFO] Configure kubeadm file ...\033[0m"
sleep 1

sed \
-e "s/K8SHA_HOST1/${K8SHA_HOST1}/g" \
-e "s/K8SHA_HOST2/${K8SHA_HOST2}/g" \
-e "s/K8SHA_HOST3/${K8SHA_HOST3}/g" \
-e "s/K8SHA_IP1/${K8SHA_IP1}/g" \
-e "s/K8SHA_IP2/${K8SHA_IP2}/g" \
-e "s/K8SHA_IP3/${K8SHA_IP3}/g" \
-e "s/K8SHA_VIP/${K8SHA_VIP}/g" \
-e "s!K8SHA_PODCIDR!${K8SHA_PODCIDR}!g" \
-e "s!K8SHA_SVCCIDR!${K8SHA_SVCCIDR}!g" \
kubeadm/kubeadm-config.yaml.tpl > kubeadm/kubeadm-config.yaml

echo -e "\n\n\033[32m[SUCC] Configure kubeadm file successful ...\033[0m"
sleep 1

# create all keepalived files
echo -e "\n\n\033[33m[INFO] Configure keepalived file ...\033[0m"
sleep 1

chmod u+x kubeadm/keepalived/check_apiserver.sh
cp kubeadm/keepalived/check_apiserver.sh kubeadm/${K8SHA_HOST1}/keepalived
cp kubeadm/keepalived/check_apiserver.sh kubeadm/${K8SHA_HOST2}/keepalived
cp kubeadm/keepalived/check_apiserver.sh kubeadm/${K8SHA_HOST3}/keepalived

sed \
-e "s/K8SHA_KA_STATE/BACKUP/g" \
-e "s/K8SHA_KA_INTF/${K8SHA_NETINF1}/g" \
-e "s/K8SHA_IPLOCAL/${K8SHA_IP1}/g" \
-e "s/K8SHA_KA_PRIO/102/g" \
-e "s/K8SHA_VIP/${K8SHA_VIP}/g" \
-e "s/K8SHA_KA_AUTH/${K8SHA_KEEPALIVED_AUTH}/g" \
kubeadm/keepalived/k8s-keepalived.conf.tpl > kubeadm/${K8SHA_HOST1}/keepalived/keepalived.conf

sed \
-e "s/K8SHA_KA_STATE/BACKUP/g" \
-e "s/K8SHA_KA_INTF/${K8SHA_NETINF2}/g" \
-e "s/K8SHA_IPLOCAL/${K8SHA_IP2}/g" \
-e "s/K8SHA_KA_PRIO/101/g" \
-e "s/K8SHA_VIP/${K8SHA_VIP}/g" \
-e "s/K8SHA_KA_AUTH/${K8SHA_KEEPALIVED_AUTH}/g" \
kubeadm/keepalived/k8s-keepalived.conf.tpl > kubeadm/${K8SHA_HOST2}/keepalived/keepalived.conf

sed \
-e "s/K8SHA_KA_STATE/BACKUP/g" \
-e "s/K8SHA_KA_INTF/${K8SHA_NETINF3}/g" \
-e "s/K8SHA_IPLOCAL/${K8SHA_IP3}/g" \
-e "s/K8SHA_KA_PRIO/100/g" \
-e "s/K8SHA_VIP/${K8SHA_VIP}/g" \
-e "s/K8SHA_KA_AUTH/${K8SHA_KEEPALIVED_AUTH}/g" \
kubeadm/keepalived/k8s-keepalived.conf.tpl > kubeadm/${K8SHA_HOST3}/keepalived/keepalived.conf

echo -e "\n\n\033[32m[SUCC] Configure ${K8SHA_HOST1} & ${K8SHA_HOST2} & ${K8SHA_HOST3} keepalived file successful ...\033[0m"
sleep 1

# create all haproxy files
echo -e "\n\n\033[33m[INFO] Configure haproxy file ...\033[0m"
sleep 1

sed \
-e "s/K8SHA_IP1/$K8SHA_IP1/g" \
-e "s/K8SHA_IP2/$K8SHA_IP2/g" \
-e "s/K8SHA_IP3/$K8SHA_IP3/g" \
-e "s/K8SHA_HOST1/${K8SHA_HOST1}/g" \
-e "s/K8SHA_HOST2/${K8SHA_HOST2}/g" \
-e "s/K8SHA_HOST3/${K8SHA_HOST3}/g" \
kubeadm/haproxy/k8s-haproxy.cfg.tpl > kubeadm/haproxy/haproxy.conf

echo -e "\n\n\033[32m[SUCC] Configure ${K8SHA_HOST1} & ${K8SHA_HOST2} & ${K8SHA_HOST3} haproxy file successful ...\033[0m"
sleep 1

# create calico yaml file
echo -e "\n\n\033[33m[INFO] Configure calico file ...\033[0m"
sleep 1

sed \
-e "s!K8SHA_PODCIDR!${K8SHA_PODCIDR}!g" \
-e "s!K8SHA_PODMTU!${K8SHA_PODMTU}!g" \
kubeadm/calico/calico.yaml.tpl > kubeadm/calico/calico.yaml

echo -e "\n\n\033[32m[SUCC] Configure calico file successful ...\033[0m"
sleep 1

# scp all file
echo -e "\n\n\033[33m[INFO] Copy the configuration file to ${K8SHA_HOST1} & ${K8SHA_HOST2} & ${K8SHA_HOST3} nodes ...\033[0m"
sleep 1

scp -rp kubeadm/haproxy/haproxy.conf root@${K8SHA_HOST1}:/etc/haproxy/haproxy.cfg
scp -rp kubeadm/haproxy/haproxy.conf root@${K8SHA_HOST2}:/etc/haproxy/haproxy.cfg
scp -rp kubeadm/haproxy/haproxy.conf root@${K8SHA_HOST3}:/etc/haproxy/haproxy.cfg
scp -rp kubeadm/haproxy/k8s-haproxy.service root@${K8SHA_HOST1}:/usr/lib/systemd/system/haproxy.service
scp -rp kubeadm/haproxy/k8s-haproxy.service root@${K8SHA_HOST2}:/usr/lib/systemd/system/haproxy.service
scp -rp kubeadm/haproxy/k8s-haproxy.service root@${K8SHA_HOST3}:/usr/lib/systemd/system/haproxy.service

scp -rp kubeadm/${K8SHA_HOST1}/keepalived/* root@${K8SHA_HOST1}:/etc/keepalived/
scp -rp kubeadm/${K8SHA_HOST2}/keepalived/* root@${K8SHA_HOST2}:/etc/keepalived/
scp -rp kubeadm/${K8SHA_HOST3}/keepalived/* root@${K8SHA_HOST3}:/etc/keepalived/

echo -e "\n\n\033[32m[SUCC] Copy the configuration file to ${K8SHA_HOST1} & ${K8SHA_HOST2} & ${K8SHA_HOST3} nodes successful ...\033[0m"
sleep 1

# chmod *.sh
chmod u+x kubeadm/*.sh
