Skip to content

Commit 4597458

Browse files
author
sivakami
committed
Delegate cx subnets with delegator app.
1 parent bb837e4 commit 4597458

File tree

2 files changed

+71
-32
lines changed

2 files changed

+71
-32
lines changed

.pipelines/swiftv2-long-running/scripts/create_vnets.sh

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ az account set --subscription "$SUB_ID"
2323
# Verification functions
2424
# -------------------------------
2525
verify_vnet() {
26-
local rg="$1"; local vnet="$2"
26+
local vnet="$1"
2727
echo "==> Verifying VNet: $vnet"
28-
if az network vnet show -g "$rg" -n "$vnet" &>/dev/null; then
28+
if az network vnet show -g "$RG" -n "$vnet" &>/dev/null; then
2929
echo "[OK] Verified VNet $vnet exists."
3030
else
3131
echo "[ERROR] VNet $vnet not found!" >&2
@@ -34,16 +34,77 @@ verify_vnet() {
3434
}
3535

3636
verify_subnet() {
37-
local rg="$1"; local vnet="$2"; local subnet="$3"
37+
local vnet="$1"; local subnet="$2"
3838
echo "==> Verifying subnet: $subnet in $vnet"
39-
if az network vnet subnet show -g "$rg" --vnet-name "$vnet" -n "$subnet" &>/dev/null; then
39+
if az network vnet subnet show -g "$RG" --vnet-name "$vnet" -n "$subnet" &>/dev/null; then
4040
echo "[OK] Verified subnet $subnet exists in $vnet."
4141
else
4242
echo "[ERROR] Subnet $subnet not found in $vnet!" >&2
4343
exit 1
4444
fi
4545
}
4646

47+
# -------------------------------
48+
create_vnet_subets() {
49+
local vnet="$2";
50+
local vnet_cidr="$3"
51+
local node_subnet_cidr="$4";
52+
local extra_subnets="$5";
53+
local extra_cidrs="$6"
54+
55+
echo "==> Creating VNet: $vnet with CIDR: $vnet_cidr"
56+
az network vnet create -g "$RG" -l "$LOCATION" --name "$vnet" --address-prefixes "$vnet_cidr" -o none
57+
58+
IFS=' ' read -r -a extra_subnet_array <<< "$extra_subnets"
59+
IFS=',' read -r -a extra_cidr_array <<< "$extra_cidrs"
60+
61+
for i in "${!extra_subnet_array[@]}"; do
62+
subnet_name="${extra_subnet_array[$i]}"
63+
subnet_cidr="${extra_cidr_array[$i]}"
64+
echo "==> Creating extra subnet: $subnet_name with CIDR: $subnet_cidr"
65+
az network vnet subnet create -g "$RG" \
66+
--vnet-name "$vnet" --name "$subnet_name" \
67+
--delegations Microsoft.SubnetDelegator/msfttestclients \
68+
--address-prefixes "$subnet_cidr" -o none
69+
done
70+
}
71+
72+
delegate_subnet() {
73+
local vnet="$1"
74+
local subnet="$2"
75+
local max_attempts=3
76+
local attempt=1
77+
78+
echo "==> Delegating subnet: $subnet in VNet: $vnet to Subnet Delegator"
79+
subnet_id=$(az network vnet subnet show -g "$RG" --vnet-name "$vnet" -n "$subnet" --query id -o tsv)
80+
modified_custsubnet="${subnet_id//\//%2F}"
81+
cmd_subnetdelegator_curl="'curl -X PUT \"http://localhost:8080/DelegatedSubnet/$modified_custsubnet\"'"
82+
cmd_subnetdelegator_az="az containerapp exec -n subnetdelegator-westus-u3h4j -g subnetdelegator-westus --subscription 9b8218f9-902a-4d20-a65c-e98acec5362f --command $cmd_subnetdelegator_curl"
83+
84+
while [ $attempt -le $max_attempts ]; do
85+
echo "Attempt $attempt of $max_attempts..."
86+
response=$(eval "$cmd_subnetdelegator_az")
87+
88+
if [[ $response == *"success"* ]]; then
89+
echo "Subnet Delegator successfully registered the subnet"
90+
return 0
91+
else
92+
echo "Subnet Delegator failed to register the subnet (attempt $attempt)"
93+
echo "Response: $response"
94+
95+
if [ $attempt -lt $max_attempts ]; then
96+
echo "Retrying in 5 seconds..."
97+
sleep 5
98+
fi
99+
fi
100+
101+
((attempt++))
102+
done
103+
104+
echo "[ERROR] Failed to delegate subnet after $max_attempts attempts"
105+
exit 1
106+
}
107+
47108
# --- Loop over VNets ---
48109
for i in "${!VNAMES[@]}"; do
49110
VNET=${VNAMES[$i]}
@@ -53,26 +114,14 @@ for i in "${!VNAMES[@]}"; do
53114
EXTRA_SUBNET_CIDRS=${EXTRA_CIDRS_LIST[$i]}
54115

55116
# Create VNet + subnets
56-
make -C ./hack/aks swift-delegated-subnet-up \
57-
AZCLI=az REGION=$LOCATION GROUP=$RG VNET=$VNET \
58-
VNET_CIDR=$VNET_CIDR NODE_SUBNET_CIDR=$NODE_SUBNET_CIDR \
59-
EXTRA_SUBNETS="$EXTRA_SUBNETS" EXTRA_SUBNET_CIDRS="$EXTRA_SUBNET_CIDRS" \
60-
&& echo "Created $VNET with subnets $EXTRA_SUBNETS"
61-
62-
verify_vnet "$RG" "$VNET" # Verify VNet
63-
64-
# Loop over extra subnets to verify and create dummy clusters to delegate the pod subnets.
117+
create_vnet_subets "$VNET" "$VNET_CIDR" "$NODE_SUBNET_CIDR" "$EXTRA_SUBNETS" "$EXTRA_SUBNET_CIDRS"
118+
verify_vnet "$VNET"
119+
# Loop over extra subnets to verify and delegate the pod subnets.
65120
for PODSUBNET in $EXTRA_SUBNETS; do
66-
verify_subnet "$RG" "$VNET" "$PODSUBNET"
67-
if [[ "$PODSUBNET" == "pe" ]]; then
68-
# Skip creating dummy cluster for private endpoint subnet.
69-
continue
121+
verify_subnet "$VNET" "$PODSUBNET"
122+
if [[ "$PODSUBNET" != "pe" ]]; then
123+
delegate_subnet "$VNET" "$PODSUBNET"
70124
fi
71-
cluster_name="${BUILD_ID}-${VNET}-${PODSUBNET}"
72-
make -C ./hack/aks swiftv2-dummy-cluster-subnet-delegator-up \
73-
AZCLI=az CLUSTER=$cluster_name GROUP=$RG REGION=$LOCATION \
74-
SUB=$SUB_ID VNET=$VNET POD_SUBNET=$PODSUBNET \
75-
&& echo "Created dummy cluster for $VNET subnet $PODSUBNET"
76125
done
77126
done
78127

hack/aks/Makefile

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,6 @@ rg-up: ## Create resource group
123123
rg-down: ## Delete resource group
124124
$(AZCLI) group delete -g $(GROUP) --yes
125125

126-
swift-delegated-subnet-up: ## Create VNet, node subnet, and optional extra subnets
127-
$(AZCLI) network vnet create -g $(GROUP) -l $(REGION) --name $(VNET) --address-prefixes $(VNET_CIDR) -o none
128-
$(AZCLI) network vnet subnet create -g $(GROUP) --vnet-name $(VNET) --name nodenet --address-prefixes $(NODE_SUBNET_CIDR) -o none
129-
@i=1; \
130-
for subnet in $(EXTRA_SUBNETS); do \
131-
prefix=$$(echo $(EXTRA_SUBNET_CIDRS) | cut -d',' -f$$i); \
132-
$(AZCLI) network vnet subnet create -g $(GROUP) --vnet-name $(VNET) --name $$subnet --address-prefixes $$prefix -o none; \
133-
i=$$((i+1)); \
134-
done
135-
136126
swift-net-up: ## Create vnet, nodenet and podnet subnets
137127
$(AZCLI) network vnet create -g $(GROUP) -l $(REGION) --name $(VNET) --address-prefixes 10.0.0.0/8 -o none
138128
$(AZCLI) network vnet subnet create -g $(GROUP) --vnet-name $(VNET) --name nodenet --address-prefixes 10.240.0.0/16 -o none

0 commit comments

Comments
 (0)