Last active
November 21, 2024 21:56
-
-
Save trozet/6d81a28021b16a980e515be75d71d8e3 to your computer and use it in GitHub Desktop.
Scales up UDNs and pods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Number of namespaces to create | |
NUM_NAMESPACES=50 # Adjust this as needed | |
# Base name for the namespaces and UserDefinedNetworks | |
NAMESPACE_BASE="test-namespace" | |
UDN_BASE="user-defined-network" | |
# Record the start time of the entire script | |
SCRIPT_START_TIME=$(date +%s) | |
# Function to calculate elapsed time | |
elapsed_time() { | |
local start_time=$1 | |
local end_time=$(date +%s) | |
echo "$((end_time - start_time)) seconds" | |
} | |
# Start timing namespace and UDN creation | |
CREATION_START_TIME=$(date +%s) | |
# Loop to create namespaces and UserDefinedNetworks | |
for i in $(seq 1 $NUM_NAMESPACES); do | |
# Define namespace name | |
NAMESPACE="${NAMESPACE_BASE}-${i}" | |
# Create the namespace | |
kubectl create namespace "$NAMESPACE" | |
# Define the UserDefinedNetwork name | |
UDN_NAME="${UDN_BASE}-${i}" | |
# Apply the UserDefinedNetwork resource in the created namespace | |
cat <<EOF | kubectl apply -f - | |
apiVersion: k8s.ovn.org/v1 | |
kind: UserDefinedNetwork | |
metadata: | |
name: ${UDN_NAME} | |
namespace: ${NAMESPACE} | |
spec: | |
topology: Layer3 | |
layer3: | |
role: Primary | |
subnets: | |
- cidr: 10.20.0.0/16 | |
EOF | |
echo "Created namespace and UserDefinedNetwork for ${NAMESPACE}" | |
done | |
# Print how long namespace and UDN creation took | |
CREATION_END_TIME=$(date +%s) | |
echo "Namespace and UDN creation took $(elapsed_time $CREATION_START_TIME) seconds." | |
# Start timing UDN condition check | |
UDN_CHECK_START_TIME=$(date +%s) | |
# Function to check if a specific UDN has the desired condition | |
check_udn_condition() { | |
local namespace="$1" | |
local udn_name="$2" | |
kubectl get userdefinednetwork "$udn_name" -n "$namespace" -o jsonpath='{.status.conditions[?(@.reason=="NetworkAllocationSucceeded")].status}' 2>/dev/null | |
} | |
# Wait for all UDNs to reach the desired condition | |
echo "Waiting for all UserDefinedNetworks to reach 'NetworkAllocationSucceeded' condition..." | |
for i in $(seq 1 $NUM_NAMESPACES); do | |
NAMESPACE="${NAMESPACE_BASE}-${i}" | |
UDN_NAME="${UDN_BASE}-${i}" | |
while true; do | |
CONDITION_STATUS=$(check_udn_condition "$NAMESPACE" "$UDN_NAME") | |
if [[ "$CONDITION_STATUS" == "True" ]]; then | |
echo "UserDefinedNetwork $UDN_NAME in namespace $NAMESPACE is ready." | |
break | |
fi | |
sleep 1 | |
done | |
done | |
# Print how long UDN readiness check took | |
UDN_CHECK_END_TIME=$(date +%s) | |
echo "UDN readiness check took $(elapsed_time $UDN_CHECK_START_TIME) seconds." | |
# Start timing pod creation | |
POD_CREATION_START_TIME=$(date +%s) | |
# Create one pod in each namespace | |
echo "Creating pods in all namespaces..." | |
for i in $(seq 1 $NUM_NAMESPACES); do | |
NAMESPACE="${NAMESPACE_BASE}-${i}" | |
# Apply a simple pod manifest | |
cat <<EOF | kubectl apply -f - | |
apiVersion: v1 | |
kind: Pod | |
metadata: | |
name: test-pod | |
namespace: ${NAMESPACE} | |
spec: | |
containers: | |
- name: pause | |
image: k8s.gcr.io/pause:3.9 | |
EOF | |
echo "Created pod in namespace ${NAMESPACE}" | |
done | |
# Print how long pod creation took | |
POD_CREATION_END_TIME=$(date +%s) | |
echo "Pod creation took $(elapsed_time $POD_CREATION_START_TIME) seconds." | |
# Start timing pod readiness check | |
POD_CHECK_START_TIME=$(date +%s) | |
# Function to check if a pod is ready | |
check_pod_ready() { | |
local namespace="$1" | |
local pod_name="$2" | |
kubectl get pod "$pod_name" -n "$namespace" -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}' 2>/dev/null | |
} | |
# Wait for all pods to become ready | |
echo "Waiting for all pods to become ready..." | |
for i in $(seq 1 $NUM_NAMESPACES); do | |
NAMESPACE="${NAMESPACE_BASE}-${i}" | |
POD_NAME="test-pod" | |
while true; do | |
POD_READY_STATUS=$(check_pod_ready "$NAMESPACE" "$POD_NAME") | |
if [[ "$POD_READY_STATUS" == "True" ]]; then | |
echo "Pod $POD_NAME in namespace $NAMESPACE is ready." | |
break | |
fi | |
sleep 5 | |
done | |
done | |
# Print how long pod readiness check took | |
POD_CHECK_END_TIME=$(date +%s) | |
echo "Pod readiness check took $(elapsed_time $POD_CHECK_START_TIME) seconds." | |
# Print total script time | |
SCRIPT_END_TIME=$(date +%s) | |
echo "Total script execution time: $(elapsed_time $SCRIPT_START_TIME)." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment