From 17c146f5cb5724a9894c4912309379f3ca7d0dc9 Mon Sep 17 00:00:00 2001 From: zcq98 Date: Thu, 5 Sep 2024 15:15:31 +0800 Subject: [PATCH] fix: kubectl-ko using kube-ovn-cni pod for nsenter Signed-off-by: zcq98 --- dist/images/kubectl-ko | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/dist/images/kubectl-ko b/dist/images/kubectl-ko index 694ee8942a4..8e8e5a1e517 100755 --- a/dist/images/kubectl-ko +++ b/dist/images/kubectl-ko @@ -177,11 +177,16 @@ tcpdump(){ fi podNicType=$(kubectl get pod "$podName" -n "$namespace" -o jsonpath={.metadata.annotations.ovn\\.kubernetes\\.io/pod_nic_type}) podNetNs=$(kubectl exec "$ovsPod" -n $KUBE_OVN_NS -- ovs-vsctl --data=bare --no-heading get interface "$nicName" external-ids:pod_netns | tr -d '\r' | sed -e 's/^"//' -e 's/"$//') + ovnCni=$(kubectl get pod -n $KUBE_OVN_NS -l app=kube-ovn-cni -o 'jsonpath={.items[?(@.spec.nodeName=="'$nodeName'")].metadata.name}') + if [ -z "$ovnCni" ]; then + echo "kube-ovn-cni not exist on node $nodeName" + exit 1 + fi set -x if [ "$podNicType" = "internal-port" ]; then - kubectl exec "$ovsPod" -n $KUBE_OVN_NS -- nsenter --net="$podNetNs" tcpdump -nn -i "$nicName" "$@" + kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- nsenter --net="$podNetNs" tcpdump -nn -i "$nicName" "$@" else - kubectl exec "$ovsPod" -n $KUBE_OVN_NS -- nsenter --net="$podNetNs" tcpdump -nn -i eth0 "$@" + kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- nsenter --net="$podNetNs" tcpdump -nn -i eth0 "$@" fi fi } @@ -313,6 +318,12 @@ trace(){ exit 1 fi + ovnCni=$(kubectl get pod -n $KUBE_OVN_NS -l app=kube-ovn-cni -o 'jsonpath={.items[?(@.spec.nodeName=="'$node'")].metadata.name}') + if [ -z "$ovnCni" ]; then + echo "kube-ovn-cni not exist on node $node" + exit 1 + fi + local vlan=$(kubectl get subnet "$ls" -o jsonpath={.spec.vlan}) local logicalGateway=$(kubectl get subnet "$ls" -o jsonpath={.spec.logicalGateway}) local u2oIC=$(kubectl get subnet "$ls" -o jsonpath={.spec.u2oInterconnection}) @@ -342,12 +353,12 @@ trace(){ local interface=$(kubectl exec "$ovsPod" -c openvswitch -n $KUBE_OVN_NS -- ovs-vsctl --format=csv --data=bare --no-heading --columns=name find interface external_id:iface-id="$lsp") local peer=$(kubectl exec "$ovsPod" -c openvswitch -n $KUBE_OVN_NS -- ip link show $interface | grep -oE "^[0-9]+:\\s$interface@if[0-9]+" | awk -F @ '{print $2}') local peerIndex=${peer//if/} - local peer=$(kubectl exec "$ovsPod" -c openvswitch -n $KUBE_OVN_NS -- sh -c "$nsenterCmd ip link show type veth" | grep "^$peerIndex:" | awk -F @ '{print $1}') + local peer=$(kubectl exec "$ovnCni" -c cni-server -n $KUBE_OVN_NS -- sh -c "$nsenterCmd ip link show type veth" | grep "^$peerIndex:" | awk -F @ '{print $1}') nicName=$(echo $peer | awk '{print $2}') fi set +o pipefail - local master=$(kubectl exec "$ovsPod" -c openvswitch -n $KUBE_OVN_NS -- sh -c "$nsenterCmd ip link show $nicName" | grep -Eo '\smaster\s\w+\s' | awk '{print $2}') + local master=$(kubectl exec "$ovnCni" -c cni-server -n $KUBE_OVN_NS -- sh -c "$nsenterCmd ip link show $nicName" | grep -Eo '\smaster\s\w+\s' | awk '{print $2}') set -o pipefail if [ ! -z "$master" ]; then echo "Error: Pod nic $nicName is a slave of $master, please set the destination mac address." @@ -357,10 +368,10 @@ trace(){ local cmd= output= if [[ "$gateway" =~ .*:.* ]]; then cmd="ndisc6 -q $gateway $nicName" - output=$(kubectl exec "$ovsPod" -c openvswitch -n $KUBE_OVN_NS -- sh -c "$nsenterCmd ndisc6 -q $gateway $nicName") + output=$(kubectl exec "$ovnCni" -c cni-server -n $KUBE_OVN_NS -- sh -c "$nsenterCmd ndisc6 -q $gateway $nicName") else cmd="arping -c3 -C1 -i1 -I $nicName $gateway" - output=$(kubectl exec "$ovsPod" -c openvswitch -n $KUBE_OVN_NS -- sh -c "$nsenterCmd arping -c3 -C1 -i1 -I $nicName $gateway") + output=$(kubectl exec "$ovnCni" -c cni-server -n $KUBE_OVN_NS -- sh -c "$nsenterCmd arping -c3 -C1 -i1 -I $nicName $gateway") fi if [ $? -ne 0 ]; then