|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright 2026 The kpt and Nephio Authors |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +# Utility script to dynamically determine the MetalLB IP range from the kind Docker network. |
| 17 | +# This removes the need to hardcode 172.18.255.x addresses in scripts and configs. |
| 18 | +# |
| 19 | +# Usage: |
| 20 | +# source scripts/get-kind-metallb-subnet.sh |
| 21 | +# # Now METALLB_IP_RANGE_START and METALLB_IP_RANGE_END are set |
| 22 | +# |
| 23 | +# Or call individual functions: |
| 24 | +# get_metallb_ip_range -> sets METALLB_IP_RANGE_START and METALLB_IP_RANGE_END |
| 25 | +# get_service_lb_ip <svc> <ns> -> prints the LoadBalancer IP of a service |
| 26 | +# wait_for_service_lb_ip <svc> <ns> [timeout] -> waits for and prints the LB IP |
| 27 | + |
| 28 | +# Only set strict mode when executed directly, not when sourced. |
| 29 | +# This avoids changing the caller's shell options unexpectedly. |
| 30 | +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then |
| 31 | + set -euo pipefail |
| 32 | +fi |
| 33 | + |
| 34 | +# Get the subnet of the kind Docker network and derive a MetalLB-suitable IP range. |
| 35 | +# The range is placed at the top of the subnet's last octet range (x.x.255.200 - x.x.255.250) |
| 36 | +# to avoid conflicts with container IPs assigned by Docker. |
| 37 | +# |
| 38 | +# Sets: METALLB_IP_RANGE_START, METALLB_IP_RANGE_END |
| 39 | +get_metallb_ip_range() { |
| 40 | + local kind_network="${KIND_DOCKER_NETWORK:-kind}" |
| 41 | + |
| 42 | + # Get the IPv4 subnet from the Docker network (e.g. "172.18.0.0/16") |
| 43 | + # Docker networks can have both IPv4 and IPv6 configs, so we iterate over all |
| 44 | + # IPAM configs and pick the one that looks like an IPv4 CIDR. |
| 45 | + local subnet |
| 46 | + subnet="$(docker network inspect "$kind_network" \ |
| 47 | + --format='{{range .IPAM.Config}}{{.Subnet}} {{end}}' \ |
| 48 | + | tr ' ' '\n' \ |
| 49 | + | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/' \ |
| 50 | + | head -1)" \ |
| 51 | + || { echo "ERROR: Could not inspect Docker network '$kind_network'. Is kind running?" >&2; return 1; } |
| 52 | + |
| 53 | + if [[ -z "$subnet" ]]; then |
| 54 | + echo "ERROR: No IPv4 subnet found on Docker network '$kind_network'." >&2 |
| 55 | + return 1 |
| 56 | + fi |
| 57 | + |
| 58 | + # Parse the subnet base address and CIDR mask to compute a valid range. |
| 59 | + # We place the MetalLB pool at the high end of the subnet to avoid conflicts |
| 60 | + # with container IPs assigned by Docker. |
| 61 | + local base_ip cidr_mask |
| 62 | + base_ip="${subnet%%/*}" |
| 63 | + cidr_mask="${subnet##*/}" |
| 64 | + |
| 65 | + # Convert base IP to integer for arithmetic |
| 66 | + local IFS='.' |
| 67 | + # shellcheck disable=SC2086 |
| 68 | + set -- $base_ip |
| 69 | + local ip_int=$(( ($1 << 24) + ($2 << 16) + ($3 << 8) + $4 )) |
| 70 | + IFS=' ' |
| 71 | + |
| 72 | + # Calculate the number of host addresses in the subnet |
| 73 | + local host_bits=$(( 32 - cidr_mask )) |
| 74 | + local subnet_size=$(( 1 << host_bits )) |
| 75 | + |
| 76 | + # Place the pool near the top of the subnet: last 51 addresses before broadcast |
| 77 | + # (broadcast = base + subnet_size - 1, so we use base + subnet_size - 52 to base + subnet_size - 2) |
| 78 | + local start_int=$(( ip_int + subnet_size - 52 )) |
| 79 | + local end_int=$(( ip_int + subnet_size - 2 )) |
| 80 | + |
| 81 | + # Convert integers back to dotted-quad |
| 82 | + METALLB_IP_RANGE_START="$(( (start_int >> 24) & 255 )).$(( (start_int >> 16) & 255 )).$(( (start_int >> 8) & 255 )).$(( start_int & 255 ))" |
| 83 | + METALLB_IP_RANGE_END="$(( (end_int >> 24) & 255 )).$(( (end_int >> 16) & 255 )).$(( (end_int >> 8) & 255 )).$(( end_int & 255 ))" |
| 84 | + |
| 85 | + export METALLB_IP_RANGE_START |
| 86 | + export METALLB_IP_RANGE_END |
| 87 | +} |
| 88 | + |
| 89 | +# Generate a MetalLB IPAddressPool + L2Advertisement YAML config using the dynamic IP range. |
| 90 | +# Prints the YAML to stdout. |
| 91 | +generate_metallb_config() { |
| 92 | + get_metallb_ip_range |
| 93 | + |
| 94 | + cat <<EOF |
| 95 | +apiVersion: metallb.io/v1beta1 |
| 96 | +kind: IPAddressPool |
| 97 | +metadata: |
| 98 | + name: example |
| 99 | + namespace: metallb-system |
| 100 | +spec: |
| 101 | + addresses: |
| 102 | + - ${METALLB_IP_RANGE_START}-${METALLB_IP_RANGE_END} |
| 103 | +--- |
| 104 | +apiVersion: metallb.io/v1beta1 |
| 105 | +kind: L2Advertisement |
| 106 | +metadata: |
| 107 | + name: empty |
| 108 | + namespace: metallb-system |
| 109 | +EOF |
| 110 | +} |
| 111 | + |
| 112 | +# Get the LoadBalancer IP of a Kubernetes service. |
| 113 | +# Args: <service-name> <namespace> |
| 114 | +# Prints the IP to stdout. Returns 1 if not available. |
| 115 | +get_service_lb_ip() { |
| 116 | + local svc_name="$1" |
| 117 | + local namespace="$2" |
| 118 | + |
| 119 | + local ip |
| 120 | + ip="$(kubectl get svc "$svc_name" -n "$namespace" -o jsonpath='{.status.loadBalancer.ingress[0].ip}' 2>/dev/null)" |
| 121 | + |
| 122 | + if [[ -z "$ip" || "$ip" == "null" ]]; then |
| 123 | + return 1 |
| 124 | + fi |
| 125 | + |
| 126 | + echo "$ip" |
| 127 | +} |
| 128 | + |
| 129 | +# Wait for a LoadBalancer service to get an external IP assigned. |
| 130 | +# Args: <service-name> <namespace> [timeout-seconds] |
| 131 | +# Prints the IP to stdout once available. |
| 132 | +wait_for_service_lb_ip() { |
| 133 | + local svc_name="$1" |
| 134 | + local namespace="$2" |
| 135 | + local timeout="${3:-60}" |
| 136 | + |
| 137 | + local elapsed=0 |
| 138 | + local ip="" |
| 139 | + |
| 140 | + while [[ $elapsed -lt $timeout ]]; do |
| 141 | + ip="$(get_service_lb_ip "$svc_name" "$namespace" 2>/dev/null)" && break |
| 142 | + sleep 2 |
| 143 | + elapsed=$((elapsed + 2)) |
| 144 | + done |
| 145 | + |
| 146 | + if [[ -z "$ip" ]]; then |
| 147 | + echo "ERROR: Timed out waiting for LoadBalancer IP on $namespace/$svc_name after ${timeout}s" >&2 |
| 148 | + return 1 |
| 149 | + fi |
| 150 | + |
| 151 | + echo "$ip" |
| 152 | +} |
| 153 | + |
| 154 | +# Get the function-runner LoadBalancer IP. |
| 155 | +# Falls back to waiting if not yet assigned. |
| 156 | +get_function_runner_ip() { |
| 157 | + wait_for_service_lb_ip "function-runner" "porch-system" "${1:-60}" |
| 158 | +} |
| 159 | + |
| 160 | +# Get the Gitea LoadBalancer IP. |
| 161 | +# Falls back to waiting if not yet assigned. |
| 162 | +get_gitea_ip() { |
| 163 | + wait_for_service_lb_ip "gitea-lb" "gitea" "${1:-60}" |
| 164 | +} |
0 commit comments