Skip to content

Commit

Permalink
[kubectl-ko] support trace in underlay networking
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzujian committed Jul 13, 2021
1 parent dfc2f1e commit 6499e58
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dist/images/Dockerfile.base
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ FROM ubuntu:20.04

RUN apt update && apt install ca-certificates python3 hostname libunwind8 netbase \
ethtool iproute2 ncat libunbound-dev procps libatomic1 kmod \
tcpdump ipset curl uuid-runtime openssl inetutils-ping \
tcpdump ipset curl uuid-runtime openssl inetutils-ping arping \
logrotate -y --no-install-recommends && \
rm -rf /var/lib/apt/lists/*

Expand Down
35 changes: 34 additions & 1 deletion dist/images/install-pre-1.16.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2242,7 +2242,40 @@ trace(){
exit 1
fi
gwMac=$(kubectl exec $OVN_NB_POD -n $KUBE_OVN_NS -c ovn-central -- ovn-nbctl --data=bare --no-heading --columns=mac find logical_router_port name=ovn-cluster-"$ls" | tr -d '\r')
vlan=$(kubectl get subnet "$ls" -o jsonpath={.spec.vlan})
underlayGateway=$(kubectl get subnet "$ls" -o jsonpath={.spec.underlayGateway})
gwMac=""
if [ ! -z "$vlan" -a "$underlayGateway" = "true" ]; then
ovnCni=$(kubectl get pod -n $KUBE_OVN_NS -o wide | grep -w kube-ovn-cni | grep " $nodeName " | awk '{print $1}')
if [ -z "$ovnCni" ]; then
echo "No kube-ovn-cni Pod running on node $nodeName"
exit 1
fi
nicName=$(kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- ovs-vsctl --data=bare --no-heading --columns=name find interface external-ids:iface-id="$podName"."$namespace" | tr -d '\r')
if [ -z "$nicName" ]; then
echo "nic doesn't exist on node $nodeName"
exit 1
fi
gateway=$(kubectl get subnet "$ls" -o jsonpath={.spec.gateway})
podNetNs=$(kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- ovs-vsctl --data=bare --no-heading get interface "$nicName" external-ids:pod_netns | tr -d '\r')
podNicType=$(kubectl get pod "$podName" -n "$namespace" -o jsonpath={.metadata.annotations.ovn\\.kubernetes\\.io/pod_nic_type})
if [ "$podNicType" != "internal-port" ]; then
nicName="eth0"
fi
output=$(kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- nsenter --net="/var/run/netns/$podNetNs" arping -c1 -i "$nicName" "$gateway")
if [ $? -ne 0 ]; then
echo "failed to run 'arping -c1 -i $nicName $gateway' in Pod netns"
exit 1
fi
gwMac=$(echo "$output" | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}')
else
gwMac=$(kubectl exec $OVN_NB_POD -n $KUBE_OVN_NS -c ovn-central -- ovn-nbctl --data=bare --no-heading --columns=mac find logical_router_port name=ovn-cluster-"$ls" | tr -d '\r')
fi
if [ -z "$gwMac" ]; then
echo "get gw mac failed"
Expand Down
35 changes: 34 additions & 1 deletion dist/images/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2284,7 +2284,40 @@ trace(){
exit 1
fi
gwMac=$(kubectl exec $OVN_NB_POD -n $KUBE_OVN_NS -c ovn-central -- ovn-nbctl --data=bare --no-heading --columns=mac find logical_router_port name=ovn-cluster-"$ls" | tr -d '\r')
vlan=$(kubectl get subnet "$ls" -o jsonpath={.spec.vlan})
underlayGateway=$(kubectl get subnet "$ls" -o jsonpath={.spec.underlayGateway})
gwMac=""
if [ ! -z "$vlan" -a "$underlayGateway" = "true" ]; then
ovnCni=$(kubectl get pod -n $KUBE_OVN_NS -o wide | grep -w kube-ovn-cni | grep " $nodeName " | awk '{print $1}')
if [ -z "$ovnCni" ]; then
echo "No kube-ovn-cni Pod running on node $nodeName"
exit 1
fi
nicName=$(kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- ovs-vsctl --data=bare --no-heading --columns=name find interface external-ids:iface-id="$podName"."$namespace" | tr -d '\r')
if [ -z "$nicName" ]; then
echo "nic doesn't exist on node $nodeName"
exit 1
fi
gateway=$(kubectl get subnet "$ls" -o jsonpath={.spec.gateway})
podNetNs=$(kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- ovs-vsctl --data=bare --no-heading get interface "$nicName" external-ids:pod_netns | tr -d '\r')
podNicType=$(kubectl get pod "$podName" -n "$namespace" -o jsonpath={.metadata.annotations.ovn\\.kubernetes\\.io/pod_nic_type})
if [ "$podNicType" != "internal-port" ]; then
nicName="eth0"
fi
output=$(kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- nsenter --net="/var/run/netns/$podNetNs" arping -c1 -i "$nicName" "$gateway")
if [ $? -ne 0 ]; then
echo "failed to run 'arping -c1 -i $nicName $gateway' in Pod netns"
exit 1
fi
gwMac=$(echo "$output" | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}')
else
gwMac=$(kubectl exec $OVN_NB_POD -n $KUBE_OVN_NS -c ovn-central -- ovn-nbctl --data=bare --no-heading --columns=mac find logical_router_port name=ovn-cluster-"$ls" | tr -d '\r')
fi
if [ -z "$gwMac" ]; then
echo "get gw mac failed"
Expand Down
35 changes: 34 additions & 1 deletion dist/images/kubectl-ko
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,40 @@ trace(){
exit 1
fi

gwMac=$(kubectl exec $OVN_NB_POD -n $KUBE_OVN_NS -c ovn-central -- ovn-nbctl --data=bare --no-heading --columns=mac find logical_router_port name=ovn-cluster-"$ls" | tr -d '\r')
vlan=$(kubectl get subnet "$ls" -o jsonpath={.spec.vlan})
underlayGateway=$(kubectl get subnet "$ls" -o jsonpath={.spec.underlayGateway})

gwMac=""
if [ ! -z "$vlan" -a "$underlayGateway" = "true" ]; then
ovnCni=$(kubectl get pod -n $KUBE_OVN_NS -o wide | grep -w kube-ovn-cni | grep " $nodeName " | awk '{print $1}')
if [ -z "$ovnCni" ]; then
echo "No kube-ovn-cni Pod running on node $nodeName"
exit 1
fi

nicName=$(kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- ovs-vsctl --data=bare --no-heading --columns=name find interface external-ids:iface-id="$podName"."$namespace" | tr -d '\r')
if [ -z "$nicName" ]; then
echo "nic doesn't exist on node $nodeName"
exit 1
fi

gateway=$(kubectl get subnet "$ls" -o jsonpath={.spec.gateway})
podNetNs=$(kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- ovs-vsctl --data=bare --no-heading get interface "$nicName" external-ids:pod_netns | tr -d '\r')

podNicType=$(kubectl get pod "$podName" -n "$namespace" -o jsonpath={.metadata.annotations.ovn\\.kubernetes\\.io/pod_nic_type})
if [ "$podNicType" != "internal-port" ]; then
nicName="eth0"
fi

output=$(kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- nsenter --net="/var/run/netns/$podNetNs" arping -c1 -i "$nicName" "$gateway")
if [ $? -ne 0 ]; then
echo "failed to run 'arping -c1 -i $nicName $gateway' in Pod netns"
exit 1
fi
gwMac=$(echo "$output" | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}')
else
gwMac=$(kubectl exec $OVN_NB_POD -n $KUBE_OVN_NS -c ovn-central -- ovn-nbctl --data=bare --no-heading --columns=mac find logical_router_port name=ovn-cluster-"$ls" | tr -d '\r')
fi

if [ -z "$gwMac" ]; then
echo "get gw mac failed"
Expand Down

0 comments on commit 6499e58

Please sign in to comment.