#!/bin/bash
#***************************************************************#
# ScriptName: k3sconfig.sh
# Author: xhy
# Create Date: 2025-03-08 11:30
# Modify Author: xhy
# Modify Date: 2025-03-08 11:30
# Version: v1
#***************************************************************#

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

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

# master keepalived virtual ip address
export K3SHA_VIP=172.24.8.180

# master01 ip address
export K3SHA_IP1=172.24.8.181

# master02 ip address
export K3SHA_IP2=172.24.8.182

# master03 ip address
export K3SHA_IP3=172.24.8.183

# master01 hostname
export K3SHA_HOST1=master01

# master02 hostname
export K3SHA_HOST2=master02

# master03 hostname
export K3SHA_HOST3=master03

# master01 network interface name
export K3SHA_NETINF1=eth0

# master02 network interface name
export K3SHA_NETINF2=eth0

# master03 network interface name
export K3SHA_NETINF3=eth0

# keepalived auth_pass config
export K3SHA_KEEPALIVED_AUTH=ilovek3s

# kubernetes CIDR pod subnet
export K3SHA_PODCIDR=10.10.0.0/16

# kubernetes CIDR svc subnet
export K3SHA_SVCCIDR=10.20.0.0/16

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

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

mkdir -p k3sdir/${K3SHA_HOST1}/{keepalived,haproxy}
mkdir -p k3sdir/${K3SHA_HOST2}/{keepalived,haproxy}
mkdir -p k3sdir/${K3SHA_HOST3}/{keepalived,haproxy}
mkdir -p k3sdir/keepalived
mkdir -p k3sdir/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 k3sdir/keepalived/ http://down.linuxsb.com/mydeploy/k3s/common/k3s-keepalived.conf.tpl
wget -c -P k3sdir/keepalived/ http://down.linuxsb.com/mydeploy/k3s/common/check_apiserver.sh
wget -c -P k3sdir/haproxy/ http://down.linuxsb.com/mydeploy/k3s/common/k3s-haproxy.cfg.tpl
wget -c -P k3sdir/haproxy/ http://down.linuxsb.com/mydeploy/k3s/common/k3s-haproxy.service

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

# create all k3sdir-config.yaml files
echo -e "\n\n\033[33m[INFO] Configure k3sdir file ...\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 k3sdir/keepalived/check_apiserver.sh
cp k3sdir/keepalived/check_apiserver.sh k3sdir/${K3SHA_HOST1}/keepalived
cp k3sdir/keepalived/check_apiserver.sh k3sdir/${K3SHA_HOST2}/keepalived
cp k3sdir/keepalived/check_apiserver.sh k3sdir/${K3SHA_HOST3}/keepalived

sed \
-e "s/K3SHA_KA_STATE/BACKUP/g" \
-e "s/K3SHA_KA_INTF/${K3SHA_NETINF1}/g" \
-e "s/K3SHA_IPLOCAL/${K3SHA_IP1}/g" \
-e "s/K3SHA_KA_PRIO/102/g" \
-e "s/K3SHA_VIP/${K3SHA_VIP}/g" \
-e "s/K3SHA_KA_AUTH/${K3SHA_KEEPALIVED_AUTH}/g" \
k3sdir/keepalived/k3s-keepalived.conf.tpl > k3sdir/${K3SHA_HOST1}/keepalived/keepalived.conf

sed \
-e "s/K3SHA_KA_STATE/BACKUP/g" \
-e "s/K3SHA_KA_INTF/${K3SHA_NETINF2}/g" \
-e "s/K3SHA_IPLOCAL/${K3SHA_IP2}/g" \
-e "s/K3SHA_KA_PRIO/101/g" \
-e "s/K3SHA_VIP/${K3SHA_VIP}/g" \
-e "s/K3SHA_KA_AUTH/${K3SHA_KEEPALIVED_AUTH}/g" \
k3sdir/keepalived/k3s-keepalived.conf.tpl > k3sdir/${K3SHA_HOST2}/keepalived/keepalived.conf

sed \
-e "s/K3SHA_KA_STATE/BACKUP/g" \
-e "s/K3SHA_KA_INTF/${K3SHA_NETINF3}/g" \
-e "s/K3SHA_IPLOCAL/${K3SHA_IP3}/g" \
-e "s/K3SHA_KA_PRIO/100/g" \
-e "s/K3SHA_VIP/${K3SHA_VIP}/g" \
-e "s/K3SHA_KA_AUTH/${K3SHA_KEEPALIVED_AUTH}/g" \
k3sdir/keepalived/k3s-keepalived.conf.tpl > k3sdir/${K3SHA_HOST3}/keepalived/keepalived.conf

echo -e "\n\n\033[32m[SUCC] Configure ${K3SHA_HOST1} & ${K3SHA_HOST2} & ${K3SHA_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/K3SHA_IP1/$K3SHA_IP1/g" \
-e "s/K3SHA_IP2/$K3SHA_IP2/g" \
-e "s/K3SHA_IP3/$K3SHA_IP3/g" \
-e "s/K3SHA_HOST1/${K3SHA_HOST1}/g" \
-e "s/K3SHA_HOST2/${K3SHA_HOST2}/g" \
-e "s/K3SHA_HOST3/${K3SHA_HOST3}/g" \
k3sdir/haproxy/k3s-haproxy.cfg.tpl > k3sdir/haproxy/haproxy.conf

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

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

scp -rp k3sdir/haproxy/haproxy.conf root@${K3SHA_HOST1}:/etc/haproxy/haproxy.cfg
scp -rp k3sdir/haproxy/haproxy.conf root@${K3SHA_HOST2}:/etc/haproxy/haproxy.cfg
scp -rp k3sdir/haproxy/haproxy.conf root@${K3SHA_HOST3}:/etc/haproxy/haproxy.cfg
scp -rp k3sdir/haproxy/k3s-haproxy.service root@${K3SHA_HOST1}:/usr/lib/systemd/system/haproxy.service
scp -rp k3sdir/haproxy/k3s-haproxy.service root@${K3SHA_HOST2}:/usr/lib/systemd/system/haproxy.service
scp -rp k3sdir/haproxy/k3s-haproxy.service root@${K3SHA_HOST3}:/usr/lib/systemd/system/haproxy.service

scp -rp k3sdir/${K3SHA_HOST1}/keepalived/* root@${K3SHA_HOST1}:/etc/keepalived/
scp -rp k3sdir/${K3SHA_HOST2}/keepalived/* root@${K3SHA_HOST2}:/etc/keepalived/
scp -rp k3sdir/${K3SHA_HOST3}/keepalived/* root@${K3SHA_HOST3}:/etc/keepalived/

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

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