-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.sh
More file actions
1751 lines (1413 loc) · 54.3 KB
/
test.sh
File metadata and controls
1751 lines (1413 loc) · 54.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# sal=c4f01b22-f4c8-495a-bd4d-2df90498b81b
# while getopts "a:" opt; do
# case $opt in
# a)
# AUTH_TOKEN="$OPTARG"
# ;;
# *)
# echo "Usage: $0 -a <AUTH_TOKEN>"
# exit 1
# ;;
# esac
# done
# # Check if AUTH_TOKEN is provided
# if [[ -z "$AUTH_TOKEN" ]]; then
# echo "Error: AUTH_TOKEN is required."
# echo "Usage: $0 -a <AUTH_TOKEN>"
# exit 1
# fi
echo "auth to aks..."
az aks get-credentials --resource-group dala-aks-runner23 --name aks --overwrite-existing --admin || exit 1
apk add --no-cache curl
if ! command -v kubectl &> /dev/null; then
echo "kubectl not found! Installing..."
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
wait
chmod +x kubectl
mv kubectl /usr/local/bin/
kubectl version --client
fi
if ! command -v helm &> /dev/null; then
echo "helm not found! Installing..."
apk add --no-cache helm
wait
helm version
fi
apk add --no-cache jq
apk add --no-cache util-linux
apk add --no-cache jq
apk add --no-cache gettext
########### Port Forwarding Setup ###########
DNC_POD=$(kubectl get pods -n default -l app=dnc -o jsonpath='{.items[0].metadata.name}')
echo "DNC Pod Name: $DNC_POD"
# DNC_POD=$DNC_POD_NAME
NAMESPACE="default" # Replace with the namespace of the DNC deployment
LABEL_SELECTOR="app=dnc" # Replace with the label selector for the DNC pod
LOCAL_PORT=9000 # Local port to forward
REMOTE_PORT=9000 # Pod's port to forward
# DNC_POD="dnc-7f75b67795-cpc5b" # TODO: make it from az command
# Start port forwarding
echo "Starting port forwarding from localhost:$LOCAL_PORT to $DNC_POD:$REMOTE_PORT..."
kubectl port-forward -n "$NAMESPACE" pod/"$DNC_POD" "$LOCAL_PORT:$REMOTE_PORT" & PORT_FORWARD_PID=$!
# Wait for port forwarding to establish
sleep 20
# Check if the port forwarding process is running
if ! kill -0 $PORT_FORWARD_PID 2>/dev/null; then
echo "Error: Port forwarding failed to start"
exit 1
fi
# Log the forwarded URL
DNC_URL="http://localhost:$LOCAL_PORT"
echo "Successfully port forwarded to DNC: $DNC_URL"
########### Port Forwarding Setup End ###########
############################ Deploy Pods ###########################
# Define an array of pods with their details
PODS=(
"container2-pod|linuxpool180000000|container2.yaml|cx=vm2" # Format: POD_NAME|NODE_NAME|POD_YAML|LABEL_SELECTOR TODO: Make it come from inputs
"container1-pod|linuxpool181000000|container1.yaml|cx=vm1"
)
NAMESPACE="default" # Replace with the namespace of the DNC deployment
POD_HEALTH_CHECK_RETRY_COUNT=10 # Number of retry attempts
POD_HEALTH_CHECK_RETRY_DELAY=5 # Delay between retries in seconds
# Function to deploy a pod
deploy_pod() {
local POD_NAME=$1
local NODE_NAME=$2
local POD_YAML=$3
local LABEL_SELECTOR=$4
echo "Deploying pod: $POD_NAME on node: $NODE_NAME with YAML: $POD_YAML"
# Label the node
kubectl label node "$NODE_NAME" "$LABEL_SELECTOR" --overwrite
envsubst < "$POD_YAML" > temp.yaml && mv temp.yaml "$POD_YAML"
# Apply the pod YAML
kubectl apply -f "$POD_YAML" -n "$NAMESPACE"
echo "Pod $POD_NAME deployed"
}
# Main script logic
echo "Starting orchestration..."
PRIVATE_IPS=()
# Iterate over the pods and deploy each one
for pod in "${PODS[@]}"; do
IFS="|" read -r POD_NAME NODE_NAME POD_YAML LABEL_SELECTOR <<< "$pod"
# Deploy the pod
deploy_pod "$POD_NAME" "$NODE_NAME" "$POD_YAML" "$LABEL_SELECTOR"
sleep 60
# Get the private IP of the pod
PRIVATE_IP=$(kubectl get pod "$POD_NAME" -n "$NAMESPACE" -o jsonpath='{.status.podIP}')
echo "Private IP of Pod $POD_NAME: $PRIVATE_IP"
# Add the private IP to the array
PRIVATE_IPS+=("$POD_NAME|$PRIVATE_IP")
done
echo "{\"privateIPs\": $(printf '%s\n' "${PRIVATE_IPS[@]}" | jq -R . | jq -s .)}" > $AZ_SCRIPTS_OUTPUT_PATH
# echo "All pods deployed and verified successfully."
############ Stop port forwarding after the operation #############
# Stop port forwarding
echo "Stopping port forwarding..."
kill $PORT_FORWARD_PID
sleep 20
# Verify the process has stopped
if kill -0 $PORT_FORWARD_PID 2>/dev/null; then
echo "Error: Failed to stop port forwarding"
exit 1
fi
echo "Port forwarding stopped successfully."
# # Create Worker node pool(s)
# echo "Creating Worker node pool(s)..."
# WORKER_VMSSES=("linuxpool190") # TODO : make it come from inputs
# INSTANCE_NAMES=()
# # Loop through VMSS names and create VMSS
# for VMSS_NAME in "${WORKER_VMSSES[@]}"; do
# EXTENSION_NAME="NodeJoin-${VMSS_NAME}" # Unique extension name for each VMSS
# echo "Creating VMSS: $VMSS_NAME with extension: $EXTENSION_NAME"
# az deployment group create \
# --name "vmss-deployment-${VMSS_NAME}" \
# --resource-group "$RESOURCE_GROUP" \
# --template-file "$BICEP_TEMPLATE_PATH" \
# --parameters vnetname="$INFRA_VNET_NAME" \
# subnetname="$INFRA_SUBNET_NAME" \
# name="$VMSS_NAME" \
# adminPassword="$ADMIN_PASSWORD" \
# vnetrgname="$RESOURCE_GROUP" \
# vmsssku="Standard_E8s_v3" \
# location="westus" \
# extensionName="$EXTENSION_NAME" > "./lin-script-${VMSS_NAME}.log" 2>&1 &
# wait
# INSTANCE_IDS=$(az vmss list-instances --resource-group "$RESOURCE_GROUP" --name "$VMSS_NAME" --query "[].instanceId" -o tsv)
# for INSTANCE_ID in $INSTANCE_IDS; do
# INSTANCE_NAME=$(az vmss get-instance-view --resource-group "$RESOURCE_GROUP" --name "$VMSS_NAME" --instance-id "$INSTANCE_ID" --query "osProfile.computerName" -o tsv)
# INSTANCE_NAMES+=("$INSTANCE_NAME")
# done
# done
# sleep 240
# # Label the worker nodes and deploy the cns ConfigMap and DaemonSet
# echo "Labeling worker nodes..."
# WORKER_NODES=("linuxpool190000000") # TODO : make it come from inpu
# # Label key and value
# LABEL_KEY="kubernetes.azure.com/mode"
# LABEL_VALUE="user"
# # Loop through each node and apply the label
# for NODE in "${WORKER_NODES[@]}"; do
# kubectl label node "$NODE" "$LABEL_KEY=$LABEL_VALUE" --overwrite
# kubectl label node "$NODE" node-type=cnscni --overwrite
# echo "Successfully labeled node: $NODE"
# done
# # Assign MI to worker nodes to access runner worker image
# echo "Assigning Managed Identity to worker nodes..."
# for VMSS in "${WORKER_VMSSES[@]}"; do
# az vmss identity assign --name $VMSS --resource-group $RESOURCE_GROUP --identities $AKS_KUBERNETES_SERVICE_MANAGED_IDENTITY_CLIENT_ID
# wait
# done
# DNC_POD=$(kubectl get pods -n default -l app=dnc -o jsonpath='{.items[0].metadata.name}')
# echo "DNC Pod Name: $DNC_POD"
# # DNC_POD=$DNC_POD_NAME
# NAMESPACE="default" # Replace with the namespace of the DNC deployment
# LABEL_SELECTOR="app=dnc" # Replace with the label selector for the DNC pod
# LOCAL_PORT=9000 # Local port to forward
# REMOTE_PORT=9000 # Pod's port to forward
# # DNC_POD="dnc-7f75b67795-cpc5b" # TODO: make it from az command
# # Start port forwarding
# echo "Starting port forwarding from localhost:$LOCAL_PORT to $DNC_POD:$REMOTE_PORT..."
# kubectl port-forward -n "$NAMESPACE" pod/"$DNC_POD" "$LOCAL_PORT:$REMOTE_PORT" & PORT_FORWARD_PID=$!
# # Wait for port forwarding to establish
# sleep 20
# # Check if the port forwarding process is running
# if ! kill -0 $PORT_FORWARD_PID 2>/dev/null; then
# echo "Error: Port forwarding failed to start"
# exit 1
# fi
# # Log the forwarded URL
# DNC_URL="http://localhost:$LOCAL_PORT"
# echo "Successfully port forwarded to DNC: $DNC_URL"
# NODES=(
# "linuxpool190000000"
# )
# # Initialize an empty array to store the formatted NODES
# FORMATTED_NODES=()
# # Loop through each node in the NODES array
# for NODE in "${NODES[@]}"; do
# # Get the internal IP of the node using kubectl
# NODE_IP=$(kubectl get node "$NODE" -o jsonpath='{.status.addresses[?(@.type=="InternalIP")].address}')
# # Append the formatted NODE_ID|NODE_IP to the FORMATTED_NODES array
# FORMATTED_NODES+=("$NODE|$NODE_IP")
# done
# # Update the NODES array with the formatted values
# NODES=("${FORMATTED_NODES[@]}")
# DNC_ENDPOINT=$DNC_URL # Replace with the actual DNC endpoint
# JSON_CONTENT_TYPE="application/json"
# # Function to register a node
# register_node() {
# local NODE_ID=$1
# local NODE_IP=$2
# echo "Registering node: $NODE_ID with IP: $NODE_IP"
# # Node information payload
# NODE_INFO_JSON=$(cat <<EOF
# {
# "IPAddresses": ["$NODE_IP"],
# "OrchestratorType": "Kubernetes",
# "InfrastructureNetwork": "52ebbf7f-eb3b-4eea-8ef6-51fe3e2d8bcd",
# "AZID": "",
# "NodeType": "",
# "NodeSet": "",
# "NumCores": 8,
# "DualstackEnabled": false
# }
# EOF
# )
# # Send HTTP POST request to add the node
# response=$(curl -s -w "%{http_code}" -o /tmp/add_node_response_$NODE_ID.json -X POST "$DNC_ENDPOINT/nodes/$NODE_ID?api-version=2018-03-01" \
# -H "Content-Type: $JSON_CONTENT_TYPE" \
# -d "$NODE_INFO_JSON")
# # Extract HTTP status code
# http_status=$(tail -n1 <<< "$response")
# # Check if the request was successful
# if [[ "$http_status" -ne 200 ]]; then
# echo "Failed to add node $NODE_ID. HTTP status: $http_status"
# cat /tmp/add_node_response_$NODE_ID.json
# return 1
# fi
# echo "Node $NODE_ID added successfully!"
# cat /tmp/add_node_response_$NODE_ID.json
# }
# # Iterate over the nodes and register each one
# for node in "${NODES[@]}"; do
# IFS="|" read -r NODE_ID NODE_IP <<< "$node"
# if ! register_node "$NODE_ID" "$NODE_IP"; then
# echo "Error: Failed to register node $NODE_ID"
# exit 1
# fi
# done
# echo "All nodes registered successfully!"
# CUSTOMER_VNET_GUID="b4d2d13d-ee06-4eff-ba82-95d182f40a71"
# DNC_API_ENDPOINT=$DNC_URL # Replace with the actual DNC endpoint
# POD_NAMESPACE="default" # Replace with the pod namespace
# RETRY_COUNT=20 # Number of retry attempts
# RETRY_DELAY=3 # Delay between retries in seconds
# IP_CONSTRAINT=""
# NODE_CONSTRAINT=""
# SECONDARY_IP_COUNT=0
# PRIMARY_IP_PREFIX_BITS=0
# CONTAINER_TYPE="AzureContainerInstance"
# OWNER_ID=""
# RESERVATION_ID=""
# RESERVATION_SET_ID=""
# HOST_TO_NC=false
# NC_TO_HOST=false
# nc_id=""
# # NC_NODES=(
# # "linuxpool160000000|10.224.0.76|container1-pod" # Format: NODE_NAME|NODE_IP|POD_NAME TODO: Make it come from inputs
# # "linuxpool161000000|10.224.0.78|container2-pod"
# # )
# # CUSTOMER_VNET_GUID="3f84330f-6410-4996-bb28-78513d2eb093" # TODO: make it come from inputs
# # Define an array of POD_NAMES corresponding to the nodes
# POD_NAMES=(
# #"container1-pod"
# "container2-pod"
# )
# # Initialize an empty array to store the updated NODES
# NC_NODES=()
# # Loop through the NODES array and append the corresponding POD_NAME
# for i in "${!NODES[@]}"; do
# NODE="${NODES[i]}"
# POD_NAME="${POD_NAMES[i]}"
# NC_NODES+=("$NODE|$POD_NAME")
# done
# CUSTOMER_SUBNET_NAMES=(
# "delegatedSubnet"
# # "delegatedSubnet1"
# )
# # Function to create a network container (NC)
# create_nc() {
# local NODE_NAME=$1
# local NODE_IP=$2
# local POD_NAME=$3
# local CUSTOMER_SUBNET_NAME=$4
# local NC_ID=$(uuidgen) # Generate a unique NC ID
# nc_id="$NC_ID"
# echo "Attempting to create NC: $NC_ID on node: $NODE_NAME with pod: $POD_NAME"
# # Construct the NC request payload
# if [[ -z "$RESERVATION_ID" && -z "$RESERVATION_SET_ID" ]]; then
# # V2 request
# nc_request=$(cat <<EOF
# {
# "AllocationRequest": {
# "SubnetName": "$CUSTOMER_SUBNET_NAME",
# "IPConstraint": "$IP_CONSTRAINT",
# "NodeConstraint": "$NODE_CONSTRAINT",
# "SecondaryIPCount": $SECONDARY_IP_COUNT,
# "PrimaryIPPrefixBits": $PRIMARY_IP_PREFIX_BITS
# },
# "AssociationInfo": {
# "NodeID": "$NODE_NAME",
# "InterfaceID": "$NODE_IP",
# "ContainerType": "$CONTAINER_TYPE",
# "OrchestratorContext": {
# "PodName": "$POD_NAME",
# "PodNamespace": "$POD_NAMESPACE"
# }
# },
# "AllowHostToNCCommunication": $HOST_TO_NC,
# "AllowNCToHostCommunication": $NC_TO_HOST,
# "OwnerID": "$OWNER_ID"
# }
# EOF
# )
# else
# # V1 request
# nc_request=$(cat <<EOF
# {
# "ReservationID": "$RESERVATION_ID",
# "ReservationSetID": "$RESERVATION_SET_ID",
# "AssociationInfo": {
# "NodeID": "$NODE_NAME",
# "InterfaceID": "$NODE_IP",
# "ContainerType": "$CONTAINER_TYPE",
# "OrchestratorContext": {
# "PodName": "$POD_NAME",
# "PodNamespace": "$POD_NAMESPACE"
# }
# },
# "AllowHostToNCCommunication": $HOST_TO_NC,
# "AllowNCToHostCommunication": $NC_TO_HOST,
# "OwnerID": "$OWNER_ID"
# }
# EOF
# )
# fi
# echo "NC request payload: $nc_request"
# # Send the POST request to create the NC
# response=$(curl -s -w "%{http_code}" -o /tmp/create_nc_response_$NC_ID.json -X POST "$DNC_API_ENDPOINT/networks/$CUSTOMER_VNET_GUID/networkcontainer/$NC_ID?api-version=2018-03-01" \
# -H "Content-Type: application/json" \
# -d "$nc_request")
# # Extract HTTP status code
# http_status=$(tail -n1 <<< "$response")
# # Check if the request was successful or if there was a conflict
# if [[ "$http_status" -ne 200 && "$http_status" -ne 409 ]]; then
# echo "Failed to create NC $NC_ID. HTTP status: $http_status"
# cat /tmp/create_nc_response_$NC_ID.json
# return 1
# fi
# echo "Successfully created NC: $NC_ID"
# cat /tmp/create_nc_response_$NC_ID.json
# }
# # Function to poll the NC status
# poll_nc_status() {
# local NC_ID=$1
# echo "Polling status of NC: $NC_ID"
# # Send the GET request to check the NC status
# response=$(curl -s -w "%{http_code}" -o /tmp/nc_status_response_$NC_ID.json -X GET "$DNC_API_ENDPOINT/networks/$CUSTOMER_VNET_GUID/networkcontainer/$NC_ID/status?api-version=2018-03-01" \
# -H "Content-Type: application/json")
# # Extract HTTP status code
# http_status=$(tail -n1 <<< "$response")
# # Check if the request was successful
# if [[ "$http_status" -ne 200 ]]; then
# echo "Failed to get status for NC $NC_ID. HTTP status: $http_status"
# cat /tmp/nc_status_response_$NC_ID.json
# return 1
# fi
# # Parse the status from the response
# nc_status=$(jq -r '.Status' /tmp/nc_status_response_$NC_ID.json)
# if [[ "$nc_status" != "Completed" ]]; then
# echo "NC $NC_ID status is not 'Completed'. Current status: $nc_status"
# return 1
# fi
# echo "NC $NC_ID status is 'Completed'."
# }
# # Iterate over the nodes and register NCs for each
# for index in "${!NC_NODES[@]}"; do
# IFS="|" read -r NODE_NAME NODE_IP POD_NAME <<< "${NC_NODES[index]}"
# CUSTOMER_SUBNET_NAME="${CUSTOMER_SUBNET_NAMES[index]}"
# # Retry logic for creating the NC
# attempt=1
# while [[ $attempt -le $RETRY_COUNT ]]; do
# if create_nc "$NODE_NAME" "$NODE_IP" "$POD_NAME" "$CUSTOMER_SUBNET_NAME"; then
# echo "Create NC succeeded on attempt $attempt for node: $NODE_NAME."
# break
# fi
# echo "Create NC failed on attempt $attempt for node: $NODE_NAME. Retrying in $RETRY_DELAY seconds..."
# sleep "$RETRY_DELAY"
# attempt=$((attempt + 1))
# done
# if [[ $attempt -gt $RETRY_COUNT ]]; then
# echo "Failed to create NC for node: $NODE_NAME after $RETRY_COUNT attempts."
# exit 1
# fi
# # Retry logic for polling the NC status
# attempt=1
# while [[ $attempt -le $RETRY_COUNT ]]; do
# if poll_nc_status "$nc_id"; then
# echo "NC status check succeeded on attempt $attempt for node: $NODE_NAME."
# break
# fi
# echo "NC status check failed on attempt $attempt for node: $NODE_NAME. Retrying in $RETRY_DELAY seconds..."
# sleep "$RETRY_DELAY"
# attempt=$((attempt + 1))
# done
# if [[ $attempt -gt $RETRY_COUNT ]]; then
# echo "Failed to verify NC status for node: $NODE_NAME after $RETRY_COUNT attempts."
# exit 1
# fi
# done
# echo "All NCs registered and verified successfully!"
# NAMESPACE="default" # Replace with the namespace of the DNC deployment
# LABEL_SELECTOR="app=dnc" # Replace with the label selector for the DNC pod
# LOCAL_PORT=9000 # Local port to forward
# REMOTE_PORT=9000 # Pod's port to forward
# DNC_POD="dnc-7f75b67795-cpc5b"
# # Start port forwarding
# echo "Starting port forwarding from localhost:$LOCAL_PORT to $DNC_POD:$REMOTE_PORT..."
# kubectl port-forward -n "$NAMESPACE" pod/"$DNC_POD" "$LOCAL_PORT:$REMOTE_PORT" & PORT_FORWARD_PID=$!
# # Wait for port forwarding to establish
# sleep 10
# # Check if the port forwarding process is running
# if ! kill -0 $PORT_FORWARD_PID 2>/dev/null; then
# echo "Error: Port forwarding failed to start"
# exit 1
# fi
# # Log the forwarded URL
# DNC_URL="http://localhost:$LOCAL_PORT"
# echo "Successfully port forwarded to DNC: $DNC_URL"
# ##############################################################
# # Define an array of pods with their details
# PODS=(
# "container1-pod|linuxpool180000000|container1.yaml|cx=vm1|container1-service" # Format: POD_NAME|NODE_NAME|POD_YAML|LABEL_SELECTOR TODO: Make it come from inputs
# "container2-pod|linuxpool181000000|container2.yaml|cx=vm2|container2-service"
# )
# NAMESPACE="default" # Replace with the namespace of the DNC deployment
# POD_HEALTH_CHECK_RETRY_COUNT=10 # Number of retry attempts
# POD_HEALTH_CHECK_RETRY_DELAY=5 # Delay between retries in seconds
# export RESOURCE_GROUP="dala-aks-runner8"
# # Function to deploy a pod
# deploy_pod() {
# local POD_NAME=$1
# local NODE_NAME=$2
# local POD_YAML=$3
# local LABEL_SELECTOR=$4
# echo "Deploying pod: $POD_NAME on node: $NODE_NAME with YAML: $POD_YAML"
# # Label the node
# kubectl label node "$NODE_NAME" "$LABEL_SELECTOR" --overwrite
# envsubst < "$POD_YAML" > temp.yaml && mv temp.yaml "$POD_YAML"
# # Apply the pod YAML
# kubectl apply -f "$POD_YAML" -n "$NAMESPACE"
# echo "Pod $POD_NAME deployed"
# }
# # Main script logic
# echo "Starting orchestration..."
# # Iterate over the pods and deploy each one
# for pod in "${PODS[@]}"; do
# IFS="|" read -r POD_NAME NODE_NAME POD_YAML LABEL_SELECTOR SERVICE_NAME <<< "$pod"
# # Deploy the pod
# deploy_pod "$POD_NAME" "$NODE_NAME" "$POD_YAML" "$LABEL_SELECTOR"
# # Get the private IP of the pod
# PRIVATE_IP=$(kubectl get pod "$POD_NAME" -n "$NAMESPACE" -o jsonpath='{.status.podIP}')
# echo "Private IP of Pod $POD_NAME: $PRIVATE_IP"
# # Get the public IP and FQDN of the service
# PUBLIC_IP=$(kubectl get service "$SERVICE_NAME" -n "$NAMESPACE" -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
# PUBLIC_FQDN=$(kubectl get service "$SERVICE_NAME" -n "$NAMESPACE" -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')
# echo "Public IP of Service $SERVICE_NAME: $PUBLIC_IP"
# echo "Public FQDN of Service $SERVICE_NAME: $PUBLIC_FQDN"
# done
######################################
# echo "All pods deployed and verified successfully."
############################################################
# DNC_API_ENDPOINT=$DNC_URL # Replace with the actual DNC endpoint
# POD_NAMESPACE="default" # Replace with the pod namespace
# RETRY_COUNT=20 # Number of retry attempts
# RETRY_DELAY=3 # Delay between retries in seconds
# IP_CONSTRAINT=""
# NODE_CONSTRAINT=""
# SECONDARY_IP_COUNT=0
# PRIMARY_IP_PREFIX_BITS=0
# CONTAINER_TYPE="AzureContainerInstance"
# OWNER_ID=""
# RESERVATION_ID=""
# RESERVATION_SET_ID=""
# HOST_TO_NC=false
# NC_TO_HOST=false
# nc_id=""
# NC_NODES=(
# "linuxpool180000000|10.224.0.90|container1-pod" # Format: NODE_NAME|NODE_IP|POD_NAME TODO: Make it come from inputs
# "linuxpool181000000|10.224.0.94|container2-pod"
# )
# CUSTOMER_VNET_GUID="3f84330f-6410-4996-bb28-78513d2eb093" # TODO: make it come from inputs
# CUSTOMER_SUBNET_NAME="delegatedSubnet" # TODO: make it come from inputs
# # Function to create a network container (NC)
# create_nc() {
# local NODE_NAME=$1
# local NODE_IP=$2
# local POD_NAME=$3
# local NC_ID=$(uuidgen) # Generate a unique NC ID
# nc_id="$NC_ID" # Store the NC ID for later use
# echo "Attempting to create NC: $NC_ID on node: $NODE_NAME with pod: $POD_NAME"
# # Construct the NC request payload
# if [[ -z "$RESERVATION_ID" && -z "$RESERVATION_SET_ID" ]]; then
# # V2 request
# nc_request=$(cat <<EOF
# {
# "AllocationRequest": {
# "SubnetName": "$CUSTOMER_SUBNET_NAME",
# "IPConstraint": "$IP_CONSTRAINT",
# "NodeConstraint": "$NODE_CONSTRAINT",
# "SecondaryIPCount": $SECONDARY_IP_COUNT,
# "PrimaryIPPrefixBits": $PRIMARY_IP_PREFIX_BITS
# },
# "AssociationInfo": {
# "NodeID": "$NODE_NAME",
# "InterfaceID": "$NODE_IP",
# "ContainerType": "$CONTAINER_TYPE",
# "OrchestratorContext": {
# "PodName": "$POD_NAME",
# "PodNamespace": "$POD_NAMESPACE"
# }
# },
# "AllowHostToNCCommunication": $HOST_TO_NC,
# "AllowNCToHostCommunication": $NC_TO_HOST,
# "OwnerID": "$OWNER_ID"
# }
# EOF
# )
# else
# # V1 request
# nc_request=$(cat <<EOF
# {
# "ReservationID": "$RESERVATION_ID",
# "ReservationSetID": "$RESERVATION_SET_ID",
# "AssociationInfo": {
# "NodeID": "$NODE_NAME",
# "InterfaceID": "$NODE_IP",
# "ContainerType": "$CONTAINER_TYPE",
# "OrchestratorContext": {
# "PodName": "$POD_NAME",
# "PodNamespace": "$POD_NAMESPACE"
# }
# },
# "AllowHostToNCCommunication": $HOST_TO_NC,
# "AllowNCToHostCommunication": $NC_TO_HOST,
# "OwnerID": "$OWNER_ID"
# }
# EOF
# )
# fi
# echo "NC request payload: $nc_request"
# # Send the POST request to create the NC
# response=$(curl -s -w "%{http_code}" -o /tmp/create_nc_response_$NC_ID.json -X POST "$DNC_API_ENDPOINT/networks/$CUSTOMER_VNET_GUID/networkcontainer/$NC_ID?api-version=2018-03-01" \
# -H "Content-Type: application/json" \
# -d "$nc_request")
# # Extract HTTP status code
# http_status=$(tail -n1 <<< "$response")
# # Check if the request was successful or if there was a conflict
# if [[ "$http_status" -ne 200 && "$http_status" -ne 409 ]]; then
# echo "Failed to create NC $NC_ID. HTTP status: $http_status"
# cat /tmp/create_nc_response_$NC_ID.json
# return 1
# fi
# echo "Successfully created NC: $NC_ID"
# cat /tmp/create_nc_response_$NC_ID.json
# }
# # Function to poll the NC status
# poll_nc_status() {
# local NC_ID=$1
# echo "Polling status of NC: $NC_ID"
# # Send the GET request to check the NC status
# response=$(curl -s -w "%{http_code}" -o /tmp/nc_status_response_$NC_ID.json -X GET "$DNC_API_ENDPOINT/networks/$CUSTOMER_VNET_GUID/networkcontainer/$NC_ID/status?api-version=2018-03-01" \
# -H "Content-Type: application/json")
# # Extract HTTP status code
# http_status=$(tail -n1 <<< "$response")
# # Check if the request was successful
# if [[ "$http_status" -ne 200 ]]; then
# echo "Failed to get status for NC $NC_ID. HTTP status: $http_status"
# cat /tmp/nc_status_response_$NC_ID.json
# return 1
# fi
# # Parse the status from the response
# nc_status=$(jq -r '.Status' /tmp/nc_status_response_$NC_ID.json)
# if [[ "$nc_status" != "Completed" ]]; then
# echo "NC $NC_ID status is not 'Completed'. Current status: $nc_status"
# return 1
# fi
# echo "NC $NC_ID status is 'Completed'."
# }
# # Iterate over the nodes and register NCs for each
# for node in "${NC_NODES[@]}"; do
# IFS="|" read -r NODE_NAME NODE_IP POD_NAME <<< "$node"
# # Retry logic for creating the NC
# attempt=1
# while [[ $attempt -le $RETRY_COUNT ]]; do
# if create_nc "$NODE_NAME" "$NODE_IP" "$POD_NAME"; then
# echo "Create NC succeeded on attempt $attempt for node: $NODE_NAME."
# break
# fi
# echo "Create NC failed on attempt $attempt for node: $NODE_NAME. Retrying in $RETRY_DELAY seconds..."
# sleep "$RETRY_DELAY"
# attempt=$((attempt + 1))
# done
# if [[ $attempt -gt $RETRY_COUNT ]]; then
# echo "Failed to create NC for node: $NODE_NAME after $RETRY_COUNT attempts."
# exit 1
# fi
# # Retry logic for polling the NC status
# attempt=1
# while [[ $attempt -le $RETRY_COUNT ]]; do
# if poll_nc_status "$nc_id"; then
# echo "NC status check succeeded on attempt $attempt for node: $NODE_NAME."
# break
# fi
# echo "NC status check failed on attempt $attempt for node: $NODE_NAME and nc: $nc_id. Retrying in $RETRY_DELAY seconds..."
# sleep "$RETRY_DELAY"
# attempt=$((attempt + 1))
# done
# if [[ $attempt -gt $RETRY_COUNT ]]; then
# echo "Failed to verify NC status for node: $NODE_NAME after $RETRY_COUNT attempts."
# exit 1
# fi
# done
# echo "All NCs registered and verified successfully!"
#############################################################################
# # Define an array of nodes with their details
# NODES=(
# "linuxpool180000000|10.224.0.90" # Format: NODE_ID|NODE_IP TODO: Make it come from inputs
# "linuxpool181000000|10.224.0.94"
# )
# DNC_ENDPOINT=$DNC_URL # Replace with the actual DNC endpoint
# JSON_CONTENT_TYPE="application/json"
# # Function to register a node
# register_node() {
# local NODE_ID=$1
# local NODE_IP=$2
# echo "Registering node: $NODE_ID with IP: $NODE_IP"
# # Node information payload
# NODE_INFO_JSON=$(cat <<EOF
# {
# "IPAddresses": ["$NODE_IP"],
# "OrchestratorType": "Kubernetes",
# "InfrastructureNetwork": "52ebbf7f-eb3b-4eea-8ef6-51fe3e2d8bcd",
# "AZID": "",
# "NodeType": "",
# "NodeSet": "",
# "NumCores": 8,
# "DualstackEnabled": false
# }
# EOF
# )
# # Send HTTP POST request to add the node
# response=$(curl -s -w "%{http_code}" -o /tmp/add_node_response_$NODE_ID.json -X POST "$DNC_ENDPOINT/nodes/$NODE_ID?api-version=2018-03-01" \
# -H "Content-Type: $JSON_CONTENT_TYPE" \
# -d "$NODE_INFO_JSON")
# # Extract HTTP status code
# http_status=$(tail -n1 <<< "$response")
# # Check if the request was successful
# if [[ "$http_status" -ne 200 ]]; then
# echo "Failed to add node $NODE_ID. HTTP status: $http_status"
# cat /tmp/add_node_response_$NODE_ID.json
# return 1
# fi
# echo "Node $NODE_ID added successfully!"
# cat /tmp/add_node_response_$NODE_ID.json
# }
# # Iterate over the nodes and register each one
# for node in "${NODES[@]}"; do
# IFS="|" read -r NODE_ID NODE_IP <<< "$node"
# if ! register_node "$NODE_ID" "$NODE_IP"; then
# echo "Error: Failed to register node $NODE_ID"
# exit 1
# fi
# done
# echo "All nodes registered successfully!"
############################################################
# echo "Labeling worker nodes..."
# WORKER_NODES=("linuxpool180000000" "linuxpool181000000") # TODO : make it come from inputs
# # Label key and value
# LABEL_KEY="kubernetes.azure.com/mode"
# LABEL_VALUE="user"
# # Loop through each node and apply the label
# for NODE in "${WORKER_NODES[@]}"; do
# kubectl label node "$NODE" "$LABEL_KEY=$LABEL_VALUE" --overwrite
# kubectl label node "$NODE" node-type=cnscni --overwrite
# echo "Successfully labeled node: $NODE"
# done
# WORKER_VMSSES=("linuxpool180" "linuxpool181") # TODO : make it come from inputs
# MANAGED_IDENTITY="/subscriptions/9b8218f9-902a-4d20-a65c-e98acec5362f/resourceGroups/dala-aks-runner8/providers/Microsoft.ManagedIdentity/userAssignedIdentities/aksClusterKubeletIdentity"
# # Assign MI to worker nodes to access runner worker image
# echo "Assigning Managed Identity to worker nodes..."
# for VMSS in "${WORKER_VMSSES[@]}"; do
# # echo "Assigning Managed Identity to VMSS: $VMSS"
# # az vmss identity assign --name $VMSS --resource-group dala-aks-runner8 --identities /subscriptions/9b8218f9-902a-4d20-a65c-e98acec5362f/resourceGroups/dala-aks-runner8/providers/Microsoft.ManagedIdentity/userAssignedIdentities/aksClusterKubeletIdentity
# # echo "Successfully assigned Managed Identity to VMSS: $VMSS"
# echo "Checking Managed Identity for VMSS: $VMSS"
# # Check if the VMSS already has the managed identity
# IDENTITY_CHECK=$(az vmss show --name "$VMSS" --resource-group dala-aks-runner8 --query "identity.userAssignedIdentities['$MANAGED_IDENTITY']" --output tsv)
# if [[ -n "$IDENTITY_CHECK" ]]; then
# echo "Managed Identity is already assigned to VMSS: $VMSS. Skipping assignment."
# else
# echo "Managed Identity not found for VMSS: $VMSS. Assigning now..."
# az vmss identity assign --name "$VMSS" --resource-group dala-aks-runner8 --identities "$MANAGED_IDENTITY"
# echo "Successfully assigned Managed Identity to VMSS: $VMSS"
# fi
# done
# # echo "Deploying cns ConfigMap and DaemonSet..."
# # Deploy the cns ConfigMap
# echo "Deploying azure_cns_configmap.yaml to namespace default..."
# kubectl apply -f azure_cns_configmap.yaml -n default
# # Deploy the cns DaemonSet
# echo "Deploying azure_cns_daemonset.yaml to namespace default..."
# kubectl apply -f azure_cns_daemonset.yaml -n default
############# Delete node in DNC #############
# # Variables
# DNC_API_ENDPOINT=$DNC_URL # Replace with your DNC API endpoint
# NODE_ID="linuxpool151000000" # Replace with the node ID to delete
# API_VERSION="2018-03-01" # API version
# # Construct the Node API URL
# NODE_API_URL="$DNC_API_ENDPOINT/nodes/$NODE_ID?api-version=$API_VERSION"
# # Function to delete a node
# delete_node() {
# echo "Attempting to delete node: $NODE_ID"
# # Send the DELETE request
# response=$(curl -s -w "%{http_code}" -o /tmp/delete_node_response.json -X DELETE "$NODE_API_URL")
# # Extract HTTP status code
# http_status=$(tail -n1 <<< "$response")
# # Check if the request was successful
# if [[ "$http_status" -ne 200 ]]; then
# echo "Failed to delete node $NODE_ID. HTTP status: $http_status"
# cat /tmp/delete_node_response.json
# exit 1
# fi
# echo "Successfully deleted node: $NODE_ID"
# cat /tmp/delete_node_response.json
# }
# # Call the function
# delete_node
# sleep 10
###################### Add node to DNC ######################
# Variables
# NODE_ID="linuxpool15000000"
# NODE_IP="10.224.0.69"
# NODE_ID="linuxpool163000000"
# NODE_IP="10.224.0.86"
# DNC_ENDPOINT=$DNC_URL #"https://10.224.0.65:9000" # Replace with the actual DNC endpoint
# NODE_API="$DNC_ENDPOINT/nodes/$NODE_ID?api-version=2018-03-01"
# JSON_CONTENT_TYPE="application/json"
# # Node information payload
# NODE_INFO_JSON=$(cat <<EOF
# {
# "IPAddresses": ["$NODE_IP"],
# "OrchestratorType": "Kubernetes",
# "InfrastructureNetwork": "52ebbf7f-eb3b-4eea-8ef6-51fe3e2d8bcd",
# "AZID": "",
# "NodeType": "",
# "NodeSet": "",
# "NumCores": 8,
# "DualstackEnabled": false
# }
# EOF
# )
# # Send HTTP POST request to add the node
# response=$(curl -s -w "%{http_code}" -o /tmp/add_node_response.json -X POST "$NODE_API" \
# -H "Content-Type: $JSON_CONTENT_TYPE" \
# -d "$NODE_INFO_JSON")
# # Extract HTTP status code
# http_status=$(tail -n1 <<< "$response")
# # Check if the request was successful
# if [[ "$http_status" -ne 200 ]]; then
# echo "Failed to add node. HTTP status: $http_status"
# cat /tmp/add_node_response.json
# exit 1
# fi
# echo "Node added successfully!"
# cat /tmp/add_node_response.json
###########new code #############
# # Define an array of nodes with their details
# NODES=(
# "linuxpool160000000|10.224.0.76" # Format: NODE_ID|NODE_IP
# "linuxpool161000000|10.224.0.78"
# )
# DNC_ENDPOINT=$DNC_URL # Replace with the actual DNC endpoint
# JSON_CONTENT_TYPE="application/json"
# # Function to register a node
# register_node() {
# local NODE_ID=$1
# local NODE_IP=$2
# echo "Registering node: $NODE_ID with IP: $NODE_IP"
# # Node information payload
# NODE_INFO_JSON=$(cat <<EOF
# {
# "IPAddresses": ["$NODE_IP"],
# "OrchestratorType": "Kubernetes",
# "InfrastructureNetwork": "52ebbf7f-eb3b-4eea-8ef6-51fe3e2d8bcd",
# "AZID": "",