From 17a27d0570f79cb8caaac0228721bad12a8c6191 Mon Sep 17 00:00:00 2001 From: John Bowdre Date: Tue, 20 Dec 2022 08:26:06 -0600 Subject: [PATCH] update script logic --- terraform/scripts/initialize-controlplane.sh | 44 +++++++++++++------- terraform/scripts/join-workers.sh | 4 +- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/terraform/scripts/initialize-controlplane.sh b/terraform/scripts/initialize-controlplane.sh index 8dbeee8..a3451cb 100644 --- a/terraform/scripts/initialize-controlplane.sh +++ b/terraform/scripts/initialize-controlplane.sh @@ -1,18 +1,17 @@ #!/bin/bash -eu source ./env.txt -if [ ! -f /etc/kubernetes/manifests/kube-vip.yaml ]; then - echo ">> Configuring kube-vip..." - sudo ctr image pull ghcr.io/kube-vip/kube-vip:"${KUBEVIP_VER}" - sudo ctr run --rm --net-host ghcr.io/kube-vip/kube-vip:"${KUBEVIP_VER}" vip /kube-vip manifest pod \ - --interface ens192 \ - --vip "${K8S_CONTROLPLANE_VIP}" \ - --controlplane \ - --arp \ - --leaderElection | sudo tee /etc/kubernetes/manifests/kube-vip.yaml -fi - if [ "${HOSTNAME}" == "${K8S_INITIAL_NODE}" ]; then + if [ ! -f /etc/kubernetes/manifests/kube-vip.yaml ]; then + echo ">> Configuring kube-vip..." + sudo ctr image pull ghcr.io/kube-vip/kube-vip:"${KUBEVIP_VER}" + sudo ctr run --rm --net-host ghcr.io/kube-vip/kube-vip:"${KUBEVIP_VER}" vip /kube-vip manifest pod \ + --interface ens192 \ + --vip "${K8S_CONTROLPLANE_VIP}" \ + --controlplane \ + --arp \ + --leaderElection | sudo tee /etc/kubernetes/manifests/kube-vip.yaml + fi if ! kubectl get nodes 2>/dev/null; then echo ">> Bootstrapping first controlplane node..." cat << EOF > kubeadminit.yaml @@ -57,14 +56,14 @@ EOF echo ">> Creating discovery config..." kubectl -n kube-public get configmap cluster-info -o jsonpath='{.data.kubeconfig}' > discovery.yaml - sudo install -o root -g root -m 600 discovery.yaml /etc/kubernetes/discovery.yaml + sudo install -m 600 discovery.yaml /etc/kubernetes/discovery.yaml else echo ">> [ERROR] Cluster initialization unsuccessful on ${HOSTNAME}! <<" exit 1 fi fi echo ">> Waiting up to 10 minutes for all control-plane nodes to be Ready..." - python3 -m http.server & + python3 -m http.server 2>/dev/null & PROC_ID=$! attempts_max=60 attempt=0 @@ -388,8 +387,11 @@ else attempt=0 until [ -f /etc/kubernetes/discovery.yaml ]; do wget "http://${K8S_CONTROLPLANE_VIP}:8000/discovery.yaml" 2>/dev/null - sudo install -o root -g root -m 600 discovery.yaml /etc/kubernetes/discovery.yaml 2>/dev/null - if [ ! -f /etc/kubernetes/discovery.yaml ]; then + if ! sudo install -m 600 discovery.yaml /etc/kubernetes/discovery.yaml 2>/dev/null; then + if [ ${attempt} -eq ${attempts_max} ]; then + echo ">> [ERROR] Timeout waiting for discovery.yaml! <<" + exit 1 + fi attempt=$((attempt+1)) sleep 10 fi @@ -409,7 +411,17 @@ nodeRegistration: controlPlane: certificateKey: ${KUBEADM_CERTKEY} EOF - if sudo kubeadm join "${K8S_CONTROLPLANE_VIP}":6443 --config kubeadmjoin.yaml; then + if sudo kubeadm join "${K8S_CONTROLPLANE_VIP}:6443" --config kubeadmjoin.yaml; then + if [ ! -f /etc/kubernetes/manifests/kube-vip.yaml ]; then + echo ">> Configuring kube-vip..." + sudo ctr image pull ghcr.io/kube-vip/kube-vip:"${KUBEVIP_VER}" + sudo ctr run --rm --net-host ghcr.io/kube-vip/kube-vip:"${KUBEVIP_VER}" vip /kube-vip manifest pod \ + --interface ens192 \ + --vip "${K8S_CONTROLPLANE_VIP}" \ + --controlplane \ + --arp \ + --leaderElection | sudo tee /etc/kubernetes/manifests/kube-vip.yaml + fi echo ">> Node ${HOSTNAME} successfully initialized!" touch .k8s-node-success mkdir -p "${HOME}"/.kube diff --git a/terraform/scripts/join-workers.sh b/terraform/scripts/join-workers.sh index 8e10b7e..c776f86 100644 --- a/terraform/scripts/join-workers.sh +++ b/terraform/scripts/join-workers.sh @@ -17,7 +17,7 @@ echo ">> Continuing after $((attempt*10)) seconds." echo ">> Waiting up to 10 minutes for all control-plane nodes..." attempts_max=60 attempt=0 -until "$(wget http://${K8S_CONTROLPLANE_VIP}:8000/.k8s-controlplane-success)" 2>/dev/null; do +until wget "http://${K8S_CONTROLPLANE_VIP}:8000/.k8s-controlplane-success" 2>/dev/null; do if [ ${attempt} -eq ${attempts_max} ]; then echo ">> [ERROR] Timeout waiting for control-plane nodes! <<" exit 1 @@ -31,7 +31,7 @@ attempts_max=6 attempt=0 until [ -f /etc/kubernetes/discovery.yaml ]; do wget "http://${K8S_CONTROLPLANE_VIP}:8000/discovery.yaml" 2>/dev/null - sudo install -o root -g root -m 600 discovery.yaml /etc/kubernetes/discovery.yaml 2>/dev/null + sudo install -m 600 discovery.yaml /etc/kubernetes/discovery.yaml 2>/dev/null if [ ! -f /etc/kubernetes/discovery.yaml ]; then attempt=$((attempt+1)) sleep 10