#!/bin/bash

source /etc/profile

MASTER_IP=""

function tip() {
    echo "Usage:"
    echo "sh kubeadm-master-init.sh [-m MASTER_IP] [-h help]"
    echo "Description:"
    echo "MASTER_IP, kubeadm master node ip"
    exit -1
}

function checkpoint() {
    if [ $1 == 0 ]; then
        echo -e "\033[32m \ndone! \033[0m" 
    else
        echo -e "\033[31m \nfailed~ \033[0m"
        exit 1
    fi
}

while getopts 'm:h' OPT; do
    case $OPT in
        m) MASTER_IP="$OPTARG";;
        h) tip;;
        ?) tip;;
    esac
done

function stage_info() {
    info_msg=$1
    echo -e "\033[36m \n======================== ${info_msg} ========================\n \033[0m"
}


stage_info "参数检查"
if [ "${MASTER_IP}" == "" ]; then
    echo -e "MASTER_IP is required, eg.'sh kubeadm-master-init.sh -master 1.2.3.4' \n\n"
    exit 1
else
    echo MASTER_IP: ${MASTER_IP}
fi
checkpoint $?


stage_info "hosts 修改"
cat >> /etc/hosts << EOF
10.192.21.21 k8s.io
10.192.21.21 k8s.gcr.io
10.214.34.164 git-sa.nie.netease.com
EOF
checkpoint $?


stage_info "开启ipv4转发"
cat > /etc/sysctl.conf << EOF 
net.ipv4.ip_forward = 1
EOF
sysctl -p /etc/sysctl.conf
checkpoint $?


stage_info "开启流量桥接"
cat > /etc/modules-load.d/k8s.conf << EOF 
br_netfilter
EOF
cat > /etc/sysctl.d/k8s.conf << EOF 
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
checkpoint $?


stage_info "关闭 swap"
swapoff -a && sed -ri 's/.*swap.*/#&/' /etc/fstab
checkpoint $?


stage_info "端口放通"
iptables -I INPUT -p tcp --dport 6001 -j ACCEPT && \
iptables -I INPUT -p udp --dport 8472 -j ACCEPT && \
iptables -I INPUT -p udp --dport 8285 -j ACCEPT && \
iptables -I INPUT -p tcp --dport 30000:32767 -j ACCEPT && \
iptables -I INPUT -p tcp --dport 27017 -j ACCEPT && \
iptables -I INPUT -p tcp --dport 8081 -j ACCEPT && \
iptables -I INPUT -p tcp --dport 80 -j ACCEPT && \
iptables -I INPUT -p tcp --dport 18080 -j ACCEPT && \
iptables -I INPUT -p tcp --dport 18090 -j ACCEPT && \
iptables -I INPUT -p tcp --dport 18888 -j ACCEPT && \
iptables -I INPUT -p tcp --dport 8000 -j ACCEPT && \
iptables -I INPUT -p tcp --dport 18000 -j ACCEPT && \
iptables -I INPUT -p tcp --dport 5000 -j ACCEPT && \
iptables -I INPUT -p udp --dport 111 -j ACCEPT && \
iptables -I INPUT -p tcp --dport 111 -j ACCEPT && \
iptables -I INPUT -p udp --dport 2049 -j ACCEPT && \
iptables -I INPUT -p tcp --dport 2049 -j ACCEPT && \
iptables -I INPUT -p tcp --dport 10252 -j ACCEPT && \
iptables -I INPUT -p tcp --dport 10251 -j ACCEPT && \
iptables -I INPUT -p tcp --dport 10250 -j ACCEPT && \
iptables -I INPUT -p tcp --dport 2380 -j ACCEPT && \
iptables -I INPUT -p tcp --dport 2379 -j ACCEPT && \
iptables -I INPUT -p tcp --dport 16443 -j ACCEPT && \
iptables -I INPUT -p tcp --dport 6443 -j ACCEPT

checkpoint $?

stage_info "iptables 持久化"
apt-get install -y iptables-persistent
netfilter-persistent save
checkpoint $?

stage_info "Docker 部署 (版本选择: 24.0.5)"
# 首先下载安装脚本到本地
curl -fsSL http://files.yingchi.io/scripts/docker-install-china.sh -o docker-install-china.sh
# 给脚本添加执行权限
chmod +x docker-install-china.sh
# 执行安装脚本
./docker-install-china.sh --source mirrors.tuna.tsinghua.edu.cn/docker-ce --source-registry docker.m.daocloud.io --install-latested false --ignore-backup-tips

mkdir -p /home/cld/var/lib/docker
cat > /etc/docker/daemon.json <<EOF
{
  "data-root": "/home/cld/var/lib/docker",
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "300m",
    "max-file": "5"
  },
  "default-ulimits": {
    "nofile": {
      "Name": "nofile",
      "Hard": 655360,
      "Soft": 655360
    },
    "nproc": {
      "Name": "nproc",
      "Hard": 655360,
      "Soft": 655360
    }
  },
  "live-restore": true,
  "oom-score-adjust": -1000,
  "max-concurrent-downloads": 10,
  "max-concurrent-uploads": 10,
  "storage-driver": "overlay2",
  "exec-opts": ["native.cgroupdriver=systemd"],
  "registry-mirrors": [
    "https://ncr-mirror.nie.netease.com:1443"
  ]
}
EOF
systemctl daemon-reload && systemctl enable docker && systemctl restart docker
checkpoint $?


stage_info "kubernetes 源"
cat > /etc/apt/sources.list.d/kubernetes.list << EOF
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
apt-get update
checkpoint $?


stage_info "安装 kubeadm / kubelet / kubectl"
apt-get install -y kubeadm=1.23.1-00 kubelet=1.23.1-00 kubectl=1.23.1-00 && \
systemctl start kubelet && \
systemctl enable kubelet
checkpoint $?


stage_info "kubeadm 更换100年证书版本"
mv /usr/bin/kubeadm /usr/bin/kubeadm_bak
wget http://files.yingchi.io/kubeadm/kubeadm_100y_v123 -O /usr/bin/kubeadm && chmod +x /usr/bin/kubeadm
checkpoint $?

stage_info "kubeadm master 初始化"
kubeadm init --image-repository registry.aliyuncs.com/google_containers \
--pod-network-cidr=10.244.0.0/16 \
--kubernetes-version=1.23.1 \
--apiserver-advertise-address=${MASTER_IP}
checkpoint $?

stage_info "kubeconfig 配置"
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
checkpoint $?

stage_info "CNI: Flannel 部署"
kubectl apply -f http://files.yingchi.io/kubernetes/flannel.yaml
checkpoint $?

stage_info "KUBEADM MASTER INIT SUCCESSFULLY !"