Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multi-node cluster not working after restarting docker #2775

Merged
merged 5 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 56 additions & 8 deletions images/base/files/usr/local/bin/entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ grep_allow_nomatch() {
grep "$@" || [[ $? == 1 ]]
}

# regex_escape_ip converts IP address string $1 to a regex-escaped literal
regex_escape_ip(){
sed -e 's#\.#\\.#g' -e 's#\[#\\[#g' -e 's#\]#\\]#g' <<<"$1"
BenTheElder marked this conversation as resolved.
Show resolved Hide resolved
}

validate_userns() {
if [[ -z "${userns}" ]]; then
return
Expand Down Expand Up @@ -378,6 +383,22 @@ select_iptables() {
update-alternatives --set ip6tables "/usr/sbin/ip6tables-${mode}" > /dev/null
}

fix_certificate() {
BenTheElder marked this conversation as resolved.
Show resolved Hide resolved
local apiserver_crt_file="/etc/kubernetes/pki/apiserver.crt"
local apiserver_key_file="/etc/kubernetes/pki/apiserver.key"

# Skip if this Node doesn't run kube-apiserver
if [[ ! -f ${apiserver_crt_file} ]] || [[ ! -f ${apiserver_key_file} ]]; then
return
fi

# Deletes the certificate for kube-apiserver and generates a new one.
# This is necessary because the old one doesn't match the current IP.
echo 'INFO: clearing and regenerating the certificate for serving the Kubernetes API' >&2
rm -f ${apiserver_crt_file} ${apiserver_key_file}
kubeadm init phase certs apiserver --config /kind/kubeadm.conf
BenTheElder marked this conversation as resolved.
Show resolved Hide resolved
}

enable_network_magic(){
# well-known docker embedded DNS is at 127.0.0.11:53
local docker_embedded_dns_ip='127.0.0.11'
Expand Down Expand Up @@ -405,6 +426,17 @@ enable_network_magic(){
cp /etc/resolv.conf /etc/resolv.conf.original
sed -e "s/${docker_embedded_dns_ip}/${docker_host_ip}/g" /etc/resolv.conf.original >/etc/resolv.conf

local files_to_update=(
/etc/kubernetes/manifests/etcd.yaml
/etc/kubernetes/manifests/kube-apiserver.yaml
/etc/kubernetes/manifests/kube-controller-manager.yaml
/etc/kubernetes/manifests/kube-scheduler.yaml
/etc/kubernetes/controller-manager.conf
/etc/kubernetes/scheduler.conf
/kind/kubeadm.conf
/var/lib/kubelet/kubeadm-flags.env
)
local should_fix_certificate=false
# fixup IPs in manifests ...
curr_ipv4="$( (head -n1 <(getent ahostsv4 "$(hostname)") | cut -d' ' -f1) || true)"
echo "INFO: Detected IPv4 address: ${curr_ipv4}" >&2
Expand All @@ -416,10 +448,16 @@ enable_network_magic(){
echo "ERROR: Have an old IPv4 address but no current IPv4 address (!)" >&2
exit 1
fi
# kubernetes manifests are only present on control-plane nodes
sed -i "s#${old_ipv4}#${curr_ipv4}#" /etc/kubernetes/manifests/*.yaml || true
# this is no longer required with autodiscovery
sed -i "s#${old_ipv4}#${curr_ipv4}#" /var/lib/kubelet/kubeadm-flags.env || true
if [[ "${old_ipv4}" != "${curr_ipv4}" ]]; then
should_fix_certificate=true
sed_ipv4_command="s#\b$(regex_escape_ip "${old_ipv4}")\b#${curr_ipv4}#g"
for f in "${files_to_update[@]}"; do
# kubernetes manifests are only present on control-plane nodes
if [[ -f "$f" ]]; then
sed -i "${sed_ipv4_command}" "$f"
fi
done
fi
fi
if [[ -n $curr_ipv4 ]]; then
echo -n "${curr_ipv4}" >/kind/old-ipv4
Expand All @@ -435,14 +473,24 @@ enable_network_magic(){
if [[ -z $curr_ipv6 ]]; then
echo "ERROR: Have an old IPv6 address but no current IPv6 address (!)" >&2
fi
# kubernetes manifests are only present on control-plane nodes
sed -i "s#${old_ipv6}#${curr_ipv6}#" /etc/kubernetes/manifests/*.yaml || true
# this is no longer required with autodiscovery
sed -i "s#${old_ipv6}#${curr_ipv6}#" /var/lib/kubelet/kubeadm-flags.env || true
if [[ "${old_ipv6}" != "${curr_ipv6}" ]]; then
should_fix_certificate=true
sed_ipv6_command="s#\b$(regex_escape_ip "${old_ipv6}")\b#${curr_ipv6}#g"
for f in "${files_to_update[@]}"; do
# kubernetes manifests are only present on control-plane nodes
if [[ -f "$f" ]]; then
sed -i "${sed_ipv6_command}" "$f"
fi
done
fi
fi
if [[ -n $curr_ipv6 ]]; then
echo -n "${curr_ipv6}" >/kind/old-ipv6
fi

if $should_fix_certificate; then
fix_certificate
fi
}

# validate state
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/config/defaults/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ limitations under the License.
package defaults

// Image is the default for the Config.Image field, aka the default node image.
const Image = "kindest/node:v1.24.0@sha256:0866296e693efe1fed79d5e6c7af8df71fc73ae45e3679af05342239cdc5bc8e"
const Image = "kindest/node:v1.24.0@sha256:4bec67ade4adfd316ff95545a015d3071b3607c73ec167f21cba77c00a6e38c5"
2 changes: 1 addition & 1 deletion pkg/build/nodeimage/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ package nodeimage
const DefaultImage = "kindest/node:latest"

// DefaultBaseImage is the default base image used
const DefaultBaseImage = "docker.io/kindest/base:v20220518-0ffcf8d6"
const DefaultBaseImage = "docker.io/kindest/base:v20220525-316e1160"