Skip to content

Instantly share code, notes, and snippets.

@DrPsychick
Last active December 29, 2020 12:00
Show Gist options
  • Save DrPsychick/d99d938a3bedcd6d0aa2e71febeeff96 to your computer and use it in GitHub Desktop.
Save DrPsychick/d99d938a3bedcd6d0aa2e71febeeff96 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
# update IP addresses in configs in k8s and on kind master node
# * this is ugly, but the only way to avoid recreating the cluster after restart
export PATH=$PATH:/usr/local/bin
if [ -z "$1" ]; then
echo "Usage: $0 <kind-cluster-name>"
exit 1
fi
kube_context=kind-$1
docker_prefix=$1
update_cert=./update_cert.sh
# check connectivity, fail silently if not reachable
kubectl --context=$kube_context cluster-info --request-timeout=3 > /dev/null 2>&1
# 1. get configured 'advertiseAddress'
config_ip=$(kubectl -n kube-system --context=$kube_context get configmap kubeadm-config -o json \
| jq '.data.ClusterStatus' | sed -e 's/.*advertiseAddress: \([0-9.]\+\)\\n.*/\1/')
#data:
# ClusterStatus: |
# apiEndpoints:
# rootk8s-stage-control-plane:
# advertiseAddress: 172.19.0.8
# bindPort: 6443
# 2. get current master node IP
master_ip=$(kubectl -n kube-system --context=$kube_context get nodes -l node-role.kubernetes.io/master -o json \
| jq '.items[0].status.addresses[]|select(.type == "InternalIP")|.address' | tr -d \")
# IPs differ -> we need to update
if [ "$config_ip" != "$master_ip" ]; then
echo "Configured IP '$config_ip' does not match master IP '$master_ip' : updating config"
# 1. configmap
cluster_status=$(kubectl -n kube-system --context=$kube_context get configmap kubeadm-config -o json \
| jq '.data.ClusterStatus' | sed -e "s/advertiseAddress: [0-9.]\+\\\\n/advertiseAddress: $master_ip\\\\n/")
patch_data='{ "data": { "ClusterStatus": '$cluster_status' }}'
kubectl -n kube-system --context=$kube_context patch configmap kubeadm-config --type merge --patch "$patch_data"
# 2. config master node
docker exec -t ${docker_prefix}-control-plane sed -i -e "s/$config_ip:6443/$master_ip:6443/" /etc/kubernetes/scheduler.conf
docker exec -t ${docker_prefix}-control-plane sed -i -e "s/$config_ip:6443/$master_ip:6443/" /etc/kubernetes/controller-manager.conf
# 3. update API certificate IPs
$update_cert $docker_prefix $config_ip $master_ip
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment