2025-08-23 15:05:26 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
source /vagrant/.envrc
|
|
|
|
|
|
|
|
|
|
# Check if MetalLB is already installed by looking for the controller deployment
|
|
|
|
|
if ! kubectl get deployment -n metallb-system controller &>/dev/null; then
|
|
|
|
|
echo "Installing MetalLB..."
|
|
|
|
|
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/main/config/manifests/metallb-native.yaml
|
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
|
echo "Fatal: Failed to apply MetalLB manifest." >&2
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
2025-10-10 13:33:11 +01:00
|
|
|
echo "Waiting for MetalLB pods to be in 'Running' state..."
|
|
|
|
|
MAX_RETRIES=10
|
|
|
|
|
RETRY=0
|
|
|
|
|
|
|
|
|
|
while [ $RETRY -lt $MAX_RETRIES ]; do
|
|
|
|
|
NOT_READY_PODS=$(kubectl -n metallb-system get pods --no-headers | grep -v 'Running' | wc -l)
|
|
|
|
|
if [ "$NOT_READY_PODS" -eq 0 ]; then
|
|
|
|
|
echo "All MetalLB pods are running."
|
|
|
|
|
break
|
|
|
|
|
else
|
|
|
|
|
echo "$NOT_READY_PODS MetalLB pods are not ready yet. Waiting..."
|
|
|
|
|
RETRY=$((RETRY + 1))
|
|
|
|
|
sleep 5
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
if [ "$NOT_READY_PODS" -ne 0 ]; then
|
|
|
|
|
echo "Failed to get all MetalLB pods running after $MAX_RETRIES attempts."
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2025-08-23 15:05:26 +01:00
|
|
|
|
|
|
|
|
else
|
|
|
|
|
echo "MetalLB is already installed."
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Check if the IPAddressPool already exists
|
|
|
|
|
if ! kubectl get ipaddresspool -n metallb-system default &>/dev/null; then
|
|
|
|
|
echo "Creating MetalLB IPAddressPool..."
|
|
|
|
|
cat <<EOF | kubectl apply -f -
|
|
|
|
|
apiVersion: metallb.io/v1beta1
|
|
|
|
|
kind: IPAddressPool
|
|
|
|
|
metadata:
|
|
|
|
|
name: default
|
|
|
|
|
namespace: metallb-system
|
|
|
|
|
spec:
|
|
|
|
|
addresses:
|
|
|
|
|
- ${METALLB_IP_RANGE}
|
|
|
|
|
EOF
|
|
|
|
|
else
|
|
|
|
|
echo "MetalLB IPAddressPool already exists."
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Check if the L2Advertisement already exists
|
|
|
|
|
if ! kubectl get l2advertisement -n metallb-system default &>/dev/null; then
|
|
|
|
|
echo "Creating MetalLB L2Advertisement..."
|
|
|
|
|
cat <<EOF | kubectl apply -f -
|
|
|
|
|
apiVersion: metallb.io/v1beta1
|
|
|
|
|
kind: L2Advertisement
|
|
|
|
|
metadata:
|
|
|
|
|
name: default
|
|
|
|
|
namespace: metallb-system
|
|
|
|
|
spec:
|
|
|
|
|
ipAddressPools:
|
|
|
|
|
- default
|
|
|
|
|
EOF
|
|
|
|
|
else
|
|
|
|
|
echo "MetalLB L2Advertisement already exists."
|
|
|
|
|
fi
|