Skip to content

Commit

Permalink
Fix vitobotta#434 (subnet sizes != /16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Voigt committed Sep 4, 2024
1 parent cc73a0a commit 75ce69d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
2 changes: 0 additions & 2 deletions src/kubernetes/installer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
})
Expand Down
30 changes: 21 additions & 9 deletions templates/master_install_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -47,7 +59,7 @@ fi

mkdir -p /etc/rancher/k3s

cat > /etc/rancher/k3s/registries.yaml <<EOF
cat >/etc/rancher/k3s/registries.yaml <<EOF
mirrors:
"*":
EOF
Expand All @@ -71,4 +83,4 @@ curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION="{{ k3s_version }}" K3S_TOKEN
--node-external-ip=$PUBLIC_IP \
{{ server }} {{ tls_sans }}" sh -

echo true > /etc/initialized
echo true >/etc/initialized
33 changes: 23 additions & 10 deletions templates/worker_install_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <<EOF
cat >/etc/rancher/k3s/registries.yaml <<EOF
mirrors:
"*":
EOF
Expand All @@ -44,6 +57,6 @@ curl -sfL https://get.k3s.io | K3S_TOKEN="{{ k3s_token }}" INSTALL_K3S_VERSION="
--node-name=$HOSTNAME {{ extra_args }} \
--node-ip=$PRIVATE_IP \
--node-external-ip=$PUBLIC_IP \
$NETWORK_INTERFACE " sh -
$FLANNEL_SETTINGS " sh -

echo true > /etc/initialized
echo true >/etc/initialized

0 comments on commit 75ce69d

Please sign in to comment.