Skip to content

Commit

Permalink
Add post init check
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed Jun 3, 2023
1 parent 1cd7722 commit a25ae0d
Show file tree
Hide file tree
Showing 2 changed files with 294 additions and 2 deletions.
148 changes: 147 additions & 1 deletion addons/flannel/0.21.5/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ function flannel_check_nodes_connectivity() {
fi
fi

log "Verifying if all nodes can communicate with each other through port 8472/UDP."
logStep "Verifying if all nodes can communicate with each other through port 8472/UDP."
if ! "$DIR"/bin/kurl netutil nodes-connectivity --port 8472 --image "$KURL_UTIL_IMAGE" --proto udp; then
logFail "Flannel requires UDP port 8472 for communication between nodes."
logFail "Please make sure this port is open prior to running this upgrade."
bail "Not migrating from Weave to Flannel"
fi
logSuccess "Nodes can communicate with each other successfully"
}

function flannel_join() {
Expand Down Expand Up @@ -329,4 +330,149 @@ function flannel_already_applied() {

flannel_ready_spinner
check_network
check_udp_port
}

function flannel_post_init() {
check_udp_port
}

function check_udp_port() {
logStep "Checking Flannel UDP Connection over port 8472"

# Get the number of worker nodes in the cluster
# If we have at least 2 nodes we will use affinity to be able to test it across the nodes
local num_nodes=$(kubectl get nodes --selector='!node-role.kubernetes.io/master' --no-headers | wc -l)

echo "Creating Pods to check communication"
if [[ $num_nodes -gt 1 ]]; then
# Use podAntiAffinity if there are multiple nodes
echo "Create pod to do the test using podAntiAffinity to check the communication across nodes"
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
name: udp-receiver
labels:
app: udp-receiver
spec:
containers:
- name: udp-receiver
image: busybox
command: ["sh", "-c", "nc -lu 8472"]
ports:
- containerPort: 8472
protocol: UDP
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- udp-sender
topologyKey: "kubernetes.io/hostname"
EOF
else
echo "Create pod to do the test without use podAntiAffinity because has only one node"

kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
name: udp-receiver
spec:
containers:
- name: udp-receiver
image: busybox
command: ["sh", "-c", "nc -lu 8472"]
ports:
- containerPort: 8472
protocol: UDP
EOF
fi

if ! spinner_until 120 check_udp_receiver_status; then
logWarn "Timeout waiting for udp-receiver Pod to be in the 'Running' state"
fi

# Get the IP of udp-receiver
receiver_ip=$(kubectl get pod udp-receiver -o jsonpath="{.status.podIP}")

if [[ $num_nodes -gt 1 ]]; then
echo "Creating udp-sender with podAntiAffinity"
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
name: udp-sender
labels:
app: udp-sender
spec:
containers:
- name: udp-sender
image: busybox
command: ["sh", "-c", "echo 'Test message' | nc -u $receiver_ip 8472"]
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- udp-receiver
topologyKey: "kubernetes.io/hostname"
EOF
else
echo "Creating udp-sender without podAntiAffinity"
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
name: udp-sender
spec:
containers:
- name: udp-sender
image: busybox
command: ["sh", "-c", "echo 'Test message' | nc -u $receiver_ip 8472"]
EOF
fi

echo "Waiting up to 2 minutes to check pods created to perform the test are successfully running"
if ! spinner_until 120 udp_pods_are_running; then
logWarn "Pods to check UDP communication with Flannel did not become Ready within the expected time."
fi

echo "Waiting up to 2 minutes to check if the message will be received"
if ! spinner_until 120 message_received; then
logWarn "Unable to check that Flannel UDP communication is working correctly"
fi

echo "Clean up the pods created to do the check"
kubectl delete pod udp-receiver udp-sender
}

# Check the status of the UDP receiver pod
function check_udp_receiver_status() {
local pod_status=$(kubectl get pod udp-receiver -o 'jsonpath={..status.phase}')
[[ "$pod_status" == "Running" ]]
}

# check if udp pods are running
function udp_pods_are_running() {
if [[ $(kubectl get pod udp-receiver -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}') == "True" ]] && \
[[ $(kubectl get pod udp-sender -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}') == "True" ]]; then
return 0
fi
return 1
}

# Checks if the message is received
function message_received() {
if kubectl logs udp-receiver | grep -q "Test message"; then
return 0
fi
return 1
}
148 changes: 147 additions & 1 deletion addons/flannel/template/base/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ function flannel_check_nodes_connectivity() {
fi
fi

log "Verifying if all nodes can communicate with each other through port 8472/UDP."
logStep "Verifying if all nodes can communicate with each other through port 8472/UDP."
if ! "$DIR"/bin/kurl netutil nodes-connectivity --port 8472 --image "$KURL_UTIL_IMAGE" --proto udp; then
logFail "Flannel requires UDP port 8472 for communication between nodes."
logFail "Please make sure this port is open prior to running this upgrade."
bail "Not migrating from Weave to Flannel"
fi
logSuccess "Nodes can communicate with each other successfully"
}

function flannel_join() {
Expand Down Expand Up @@ -329,4 +330,149 @@ function flannel_already_applied() {

flannel_ready_spinner
check_network
check_udp_port
}

function flannel_post_init() {
check_udp_port
}

function check_udp_port() {
logStep "Checking Flannel UDP Connection over port 8472"

# Get the number of worker nodes in the cluster
# If we have at least 2 nodes we will use affinity to be able to test it across the nodes
local num_nodes=$(kubectl get nodes --selector='!node-role.kubernetes.io/master' --no-headers | wc -l)

echo "Creating Pods to check communication"
if [[ $num_nodes -gt 1 ]]; then
# Use podAntiAffinity if there are multiple nodes
echo "Create pod to do the test using podAntiAffinity to check the communication across nodes"
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
name: udp-receiver
labels:
app: udp-receiver
spec:
containers:
- name: udp-receiver
image: busybox
command: ["sh", "-c", "nc -lu 8472"]
ports:
- containerPort: 8472
protocol: UDP
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- udp-sender
topologyKey: "kubernetes.io/hostname"
EOF
else
echo "Create pod to do the test without use podAntiAffinity because has only one node"

kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
name: udp-receiver
spec:
containers:
- name: udp-receiver
image: busybox
command: ["sh", "-c", "nc -lu 8472"]
ports:
- containerPort: 8472
protocol: UDP
EOF
fi

if ! spinner_until 120 check_udp_receiver_status; then
logWarn "Timeout waiting for udp-receiver Pod to be in the 'Running' state"
fi

# Get the IP of udp-receiver
receiver_ip=$(kubectl get pod udp-receiver -o jsonpath="{.status.podIP}")

if [[ $num_nodes -gt 1 ]]; then
echo "Creating udp-sender with podAntiAffinity"
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
name: udp-sender
labels:
app: udp-sender
spec:
containers:
- name: udp-sender
image: busybox
command: ["sh", "-c", "echo 'Test message' | nc -u $receiver_ip 8472"]
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- udp-receiver
topologyKey: "kubernetes.io/hostname"
EOF
else
echo "Creating udp-sender without podAntiAffinity"
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
name: udp-sender
spec:
containers:
- name: udp-sender
image: busybox
command: ["sh", "-c", "echo 'Test message' | nc -u $receiver_ip 8472"]
EOF
fi

echo "Waiting up to 2 minutes to check pods created to perform the test are successfully running"
if ! spinner_until 120 udp_pods_are_running; then
logWarn "Pods to check UDP communication with Flannel did not become Ready within the expected time."
fi

echo "Waiting up to 2 minutes to check if the message will be received"
if ! spinner_until 120 message_received; then
logWarn "Unable to check that Flannel UDP communication is working correctly"
fi

echo "Clean up the pods created to do the check"
kubectl delete pod udp-receiver udp-sender
}

# Check the status of the UDP receiver pod
function check_udp_receiver_status() {
local pod_status=$(kubectl get pod udp-receiver -o 'jsonpath={..status.phase}')
[[ "$pod_status" == "Running" ]]
}

# check if udp pods are running
function udp_pods_are_running() {
if [[ $(kubectl get pod udp-receiver -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}') == "True" ]] && \
[[ $(kubectl get pod udp-sender -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}') == "True" ]]; then
return 0
fi
return 1
}

# Checks if the message is received
function message_received() {
if kubectl logs udp-receiver | grep -q "Test message"; then
return 0
fi
return 1
}

0 comments on commit a25ae0d

Please sign in to comment.