Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions dist/images/kubectl-ko
Original file line number Diff line number Diff line change
Expand Up @@ -811,26 +811,29 @@ diagnose(){
esac
}

getLeaderPod(){
local label=$1
local role=$2
local result=
for i in $(seq 1 10); do
set +o pipefail
result=$(kubectl get pod -n $KUBE_OVN_NS -l "$label"=true 2>/dev/null | grep ovn-central | awk '{if($2=="1/1" && $3=="Running") print $1}' | head -n 1)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using grep to filter pods by name can be brittle. It's better to use a more specific label selector with kubectl to ensure you're selecting the correct pods. The ovn-central pods have the app=ovn-central label, which can be used for more precise selection.

Suggested change
result=$(kubectl get pod -n $KUBE_OVN_NS -l "$label"=true 2>/dev/null | grep ovn-central | awk '{if($2=="1/1" && $3=="Running") print $1}' | head -n 1)
result=$(kubectl get pod -n $KUBE_OVN_NS -l "app=ovn-central,$label=true" 2>/dev/null | awk '{if($2=="1/1" && $3=="Running") print $1}' | head -n 1)

set -o pipefail
if [ -n "$result" ]; then
echo "$result"
return
fi
sleep 1
done
echo "$role leader not exists" >&2
exit 1
}

getOvnCentralPod(){
NB_POD=$(kubectl get pod -n $KUBE_OVN_NS -l ovn-nb-leader=true | grep ovn-central | awk '{if($2=="1/1" && $3=="Running") print $1}' | head -n 1 )
if [ -z "$NB_POD" ]; then
echo "nb leader not exists"
exit 1
fi
OVN_NB_POD=$NB_POD
SB_POD=$(kubectl get pod -n $KUBE_OVN_NS -l ovn-sb-leader=true | grep ovn-central | awk '{if($2=="1/1" && $3=="Running") print $1}' | head -n 1 )
if [ -z "$SB_POD" ]; then
echo "sb leader not exists"
exit 1
fi
OVN_SB_POD=$SB_POD
NORTHD_POD=$(kubectl get pod -n kube-system -l ovn-northd-leader=true | grep ovn-central | head -n 1 | awk '{print $1}')
if [ -z "$NORTHD_POD" ]; then
echo "ovn northd not exists"
exit 1
fi
OVN_NORTHD_POD=$NORTHD_POD
image=$(kubectl -n kube-system get pods -l app=kube-ovn-cni -o jsonpath='{.items[0].spec.containers[0].image}')
OVN_NB_POD=$(getLeaderPod "ovn-nb-leader" "nb")
OVN_SB_POD=$(getLeaderPod "ovn-sb-leader" "sb")
OVN_NORTHD_POD=$(getLeaderPod "ovn-northd-leader" "northd")
image=$(kubectl -n $KUBE_OVN_NS get pods -l app=kube-ovn-cni -o jsonpath='{.items[0].spec.containers[0].image}')
if [ -z "$image" ]; then
echo "cannot get kube-ovn image"
exit 1
Expand Down