Skip to content

Commit

Permalink
fix unit tests and api updates
Browse files Browse the repository at this point in the history
Signed-off-by: Hang Yan <yhang@vmware.com>
  • Loading branch information
hangyan committed Sep 14, 2024
1 parent 79797cd commit 13d3d42
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 76 deletions.
70 changes: 30 additions & 40 deletions pkg/agent/controller/packetcapture/packetcapture_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,15 +393,15 @@ func (c *Controller) startPacketCapture(pc *crdv1alpha1.PacketCapture, pcState *
return err
}

// genEndpointMatchPackets generates match packets (with destination Endpoint's IP/port info) besides the normal match packet.
// genEndpointMatchPackets generates match packets (with destination Endpoint's IP/Port info) besides the normal match packet.
// these match packets will help the pipeline to capture the pod -> svc traffic.
// TODO: 1. support name based port name 2. dual-stack support
func (c *Controller) genEndpointMatchPackets(pc *crdv1alpha1.PacketCapture) ([]binding.Packet, error) {
var port int32
if pc.Spec.Packet.TransportHeader.TCP != nil {
port = pc.Spec.Packet.TransportHeader.TCP.DstPort
} else if pc.Spec.Packet.TransportHeader.UDP != nil {
port = pc.Spec.Packet.TransportHeader.UDP.DstPort
if pc.Spec.Packet.TransportHeader.TCP != nil && pc.Spec.Packet.TransportHeader.TCP.DstPort != nil {
port = *pc.Spec.Packet.TransportHeader.TCP.DstPort
} else if pc.Spec.Packet.TransportHeader.UDP != nil && pc.Spec.Packet.TransportHeader.UDP.DstPort != nil {
port = *pc.Spec.Packet.TransportHeader.UDP.DstPort
}
var packets []binding.Packet
dstSvc, err := c.serviceLister.Services(pc.Spec.Destination.Namespace).Get(pc.Spec.Destination.Service)
Expand All @@ -425,7 +425,7 @@ func (c *Controller) genEndpointMatchPackets(pc *crdv1alpha1.PacketCapture) ([]b
if port != 0 {
packet.DestinationPort = uint16(port)
}
packet.IPProto, _ = parseTargetProto(pc.Spec.Packet)
packet.IPProto = parseTargetProto(pc.Spec.Packet)
packets = append(packets, packet)
}
return packets, nil
Expand Down Expand Up @@ -497,55 +497,45 @@ func (c *Controller) preparePacket(pc *crdv1alpha1.PacketCapture, intf *interfac
}

if pc.Spec.Packet.TransportHeader.TCP != nil {
packet.SourcePort = uint16(pc.Spec.Packet.TransportHeader.TCP.SrcPort)
packet.DestinationPort = uint16(pc.Spec.Packet.TransportHeader.TCP.DstPort)
if pc.Spec.Packet.TransportHeader.TCP.SrcPort != nil {
packet.SourcePort = uint16(*pc.Spec.Packet.TransportHeader.TCP.SrcPort)
}
if pc.Spec.Packet.TransportHeader.TCP.DstPort != nil {
packet.DestinationPort = uint16(*pc.Spec.Packet.TransportHeader.TCP.DstPort)
}
if pc.Spec.Packet.TransportHeader.TCP.Flags != nil {
packet.TCPFlags = uint8(*pc.Spec.Packet.TransportHeader.TCP.Flags)
}
} else if pc.Spec.Packet.TransportHeader.UDP != nil {
packet.SourcePort = uint16(pc.Spec.Packet.TransportHeader.UDP.SrcPort)
packet.DestinationPort = uint16(pc.Spec.Packet.TransportHeader.UDP.DstPort)
}

proto, err := parseTargetProto(pc.Spec.Packet)
if err != nil {
return nil, err
if pc.Spec.Packet.TransportHeader.UDP.SrcPort != nil {
packet.SourcePort = uint16(*pc.Spec.Packet.TransportHeader.UDP.SrcPort)
}
if pc.Spec.Packet.TransportHeader.UDP.DstPort != nil {
packet.DestinationPort = uint16(*pc.Spec.Packet.TransportHeader.UDP.DstPort)
}
}
packet.IPProto = proto
packet.IPProto = parseTargetProto(pc.Spec.Packet)
return packet, nil
}

func parseTargetProto(packet *crdv1alpha1.Packet) (uint8, error) {
var ipProto uint8
isIPv6 := packet.IPFamily == v1.IPv6Protocol

if packet.TransportHeader.TCP != nil {
ipProto = protocol.Type_TCP
} else if packet.TransportHeader.UDP != nil {
ipProto = protocol.Type_UDP
} else {
ipProto = protocol.Type_ICMP
if isIPv6 {
ipProto = protocol.Type_IPv6ICMP
}
func parseTargetProto(packet *crdv1alpha1.Packet) uint8 {
inputProto := packet.Protocol
if inputProto == nil {
return protocol.Type_ICMP
}
if inputProto.Type == intstr.Int {
return uint8(inputProto.IntVal)
}

inputProto := packet.Protocol
if inputProto.StrVal == "TCP" {
inputProto = &intstr.IntOrString{Type: intstr.Int, IntVal: protocol.Type_TCP}
return protocol.Type_TCP
} else if inputProto.StrVal == "ICMP" {
inputProto = &intstr.IntOrString{Type: intstr.Int, IntVal: protocol.Type_ICMP}
return protocol.Type_ICMP
} else if inputProto.StrVal == "UDP" {
inputProto = &intstr.IntOrString{Type: intstr.Int, IntVal: protocol.Type_UDP}
return protocol.Type_UDP
} else {
inputProto = &intstr.IntOrString{Type: intstr.Int, IntVal: protocol.Type_IPv6ICMP}
}

if inputProto.IntVal != int32(ipProto) {
return 0, errors.New("Unmatch protocol settings in Packet between transportHeader and protocol")
return protocol.Type_IPv6ICMP
}

return ipProto, nil
}

func (c *Controller) syncPacketCapture(pcName string) error {
Expand Down
70 changes: 47 additions & 23 deletions pkg/agent/controller/packetcapture/packetcapture_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,20 @@ import (
)

var (
pod1IPv4 = "192.168.10.10"
pod2IPv4 = "192.168.11.10"
service1IPv4 = "10.96.0.10"
dstIPv4 = "192.168.99.99"
pod1MAC, _ = net.ParseMAC("aa:bb:cc:dd:ee:0f")
pod2MAC, _ = net.ParseMAC("aa:bb:cc:dd:ee:00")
ofPortPod1 = uint32(1)
ofPortPod2 = uint32(2)
testTCPFlags = int32(11)
icmp6Proto = intstr.FromInt(58)
pod1IPv4 = "192.168.10.10"
pod2IPv4 = "192.168.11.10"
service1IPv4 = "10.96.0.10"
dstIPv4 = "192.168.99.99"
pod1MAC, _ = net.ParseMAC("aa:bb:cc:dd:ee:0f")
pod2MAC, _ = net.ParseMAC("aa:bb:cc:dd:ee:00")
ofPortPod1 = uint32(1)
ofPortPod2 = uint32(2)
testTCPFlags = int32(11)
icmp6Proto = intstr.FromInt(58)
tcpProto = intstr.FromString("TCP")

Check failure on line 64 in pkg/agent/controller/packetcapture/packetcapture_controller_test.go

View workflow job for this annotation

GitHub Actions / Golangci-lint (macos-latest)

var `tcpProto` is unused (unused)

Check failure on line 64 in pkg/agent/controller/packetcapture/packetcapture_controller_test.go

View workflow job for this annotation

GitHub Actions / Golangci-lint (ubuntu-latest)

var `tcpProto` is unused (unused)
icmpProto = intstr.FromString("ICMP")
port80 int32 = 80
port81 int32 = 81

pod1 = v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -249,10 +253,11 @@ func TestPreparePacket(t *testing.T) {
Pod: pod2.Name,
},
Packet: &crdv1alpha1.Packet{
Protocol: &intstr.IntOrString{Type: intstr.String, StrVal: "TCP"},
TransportHeader: crdv1alpha1.TransportHeader{
TCP: &crdv1alpha1.TCPHeader{
SrcPort: 80,
DstPort: 81,
SrcPort: &port80,
DstPort: &port81,
Flags: &testTCPFlags,
},
},
Expand All @@ -279,6 +284,10 @@ func TestPreparePacket(t *testing.T) {
Namespace: pod1.Namespace,
Pod: pod1.Name,
},
Packet: &crdv1alpha1.Packet{
IPFamily: v1.IPv4Protocol,
Protocol: &intstr.IntOrString{Type: intstr.String, StrVal: "ICMP"},
},
},
},
receiverOnly: true,
Expand All @@ -303,6 +312,7 @@ func TestPreparePacket(t *testing.T) {
},
Packet: &crdv1alpha1.Packet{
IPFamily: v1.IPv6Protocol,
Protocol: &icmp6Proto,
},
},
},
Expand Down Expand Up @@ -346,10 +356,11 @@ func TestPreparePacket(t *testing.T) {
Pod: pod2.Name,
},
Packet: &crdv1alpha1.Packet{
Protocol: &intstr.IntOrString{Type: intstr.String, StrVal: "TCP"},
TransportHeader: crdv1alpha1.TransportHeader{
TCP: &crdv1alpha1.TCPHeader{
SrcPort: 80,
DstPort: 81,
SrcPort: &port80,
DstPort: &port81,
},
},
},
Expand All @@ -376,10 +387,11 @@ func TestPreparePacket(t *testing.T) {
Pod: pod2.Name,
},
Packet: &crdv1alpha1.Packet{
Protocol: &intstr.IntOrString{Type: intstr.String, StrVal: "UDP"},
TransportHeader: crdv1alpha1.TransportHeader{
UDP: &crdv1alpha1.UDPHeader{
SrcPort: 80,
DstPort: 100,
SrcPort: &port80,
DstPort: &port81,
},
},
},
Expand All @@ -389,7 +401,7 @@ func TestPreparePacket(t *testing.T) {
DestinationIP: net.ParseIP(pod2IPv4),
IPProto: protocol.Type_UDP,
SourcePort: 80,
DestinationPort: 100,
DestinationPort: 81,
},
},
{
Expand All @@ -405,7 +417,9 @@ func TestPreparePacket(t *testing.T) {
Namespace: pod2.Namespace,
Pod: pod2.Name,
},
Packet: &crdv1alpha1.Packet{},
Packet: &crdv1alpha1.Packet{
Protocol: &intstr.IntOrString{Type: intstr.String, StrVal: "ICMP"},
},
},
},
expectedPacket: &binding.Packet{
Expand Down Expand Up @@ -440,10 +454,11 @@ func TestPreparePacket(t *testing.T) {
Namespace: service1.Namespace,
},
Packet: &crdv1alpha1.Packet{
Protocol: &intstr.IntOrString{Type: intstr.String, StrVal: "TCP"},
TransportHeader: crdv1alpha1.TransportHeader{
TCP: &crdv1alpha1.TCPHeader{
SrcPort: 80,
DstPort: 81,
SrcPort: &port80,
DstPort: &port81,
Flags: &testTCPFlags,
},
},
Expand Down Expand Up @@ -617,6 +632,9 @@ func TestPacketCaptureControllerRun(t *testing.T) {
Number: 5,
},
},
Packet: &crdv1alpha1.Packet{
Protocol: &icmpProto,
},
},
},
newState: &packetCaptureState{tag: 1},
Expand Down Expand Up @@ -663,6 +681,9 @@ func TestProcessPacketCaptureItem(t *testing.T) {
Number: 5,
},
},
Packet: &crdv1alpha1.Packet{
Protocol: &icmpProto,
},
},
},
ofPort: ofPortPod1,
Expand Down Expand Up @@ -836,9 +857,10 @@ func TestPrepareEndpointsPackets(t *testing.T) {
Service: "svc1",
},
Packet: &crdv1alpha1.Packet{
Protocol: &intstr.IntOrString{Type: intstr.String, StrVal: "TCP"},
TransportHeader: crdv1alpha1.TransportHeader{
TCP: &crdv1alpha1.TCPHeader{
DstPort: 80,
DstPort: &port80,
},
},
},
Expand Down Expand Up @@ -878,9 +900,10 @@ func TestPrepareEndpointsPackets(t *testing.T) {
Service: "svc1",
},
Packet: &crdv1alpha1.Packet{
Protocol: &intstr.IntOrString{Type: intstr.String, StrVal: "TCP"},
TransportHeader: crdv1alpha1.TransportHeader{
TCP: &crdv1alpha1.TCPHeader{
DstPort: 80,
DstPort: &port80,
},
},
},
Expand Down Expand Up @@ -954,9 +977,10 @@ func TestPrepareEndpointsPackets(t *testing.T) {
Service: "svc1",
},
Packet: &crdv1alpha1.Packet{
Protocol: &intstr.IntOrString{Type: intstr.String, StrVal: "TCP"},
TransportHeader: crdv1alpha1.TransportHeader{
TCP: &crdv1alpha1.TCPHeader{
DstPort: 80,
DstPort: &port80,
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/agent/controller/packetcapture/packetin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func TestHandlePacketCapturePacketIn(t *testing.T) {
// check target num in status
pc, err := pcc.crdClient.CrdV1alpha1().PacketCaptures().Get(context.TODO(), tt.expectedPC.Name, metav1.GetOptions{})
require.Nil(t, err)
assert.Equal(t, tt.expectedNum, pc.Status.NumCapturedPackets)
assert.Equal(t, tt.expectedNum, *pc.Status.NumCapturedPackets)
} else {
assert.Equal(t, tt.expectedErrStr, err.Error())
}
Expand Down
7 changes: 0 additions & 7 deletions pkg/apis/crd/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/features/antrea_features.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const (
// Allows to trace path from a generated packet.
Traceflow featuregate.Feature = "Traceflow"

// alpha: v2.0
// alpha: v2.2
// Allows to capture packets for a flow.
PacketCapture featuregate.Feature = "PacketCapture"

Expand Down
10 changes: 6 additions & 4 deletions test/e2e/packetcapture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ var (
icmpProto = intstr.FromInt(1)
udpProto = intstr.FromInt(17)
icmp6Proto = intstr.FromInt(58)

testServerPort int32 = 80
)

type pcTestCase struct {
Expand Down Expand Up @@ -248,7 +250,7 @@ func testPacketCapture(t *testing.T, data *TestData) {
Protocol: &tcpProto,
TransportHeader: crdv1alpha1.TransportHeader{
TCP: &crdv1alpha1.TCPHeader{
DstPort: serverPodPort,
DstPort: &testServerPort,
},
},
},
Expand Down Expand Up @@ -287,7 +289,7 @@ func testPacketCapture(t *testing.T, data *TestData) {
Protocol: &tcpProto,
TransportHeader: crdv1alpha1.TransportHeader{
TCP: &crdv1alpha1.TCPHeader{
DstPort: serverPodPort,
DstPort: &testServerPort,
},
},
},
Expand Down Expand Up @@ -365,7 +367,7 @@ func testPacketCaptureBasic(t *testing.T, data *TestData) {
Protocol: &tcpProto,
TransportHeader: crdv1alpha1.TransportHeader{
TCP: &crdv1alpha1.TCPHeader{
DstPort: serverPodPort,
DstPort: &testServerPort,
},
},
},
Expand Down Expand Up @@ -403,7 +405,7 @@ func testPacketCaptureBasic(t *testing.T, data *TestData) {
Protocol: &udpProto,
TransportHeader: crdv1alpha1.TransportHeader{
UDP: &crdv1alpha1.UDPHeader{
DstPort: serverPodPort,
DstPort: &testServerPort,
},
},
},
Expand Down

0 comments on commit 13d3d42

Please sign in to comment.