#!/bin/bash
#***************************************************************#
# ScriptName: uk8sconfig.sh
# Author: xhy
# Create Date: 2025-03-29 13:01
# Modify Author: xhy
# Modify Date: 2025-04-27 18:13
# Version: v1
#***************************************************************#

#######################################
# 全局变量配置区
#######################################
echo -e "\n\033[33m[INFO] 开始定义全局参数...\033[0m"
sleep 1

declare -A NODES=(
    [master01]="172.24.8.181"
    [master02]="172.24.8.182"
    [master03]="172.24.8.183"
)

# 公共参数
K8SHA_VERSION=v1.33.0
K8SHA_VIP="172.24.8.180"
K8SHA_PORT="6443"
K8SHA_AUTH="ilovek8s"
K8SHA_PODCIDR="10.10.0.0/16"
K8SHA_SVCCIDR="10.20.0.0/16"
K8SHA_DNSIP="10.20.0.10"
K8SHA_NETINF="eth0"

echo -e "\n\033[32m[SUCCESS] 全局参数定义已完成...\033[0m"

set -euo pipefail  # 启用严格模式

#######################################
# 函数定义区
#######################################
# 创建目录结构
init_dirs() {
    echo -e "\n\033[33m[INFO] 开始创建目录结构...\033[0m"
    sleep 1

    for node in "${!NODES[@]}"; do
        mkdir -p k8sdir/${node}/{keepalived,haproxy}
    done
    mkdir -p k8sdir/{keepalived,haproxy,init}

    echo -e "\n\033[32m[SUCCESS] 目录结构创建已完成...\033[0m"
}

# 下载基础配置文件
download_templates() {
    echo -e "\n\033[33m[INFO] 开始下载模板文件...\033[0m"
    sleep 1

    # Keepalived模板
    wget -qcP k8sdir/keepalived/ http://down.linuxsb.com/mydeploy/k8s/common/{uk8s-keepalived.conf.tpl,uk8s-check-apiserver.sh}

    # HAProxy模板
    wget -qcP k8sdir/haproxy/ http://down.linuxsb.com/mydeploy/k8s/common/{uk8s-haproxy.cfg.tpl,k8s-haproxy.service}

    # k8s配置模板
    wget -qcP k8sdir/init/ http://down.linuxsb.com/mydeploy/k8s/${K8SHA_VERSION}/ukubeadm-config.yaml.tpl

    echo -e "\n\033[32m[SUCCESS] 模板文件下载已完成...\033[0m"
}

# 生成HAProxy配置
gen_haproxy_conf() {
    echo -e "\n\033[33m[INFO] 生成 HAProxy 配置...\033[0m"
    sleep 1

    local backend_servers=""
    for node_ip in "${NODES[@]}"; do
        backend_servers+="    server ${node_ip} ${node_ip}:${K8SHA_PORT} check\n"
    done

    # 使用临时文件处理多行替换
    local tmpfile=$(mktemp)
    echo -e "$backend_servers" > "$tmpfile"

    # 替换模板中的占位符
    sed -e "/BACKSERVER/ {
        r $tmpfile
        d
    }" "k8sdir/haproxy/uk8s-haproxy.cfg.tpl" > "k8sdir/haproxy/haproxy.cfg"

    rm -f "$tmpfile"

    echo -e "\n\033[32m[SUCCESS] HAProxy 配置已完成...\033[0m"
}

# 生成Keepalived配置
gen_keepalived_conf() {
    echo -e "\n\033[33m[INFO] 开始生成 KeepAlived 配置...\033[0m"
    sleep 1

    local priority=120
    for node in "${!NODES[@]}"; do
        sed -e "
            s/K8SHA_KA_STATE/BACKUP/g
            s/K8SHA_KA_INTF/${K8SHA_NETINF}/g
            s/K8SHA_IPLOCAL/${NODES[$node]}/g
            s/K8SHA_KA_PRIO/$((priority++))/g
            s/K8SHA_VIP/${K8SHA_VIP}/g
            s/K8SHA_KA_AUTH/${K8SHA_AUTH}/g
        " k8sdir/keepalived/uk8s-keepalived.conf.tpl > "k8sdir/${node}/keepalived/keepalived.conf"

        cp k8sdir/keepalived/uk8s-check-apiserver.sh "k8sdir/${node}/keepalived/check_apiserver.sh"
    done

    echo -e "\n\033[32m[SUCCESS] KeepAlived 配置已完成...\033[0m"
}

# 生成k8s配置文件
gen_k8s_config() {
    echo -e "\n\033[33m[INFO] 开始生成 K8S 配置...\033[0m"
    sleep 1

    # 生成动态 SAN 条目到临时文件
    tmp_sans=$(mktemp)
    {
        # 添加所有节点名称和 IP
        for node in "${!NODES[@]}"; do
            echo "  - $node"
            echo "  - ${NODES[$node]}"
        done
    } > "$tmp_sans"    # 生成动态 SAN 条目到临时文件

    for node in "${!NODES[@]}"; do
        sed -e "
            s|K8SHA_SVCCIDR|${K8SHA_SVCCIDR}|g
            s|K8SHA_PODCIDR|${K8SHA_PODCIDR}|g
            s|K8SHA_VERSION|${K8SHA_VERSION}|g
            s|K8SHA_VIP|${K8SHA_VIP}|g
            /#DYNAMIC_SANS#/ {
                r $tmp_sans
                d
        }
        " k8sdir/init/ukubeadm-config.yaml.tpl > "k8sdir/init/kubeadm-config.yaml"
    done

    rm -rf ${tmp_sans}

    echo -e "\n\033[32m[SUCCESS] K8S 配置已完成...\033[0m"
}

# 分发配置文件
distribute_files() {
    echo -e "\n\033[33m[INFO] 分发配置文件...\033[0m"
    sleep 1

    for node in "${!NODES[@]}"; do
        echo "正在处理节点: $node"

        # HAProxy配置
        cp k8sdir/haproxy/haproxy.cfg k8sdir/${node}/haproxy/
        scp -qp k8sdir/haproxy/haproxy.cfg root@$node:/etc/haproxy/haproxy.cfg
        cp k8sdir/haproxy/k8s-haproxy.service k8sdir/${node}/haproxy/
        scp -qp k8sdir/haproxy/k8s-haproxy.service root@$node:/usr/lib/systemd/system/haproxy.service

        # Keepalived配置
        scp -qp k8sdir/$node/keepalived/* root@$node:/etc/keepalived/
    done

    echo -e "\n\033[32m[SUCCESS] 配置分发已完成...\033[0m"
}

#######################################
# 主执行流程
#######################################
main() {
    init_dirs
    download_templates
    gen_keepalived_conf
    gen_k8s_config
    gen_haproxy_conf
    distribute_files

    # 设置执行权限
    chmod u+x *.sh
    echo -e "\n\033[32m[SUCCESS] 所有配置已完成...\033[0m"
    sleep 1
}

main "$@"
