Skip to content

Commit

Permalink
adapt internal tcpdump
Browse files Browse the repository at this point in the history
  • Loading branch information
hongzhen-ma committed Jun 8, 2021
1 parent 9a44afc commit 2b2df3d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
8 changes: 7 additions & 1 deletion dist/images/install-pre-1.16.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2094,8 +2094,14 @@ tcpdump(){
echo "nic doesn't exist on node $nodeName"
exit 1
fi
podNicType=$(kubectl get pod "$podName" -n "$namespace" -o jsonpath={.metadata.annotations.ovn\\.kubernetes\\.io/pod_nic_type})
podNetNs=$(kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- ovs-vsctl --data=bare --no-heading get interface "$nicName" external-ids:pod_netns | tr -d '\r')
set -x
kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- tcpdump -nn -i "$nicName" "$@"
if [ "$podNicType" = "internal-port" ]; then
kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- ip netns exec "$podNetNs" tcpdump -nn -i "$nicName" "$@"
else
kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- ip netns exec "$podNetNs" tcpdump -nn -i eth0 "$@"
fi
fi
}
Expand Down
9 changes: 8 additions & 1 deletion dist/images/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2144,8 +2144,15 @@ tcpdump(){
echo "nic doesn't exist on node $nodeName"
exit 1
fi
podNicType=$(kubectl get pod "$podName" -n "$namespace" -o jsonpath={.metadata.annotations.ovn\\.kubernetes\\.io/pod_nic_type})
podNetNs=$(kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- ovs-vsctl --data=bare --no-heading get interface "$nicName" external-ids:pod_netns | tr -d '\r')
set -x
kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- tcpdump -nn -i "$nicName" "$@"
if [ "$podNicType" = "internal-port" ]; then
kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- ip netns exec "$podNetNs" tcpdump -nn -i "$nicName" "$@"
else
kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- ip netns exec "$podNetNs" tcpdump -nn -i eth0 "$@"
fi
fi
}
Expand Down
8 changes: 7 additions & 1 deletion dist/images/kubectl-ko
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ tcpdump(){
echo "nic doesn't exist on node $nodeName"
exit 1
fi
podNicType=$(kubectl get pod "$podName" -n "$namespace" -o jsonpath={.metadata.annotations.ovn\\.kubernetes\\.io/pod_nic_type})
podNetNs=$(kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- ovs-vsctl --data=bare --no-heading get interface "$nicName" external-ids:pod_netns | tr -d '\r')
set -x
kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- tcpdump -nn -i "$nicName" "$@"
if [ "$podNicType" = "internal-port" ]; then
kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- ip netns exec "$podNetNs" tcpdump -nn -i "$nicName" "$@"
else
kubectl exec "$ovnCni" -n $KUBE_OVN_NS -- ip netns exec "$podNetNs" tcpdump -nn -i eth0 "$@"
fi
fi
}

Expand Down
9 changes: 6 additions & 3 deletions pkg/daemon/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon
}

klog.Infof("add port request %v", podRequest)
var macAddr, ip, ipAddr, cidr, gw, subnet, ingress, egress, vlanID, ifName, nicType string
var macAddr, ip, ipAddr, cidr, gw, subnet, ingress, egress, vlanID, ifName, nicType, netns string
var pod *v1.Pod
var err error
for i := 0; i < 15; i++ {
Expand Down Expand Up @@ -108,6 +108,7 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon
ipAddr = util.GetIpAddrWithMask(ip, cidr)
ifName = podRequest.IfName
nicType = pod.Annotations[util.PodNicAnnotation]
netns = podRequest.NetNs
break
}

Expand All @@ -132,10 +133,12 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon

