|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +set -eu |
| 7 | + |
| 8 | +# Setup Kind cluster for e2e tests if it does not exist |
| 9 | +KIND_CLUSTER="clos-lab-kind" |
| 10 | +echo "Setting up Kind cluster for tests..." |
| 11 | +if ! command -v kind &> /dev/null; then |
| 12 | + echo "Kind is not installed. Please install Kind manually." |
| 13 | + exit 1 |
| 14 | +fi |
| 15 | + |
| 16 | +if kind get clusters 2>/dev/null | grep -q "^${KIND_CLUSTER}$"; then |
| 17 | + echo "Kind cluster '${KIND_CLUSTER}' already exists. Skipping creation." |
| 18 | +else |
| 19 | + echo "Creating Kind cluster '${KIND_CLUSTER}'..." |
| 20 | + kind create cluster --name "${KIND_CLUSTER}" |
| 21 | +fi |
| 22 | + |
| 23 | +# Go to git repo root |
| 24 | +pushd "$(git rev-parse --show-toplevel)" || exit 1 |
| 25 | + |
| 26 | +echo "Installing CRDs..." |
| 27 | +make install |
| 28 | +# Return to original directory |
| 29 | +popd || exit 1 |
| 30 | + |
| 31 | +HELM="docker run --network host -ti --rm -v $(pwd):/apps -w /apps \ |
| 32 | + -v $HOME/.kube:/root/.kube -v $HOME/.helm:/root/.helm \ |
| 33 | + -v $HOME/.config/helm:/root/.config/helm \ |
| 34 | + -v $HOME/.cache/helm:/root/.cache/helm \ |
| 35 | + alpine/helm:3.12.3" |
| 36 | + |
| 37 | +CLABVERTER="sudo docker run --user $(id -u) -v $(pwd):/clabernetes/work --rm ghcr.io/srl-labs/clabernetes/clabverter" |
| 38 | + |
| 39 | +$HELM upgrade --install --create-namespace --namespace c9s \ |
| 40 | + clabernetes oci://ghcr.io/srl-labs/clabernetes/clabernetes |
| 41 | + |
| 42 | +kubectl apply -f https://kube-vip.io/manifests/rbac.yaml |
| 43 | +kubectl apply -f https://raw.githubusercontent.com/kube-vip/kube-vip-cloud-provider/main/manifest/kube-vip-cloud-controller.yaml |
| 44 | +kubectl create configmap --namespace kube-system kubevip \ |
| 45 | + --from-literal range-global=172.18.1.10-172.18.1.250 || true |
| 46 | + |
| 47 | +#set up the kube-vip CLI |
| 48 | +KVVERSION=$(curl -sL https://api.github.com/repos/kube-vip/kube-vip/releases | \ |
| 49 | + jq -r ".[0].name") |
| 50 | +KUBEVIP="docker run --network host \ |
| 51 | + --rm ghcr.io/kube-vip/kube-vip:$KVVERSION" |
| 52 | +#install kube-vip load balancer daemonset in ARP mode |
| 53 | +$KUBEVIP manifest daemonset --services --inCluster --arp --interface eth0 | \ |
| 54 | +kubectl apply -f - |
| 55 | + |
| 56 | + |
| 57 | +echo "Checking for configuration changes..." |
| 58 | +CONFIG=$($CLABVERTER --stdout --naming non-prefixed) |
| 59 | + |
| 60 | +if echo "$CONFIG" | kubectl diff -f - > /dev/null 2>&1; then |
| 61 | + echo "No changes detected, skipping apply and wait" |
| 62 | +else |
| 63 | + echo "Changes detected, applying configuration..." |
| 64 | + echo "$CONFIG" | kubectl apply -f - |
| 65 | + |
| 66 | + # Wait for services to be ready |
| 67 | + echo "Waiting for services to be ready..." |
| 68 | + kubectl wait --namespace c9s --for=condition=ready --timeout=300s pods --all |
| 69 | + kubectl wait --namespace c9s-clos --for=condition=ready --timeout=300s pods --all |
| 70 | + |
| 71 | + |
| 72 | + # Run script on each sonic node |
| 73 | + echo "Provisioning SONiC nodes..." |
| 74 | + for service in $(kubectl get -n c9s-clos svc -o jsonpath='{.items[*].metadata.name}' 2>/dev/null | tr ' ' '\n' | grep '^sonic-' | grep -v '\-vx$'); do |
| 75 | + until IP=$(kubectl get svc "$service" -n c9s-clos -o jsonpath='{.status.loadBalancer.ingress[0].ip}') && [ -n "$IP" ]; do |
| 76 | + echo "Waiting for external IP..." |
| 77 | + sleep 1 |
| 78 | + done |
| 79 | + |
| 80 | + h=$(kubectl get -n c9s-clos svc "$service" -o jsonpath='{.status.loadBalancer.ingress[0].ip}' 2>/dev/null) |
| 81 | + if [ ! -z "$h" ]; then |
| 82 | + echo "Running init_setup.sh on $h" |
| 83 | + max_attempts=36 # 36 attempts with 10 seconds sleep = 6 minutes total wait time |
| 84 | + attempt=1 |
| 85 | + while [ $attempt -le $max_attempts ]; do |
| 86 | + if sshpass -p 'admin' ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null admin@"$h" 'bash -s' < init_setup.sh; then |
| 87 | + echo "Successfully provisioned $h" |
| 88 | + break |
| 89 | + else |
| 90 | + if [ $attempt -lt $max_attempts ]; then |
| 91 | + echo "Provisioning attempt $attempt of $max_attempts failed for $h. Retrying in 10 seconds..." |
| 92 | + sleep 10 |
| 93 | + else |
| 94 | + echo "Failed to provision $h after $max_attempts attempts" |
| 95 | + fi |
| 96 | + fi |
| 97 | + ((attempt++)) |
| 98 | + done |
| 99 | + fi |
| 100 | + done |
| 101 | + |
| 102 | +fi |
| 103 | + |
| 104 | + |
| 105 | +echo "" |
| 106 | +echo "==========================================" |
| 107 | +echo "SONiC Lab Topology - External IPs" |
| 108 | +echo "==========================================" |
| 109 | +for service in $(kubectl get -n c9s-clos svc -o jsonpath='{.items[*].metadata.name}' 2>/dev/null | tr ' ' '\n'| grep -v '\-vx$'); do |
| 110 | + ip=$(kubectl get -n c9s-clos svc "$service" -o jsonpath='{.status.loadBalancer.ingress[0].ip}' 2>/dev/null) |
| 111 | + if [ -n "$ip" ]; then |
| 112 | + echo "$service -> $ip" |
| 113 | + |
| 114 | + if [[ "$service" == *sonic* ]]; then |
| 115 | + cat <<EOF | kubectl apply -f - |
| 116 | + apiVersion: sonic.networking.metal.ironcore.dev/v1alpha1 |
| 117 | + kind: Switch |
| 118 | + metadata: |
| 119 | + labels: |
| 120 | + app.kubernetes.io/name: sonic-operator |
| 121 | + app.kubernetes.io/managed-by: kustomize |
| 122 | + name: $service |
| 123 | + namespace: c9s-clos |
| 124 | + spec: |
| 125 | + management: |
| 126 | + host: $ip |
| 127 | + port: "57400" |
| 128 | + credentials: |
| 129 | + name: switchcredentials-sample |
| 130 | + macAddress: "aa:bb:cc:dd:ee:ff" |
| 131 | +EOF |
| 132 | + fi |
| 133 | + fi |
| 134 | + |
| 135 | +done |
| 136 | + |
| 137 | +echo "" |
| 138 | +echo "Script ended successfully" |
0 commit comments