#!/bin/bash

# IP=$(ip route get 8.8.8.8 | awk -F"src " 'NR==1{split($2,a," ");print a[1]}')
# INTERFACE=$(ip route get 8.8.8.8 | awk -F"dev " 'NR==1{split($2,a," ");print a[1]}')

# OLDIFS=$IFS
# IFS="."
# read ip1 ip2 ip3 ip4 <<< "$IP"
# VIP="$ip1.$ip2.$ip3.25" # virtual ip addr
# IFS=$OLDIFS

VIP="192.168.111.25" # vip for load balancer
# INTERFACE="wlp0s20f3" # laptop interface
INTERFACE="enp8s0" # laptop interface
# JINTERFACE="wlan0" # jetson interface
JINTERFACE="eth0" # jetson interface

LOOPINTERFACE="lo" # loopback interface for bgp

echo "Using interface: $INTERFACE"
echo "Using jetson interface: $JINTERFACE"
echo "And load-balancer IP: $VIP"

sudo swapoff -a # turn off swap memory
# put load balancer pod spec in the manifests directory
# sudo docker run --network host --rm ghcr.io/kube-vip/kube-vip:main manifest pod \
# --vip "$VIP" \
# --interface "$INTERFACE" \
# --arp \
# --controlplane \
# --leaderElection | sudo tee /etc/kubernetes/manifests/vip.yaml

sudo docker run --network host --rm ghcr.io/kube-vip/kube-vip:main manifest pod \
--vip "$VIP" \
--interface "$INTERFACE" \
--arp \
--controlplane \
--leaderElection | sudo tee /etc/kubernetes/manifests/vip.yaml

sudo sed -i 's/imagePullPolicy: Always/imagePullPolicy: IfNotPresent/g' /etc/kubernetes/manifests/vip.yaml

# load up endpoint in the config file
# configFile="kubeadm-config.yaml"
# newIP="$VIP"
# key="controlPlaneEndpoint"
# sed -r "s/^(\s*${key}\s*:\s*).*/\1"${newIP}:6443"/" -i "$configFile"

# key1="apiServer"
# key2="advertiseAddress"
# sed -r "s/^(\s*${key1}\s*:\s*${key2}\s*).*/\1${newIP}/" -i "$configFile"
#
# key="apiServerCertSANs"
# sed -r "s/^(\s*${key1}\s*:\s*).*/\1${newIP}/" -i "$configFile"

sudo kubeadm init --config kubeadm-config.yaml --upload-certs

if [[ $? -ne 0 ]] ; then
  # some error
  echo "Error! Stopping cluster set up"
  exit 1
fi

mkdir -p $HOME/.kube
sudo cp -f /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

kubectl apply -f flannel.yml

# create control-plane join command and copy it to clip
# OLDIFS=$IFS
# IFS=''
# # ifCMD="ip route get 8.8.8.8 | awk -F'dev ' 'NR==1{split(\$2,a,\" \");print a[1]}'"
# # vipCMD="sudo docker run --network host --rm ghcr.io/kube-vip/kube-vip:main manifest pod --vip "$VIP" \
# # --interface "$JINTERFACE" \
# # --arp \
# # --controlplane \
# # --leaderElection | sudo tee /etc/kubernetes/manifests/vip.yaml"
#
# # echo $(/bin/bash -c "$ifCMD")
# # echo $(/bin/bash -c "$vipCMD")
# IFS=$OLDIFS


# newKey=$(sudo kubeadm init phase upload-certs --upload-certs | sed -n '3~0p')
echo "sudo $(kubeadm token create --print-join-command)" | xclip -sel clip
# echo "sudo swapoff -a && sudo $(kubeadm token create --print-join-


# the following is for a highly-available kubernetes cluster, do NOT fuck with it. it will break first your mind then your soul
# echo "------------------------------------------"
# newKey=$(sudo kubeadm init phase upload-certs --upload-certs | sed -n '3~0p')
# echo "sudo swapoff -a && $vipCMD && sudo sed -i 's/imagePullPolicy: Always/imagePullPolicy: IfNotPresent/g' /etc/kubernetes/manifests/vip.yaml && sudo $(kubeadm token create --print-join-command) --control-plane --certificate-key $newKey -v=9" | xclip -sel clip
# # echo "sudo swapoff -a && sudo $(kubeadm token create --print-join-command) --control-plane --certificate-key $newKey" | xclip -sel clip
echo "..."
echo "Done starting cluster, control-plane join command is copied to clipboard."

exit 0