if strings.HasSuffix(podRequest.Provider, util.OvnProvider) && subnet != "" {
klog.Infof("create container interface %s mac %s, ip %s, cidr %s, gw %s", ifName, macAddr, ipAddr, cidr, gw)
nsArray := strings.Split(netns, "/")
podNetns := nsArray[len(nsArray)-1]
if nicType == util.InternalType {
err = csh.configureNicWithInternalPort(podRequest.PodName, podRequest.PodNamespace, podRequest.Provider, podRequest.NetNs, podRequest.ContainerID, ifName, macAddr, ipAddr, gw, ingress, egress, vlanID, podRequest.DeviceID, nicType)
err = csh.configureNicWithInternalPort(podRequest.PodName, podRequest.PodNamespace, podRequest.Provider, podRequest.NetNs, podRequest.ContainerID, ifName, macAddr, ipAddr, gw, ingress, egress, vlanID, podRequest.DeviceID, nicType, podNetns)
} else {
err = csh.configureNic(podRequest.PodName, podRequest.PodNamespace, podRequest.Provider, podRequest.NetNs, podRequest.ContainerID, ifName, macAddr, ipAddr, gw, ingress, egress, vlanID, podRequest.DeviceID, nicType)
err = csh.configureNic(podRequest.PodName, podRequest.PodNamespace, podRequest.Provider, podRequest.NetNs, podRequest.ContainerID, ifName, macAddr, ipAddr, gw, ingress, egress, vlanID, podRequest.DeviceID, nicType, podNetns)
}
if err != nil {
errMsg := fmt.Errorf("configure nic failed %v", err)
Expand Down
10 changes: 6 additions & 4 deletions pkg/daemon/ovs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/kubeovn/kube-ovn/pkg/util"
)

func (csh cniServerHandler) configureNic(podName, podNamespace, provider, netns, containerID, ifName, mac, ip, gateway, ingress, egress, vlanID, DeviceID, nicType string) error {
func (csh cniServerHandler) configureNic(podName, podNamespace, provider, netns, containerID, ifName, mac, ip, gateway, ingress, egress, vlanID, DeviceID, nicType, podNetns string) error {
var err error
var hostNicName, containerNicName string
if DeviceID == "" {
Expand All @@ -46,7 +46,8 @@ func (csh cniServerHandler) configureNic(podName, podNamespace, provider, netns,
"set", "interface", hostNicName, fmt.Sprintf("external_ids:iface-id=%s", ifaceID),
fmt.Sprintf("external_ids:pod_name=%s", podName),
fmt.Sprintf("external_ids:pod_namespace=%s", podNamespace),
fmt.Sprintf("external_ids:ip=%s", ipStr))
fmt.Sprintf("external_ids:ip=%s", ipStr),
fmt.Sprintf("external_ids:pod_netns=%s", podNetns))
if err != nil {
return fmt.Errorf("add nic to ovs failed %v: %q", err, output)
}
Expand Down Expand Up @@ -694,7 +695,7 @@ func renameLink(curName, newName string) error {
return nil
}

func (csh cniServerHandler) configureNicWithInternalPort(podName, podNamespace, provider, netns, containerID, ifName, mac, ip, gateway, ingress, egress, vlanID, DeviceID, nicType string) error {
func (csh cniServerHandler) configureNicWithInternalPort(podName, podNamespace, provider, netns, containerID, ifName, mac, ip, gateway, ingress, egress, vlanID, DeviceID, nicType, podNetns string) error {
var err error

_, containerNicName := generateNicName(containerID, ifName)
Expand All @@ -708,7 +709,8 @@ func (csh cniServerHandler) configureNicWithInternalPort(podName, podNamespace,
"set", "interface", containerNicName, fmt.Sprintf("external_ids:iface-id=%s", ifaceID),
fmt.Sprintf("external_ids:pod_name=%s", podName),
fmt.Sprintf("external_ids:pod_namespace=%s", podNamespace),
fmt.Sprintf("external_ids:ip=%s", ipStr))
fmt.Sprintf("external_ids:ip=%s", ipStr),
fmt.Sprintf("external_ids:pod_netns=%s", podNetns))
if err != nil {
return fmt.Errorf("add nic to ovs failed %v: %q", err, output)
}
Expand Down
8 changes: 2 additions & 6 deletions test/e2e/kubectl-ko/ko.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"os/exec"

"github.com/kubeovn/kube-ovn/pkg/util"
"github.com/kubeovn/kube-ovn/test/e2e/framework"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -65,11 +64,8 @@ var _ = Describe("[kubectl-ko]", func() {
pods, err := f.KubeClientSet.CoreV1().Pods("kube-system").List(context.Background(), metav1.ListOptions{LabelSelector: " app=kube-ovn-pinger"})
Expect(err).NotTo(HaveOccurred())
pod := pods.Items[0]
nicType := pod.Annotations[util.PodNicAnnotation]
if nicType != util.InternalType {
output, err := exec.Command("kubectl", "ko", "tcpdump", fmt.Sprintf("kube-system/%s", pod.Name), "-c", "1").CombinedOutput()
Expect(err).NotTo(HaveOccurred(), string(output))
}
output, err := exec.Command("kubectl", "ko", "tcpdump", fmt.Sprintf("kube-system/%s", pod.Name), "-c", "1").CombinedOutput()
Expect(err).NotTo(HaveOccurred(), string(output))
})

It("trace", func() {
Expand Down

0 comments on commit 2b2df3d

Please sign in to comment.