#!/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 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 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 </dev/null; then echo "Creating MetalLB L2Advertisement..." cat <