Skip to content

Commit

Permalink
kubectl-ko: support trace for pod with host network (#2761)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzujian committed May 6, 2023
1 parent bf1a3d7 commit 1933ed8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
25 changes: 12 additions & 13 deletions dist/images/kubectl-ko
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,23 @@ trace(){
typedName="node/$node"
lsp=$(kubectl get $typedName -o jsonpath={.metadata.annotations.ovn\\.kubernetes\\.io/port_name})
else
namespacedPod="$1"
local namespacedPod="$1"
namespace=$(echo "$namespacedPod" | cut -d "/" -f1)
podName=$(echo "$namespacedPod" | cut -d "/" -f2)
local podName=$(echo "$namespacedPod" | cut -d "/" -f2)
if [ "$podName" = "$namespacedPod" ]; then
namespace="default"
fi
typedName="pod/$podName"
optNamespace="-n $namespace"
lsp="$podName.$namespace"
node=$(kubectl get pod "$podName" -n "$namespace" -o jsonpath={.spec.nodeName})
typedName="pod/$podName"
local hostNetwork=$(kubectl get "$typedName" -n "$namespace" -o jsonpath={.spec.hostNetwork})
if [ "$hostNetwork" = "true" ]; then
namespace=""
typedName="node/$node"
lsp=$(kubectl get $typedName -o jsonpath={.metadata.annotations.ovn\\.kubernetes\\.io/port_name})
else
optNamespace="-n $namespace"
lsp="$podName.$namespace"
fi
fi

local dst="$2"
Expand All @@ -215,14 +222,6 @@ trace(){
exit 1
fi

if [ ! -z $namespace ]; then
hostNetwork=$(kubectl get "$typedName" -n "$namespace" -o jsonpath={.spec.hostNetwork})
if [ "$hostNetwork" = "true" ]; then
echo "Can not trace host network pod"
exit 1
fi
fi

local af="4" nw="nw" proto=
if [[ "$dst" =~ .*:.* ]]; then
af="6"
Expand Down
47 changes: 34 additions & 13 deletions test/e2e/kube-ovn/kubectl-ko/kubectl-ko.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,39 @@ var _ = framework.Describe("[group:kubectl-ko]", func() {
}
})

framework.ConformanceIt(`should support "kubectl ko trace <pod> <args...>" for pod with host network`, func() {
f.SkipVersionPriorTo(1, 12, "This feature was introduce in v1.12")

ginkgo.By("Creating pod " + podName + " with host network")
pod := framework.MakePod(namespaceName, podName, nil, nil, "", nil, nil)
pod.Spec.HostNetwork = true
pod = podClient.CreateSync(pod)

for _, ip := range pod.Status.PodIPs {
target, testARP := targetIPv4, true
if util.CheckProtocol(ip.IP) == apiv1.ProtocolIPv6 {
target, testARP = targetIPv6, false
}

targetMAC := util.GenerateMac()
prefix := fmt.Sprintf("ko trace %s/%s %s", pod.Namespace, pod.Name, target)
if testARP {
execOrDie(fmt.Sprintf("%s %s arp reply", prefix, targetMAC))
}

targetMACs := []string{"", targetMAC}
for _, mac := range targetMACs {
if testARP {
execOrDie(fmt.Sprintf("%s %s arp", prefix, mac))
execOrDie(fmt.Sprintf("%s %s arp request", prefix, mac))
}
execOrDie(fmt.Sprintf("%s %s icmp", prefix, mac))
execOrDie(fmt.Sprintf("%s %s tcp 80", prefix, mac))
execOrDie(fmt.Sprintf("%s %s udp 53", prefix, mac))
}
}
})

framework.ConformanceIt(`should support "kubectl ko trace <node> <args...>"`, func() {
f.SkipVersionPriorTo(1, 12, "This feature was introduce in v1.12")

Expand All @@ -174,21 +207,12 @@ var _ = framework.Describe("[group:kubectl-ko]", func() {
framework.ExpectNotEmpty(nodeList.Items)
node := nodeList.Items[rand.Intn(len(nodeList.Items))]

supportARP := !f.VersionPriorTo(1, 11)
supportDstMAC := !f.VersionPriorTo(1, 10)
if !supportARP {
framework.Logf("Support for ARP was introduce in v1.11")
}
if !supportDstMAC {
framework.Logf("Support for destination MAC was introduce in v1.10")
}

nodeIPv4, nodeIPv6 := util.GetNodeInternalIP(node)
for _, ip := range []string{nodeIPv4, nodeIPv6} {
if ip == "" {
continue
}
target, testARP := targetIPv4, supportARP
target, testARP := targetIPv4, true
if util.CheckProtocol(ip) == apiv1.ProtocolIPv6 {
target, testARP = targetIPv6, false
}
Expand All @@ -201,9 +225,6 @@ var _ = framework.Describe("[group:kubectl-ko]", func() {

targetMACs := []string{"", targetMAC}
for _, mac := range targetMACs {
if mac != "" && !supportDstMAC {
continue
}
if testARP {
execOrDie(fmt.Sprintf("%s %s arp", prefix, mac))
execOrDie(fmt.Sprintf("%s %s arp request", prefix, mac))
Expand Down

0 comments on commit 1933ed8

Please sign in to comment.