From 75ce69d32d06ad62a9f17c43eddc01ffe5d4efef Mon Sep 17 00:00:00 2001 From: Valentin Voigt Date: Wed, 4 Sep 2024 11:45:42 +0200 Subject: [PATCH] Fix #434 (subnet sizes != /16) --- src/kubernetes/installer.cr | 2 -- templates/master_install_script.sh | 30 +++++++++++++++++++-------- templates/worker_install_script.sh | 33 +++++++++++++++++++++--------- 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/kubernetes/installer.cr b/src/kubernetes/installer.cr index 53241ad..7bfbd55 100644 --- a/src/kubernetes/installer.cr +++ b/src/kubernetes/installer.cr @@ -176,7 +176,6 @@ class Kubernetes::Installer server: server, tls_sans: generate_tls_sans(master_count), private_network_enabled: settings.networking.private_network.enabled.to_s, - private_network_test_ip: settings.networking.private_network.subnet.split(".")[0..2].join(".") + ".0", private_network_subnet: settings.networking.private_network.enabled ? settings.networking.private_network.subnet : "", cluster_cidr: settings.networking.cluster_cidr, service_cidr: settings.networking.service_cidr, @@ -194,7 +193,6 @@ class Kubernetes::Installer k3s_version: settings.k3s_version, api_server_ip_address: api_server_ip_address, private_network_enabled: settings.networking.private_network.enabled.to_s, - private_network_test_ip: settings.networking.private_network.subnet.split(".")[0..2].join(".") + ".0", private_network_subnet: settings.networking.private_network.enabled ? settings.networking.private_network.subnet : "", extra_args: kubelet_args_list }) diff --git a/templates/master_install_script.sh b/templates/master_install_script.sh index 76a592d..d96d109 100644 --- a/templates/master_install_script.sh +++ b/templates/master_install_script.sh @@ -4,15 +4,23 @@ HOSTNAME=$(hostname -f) PUBLIC_IP=$(hostname -I | awk '{print $1}') if [ "{{ private_network_enabled }}" = "true" ]; then - echo "Using private network " > /var/log/hetzner-k3s.log + echo "Using private network " >/var/log/hetzner-k3s.log SUBNET="{{ private_network_subnet }}" - SUBNET_PREFIX=$(echo $SUBNET | cut -d'/' -f1 | sed 's/\./\\./g' | sed 's/0$//') MAX_ATTEMPTS=30 DELAY=10 UP="false" for i in $(seq 1 $MAX_ATTEMPTS); do - if ip -4 addr show | grep -q "inet $SUBNET_PREFIX"; then + NETWORK_INTERFACE=$( + ip -o link show | + grep -w 'mtu 1450' | + awk -F': ' '{print $2}' | + grep -Ev 'cilium|br|flannel|docker|veth' | + xargs -I {} bash -c 'ethtool {} &>/dev/null && echo {}' | + head -n1 + ) + + if [ ! -z "$NETWORK_INTERFACE" ]; then echo "Private network IP in subnet $SUBNET is up" 2>&1 | tee -a /var/log/hetzner-k3s.log UP="true" break @@ -25,16 +33,20 @@ if [ "{{ private_network_enabled }}" = "true" ]; then echo "Timeout waiting for private network IP in subnet $SUBNET" 2>&1 | tee -a /var/log/hetzner-k3s.log fi - PRIVATE_IP=$(ip route get {{ private_network_test_ip }} | awk -F"src " 'NR==1{split($2,a," ");print a[1]}') - NETWORK_INTERFACE=" --flannel-iface=$(ip route get {{ private_network_test_ip }} | awk -F"dev " 'NR==1{split($2,a," ");print a[1]}') " + PRIVATE_IP=$( + ip -4 -o addr show dev "$NETWORK_INTERFACE" | + awk '{print $4}' | + cut -d'/' -f1 | + head -n1 + ) else - echo "Using public network " > /var/log/hetzner-k3s.log + echo "Using public network " >/var/log/hetzner-k3s.log PRIVATE_IP="${PUBLIC_IP}" NETWORK_INTERFACE=" " fi if [ "{{ cni }}" = "true" ] && [ "{{ cni_mode }}" = "flannel" ]; then - FLANNEL_SETTINGS=" {{ flannel_backend }} $NETWORK_INTERFACE " + FLANNEL_SETTINGS=" {{ flannel_backend }} --flannel-iface=$NETWORK_INTERFACE " else FLANNEL_SETTINGS=" {{ flannel_backend }} " fi @@ -47,7 +59,7 @@ fi mkdir -p /etc/rancher/k3s -cat > /etc/rancher/k3s/registries.yaml </etc/rancher/k3s/registries.yaml < /etc/initialized +echo true >/etc/initialized diff --git a/templates/worker_install_script.sh b/templates/worker_install_script.sh index ebd4666..7cf8568 100644 --- a/templates/worker_install_script.sh +++ b/templates/worker_install_script.sh @@ -4,15 +4,23 @@ HOSTNAME=$(hostname -f) PUBLIC_IP=$(hostname -I | awk '{print $1}') if [ "{{ private_network_enabled }}" = "true" ]; then - echo "Using private network " > /var/log/hetzner-k3s.log + echo "Using private network " >/var/log/hetzner-k3s.log SUBNET="{{ private_network_subnet }}" - SUBNET_PREFIX=$(echo $SUBNET | cut -d'/' -f1 | sed 's/\./\\./g' | sed 's/0$//') MAX_ATTEMPTS=30 DELAY=10 UP="false" for i in $(seq 1 $MAX_ATTEMPTS); do - if ip -4 addr show | grep -q "inet $SUBNET_PREFIX"; then + NETWORK_INTERFACE=$( + ip -o link show | + grep -w 'mtu 1450' | + awk -F': ' '{print $2}' | + grep -Ev 'cilium|br|flannel|docker|veth' | + xargs -I {} bash -c 'ethtool {} &>/dev/null && echo {}' | + head -n1 + ) + + if [ ! -z "$NETWORK_INTERFACE" ]; then echo "Private network IP in subnet $SUBNET is up" 2>&1 | tee -a /var/log/hetzner-k3s.log UP="true" break @@ -25,17 +33,22 @@ if [ "{{ private_network_enabled }}" = "true" ]; then echo "Timeout waiting for private network IP in subnet $SUBNET" 2>&1 | tee -a /var/log/hetzner-k3s.log fi - PRIVATE_IP=$(ip route get {{ private_network_test_ip }} | awk -F"src " 'NR==1{split($2,a," ");print a[1]}') - NETWORK_INTERFACE=" --flannel-iface=$(ip route get {{ private_network_test_ip }} | awk -F"dev " 'NR==1{split($2,a," ");print a[1]}') " + PRIVATE_IP=$( + ip -4 -o addr show dev "$NETWORK_INTERFACE" | + awk '{print $4}' | + cut -d'/' -f1 | + head -n1 + ) + FLANNEL_SETTINGS=" --flannel-iface=$NETWORK_INTERFACE " else - echo "Using public network " > /var/log/hetzner-k3s.log + echo "Using public network " >/var/log/hetzner-k3s.log PRIVATE_IP="${PUBLIC_IP}" - NETWORK_INTERFACE=" " + FLANNEL_SETTINGS=" " fi mkdir -p /etc/rancher/k3s -cat > /etc/rancher/k3s/registries.yaml </etc/rancher/k3s/registries.yaml < /etc/initialized +echo true >/etc/initialized