Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use default protocol/port when destination is service in antrea traceflow #6601

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion pkg/agent/controller/traceflow/traceflow_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"time"

"antrea.io/libOpenflow/protocol"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -491,6 +492,16 @@ func (c *Controller) preparePacket(tf *crdv1beta1.Traceflow, intf *interfacestor
} else if packet.DestinationIP.To4() != nil {
return nil, errors.New("destination Service does not have an IPv6 ClusterIP")
}
if !liveTraffic {
switch dstSvc.Spec.Ports[0].Protocol {
case corev1.ProtocolTCP:
packet.IPProto = protocol.Type_TCP
packet.TCPFlags = uint8(2)
case corev1.ProtocolUDP:
packet.IPProto = protocol.Type_UDP
Comment on lines +497 to +501
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The switch statement does not include a default case. While the current implementation handles TCP and UDP, if the service protocol is SCTP which is also currently not supported , we could add a default case !
default:
// Handle unsupported protocols, maybe log a warning

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defaults to ICMP which is consistent with other cases also.
In this case, the behaviour will be run traceflow as Pod-to-IP(Service's ClusterIP) with ICMP protocol.

}
packet.DestinationPort = uint16(dstSvc.Spec.Ports[0].Port)
}
} else if !liveTraffic {
return nil, errors.New("destination is not specified")
}
Expand All @@ -507,7 +518,9 @@ func (c *Controller) preparePacket(tf *crdv1beta1.Traceflow, intf *interfacestor
packet.IPFlags = 0
}
} else if tf.Spec.Packet.IPHeader != nil {
packet.IPProto = uint8(tf.Spec.Packet.IPHeader.Protocol)
if tf.Spec.Packet.IPHeader.Protocol > 0 {
packet.IPProto = uint8(tf.Spec.Packet.IPHeader.Protocol)
}
if !liveTraffic {
packet.TTL = uint8(tf.Spec.Packet.IPHeader.TTL)
packet.IPFlags = uint16(tf.Spec.Packet.IPHeader.Flags)
Expand All @@ -534,10 +547,12 @@ func (c *Controller) preparePacket(tf *crdv1beta1.Traceflow, intf *interfacestor
}
} else if tf.Spec.Packet.TransportHeader.UDP != nil {
packet.IPProto = protocol.Type_UDP
packet.TCPFlags = uint8(0)
Atish-iaf marked this conversation as resolved.
Show resolved Hide resolved
packet.SourcePort = uint16(tf.Spec.Packet.TransportHeader.UDP.SrcPort)
packet.DestinationPort = uint16(tf.Spec.Packet.TransportHeader.UDP.DstPort)
} else if tf.Spec.Packet.TransportHeader.ICMP != nil {
isICMP = true
packet.TCPFlags = uint8(0)
if !liveTraffic {
packet.ICMPEchoID = uint16(tf.Spec.Packet.TransportHeader.ICMP.ID)
packet.ICMPEchoSeq = uint16(tf.Spec.Packet.TransportHeader.ICMP.Sequence)
Expand Down
93 changes: 92 additions & 1 deletion pkg/agent/controller/traceflow/traceflow_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/util/workqueue"
Expand All @@ -48,6 +49,7 @@ import (
var (
pod1IPv4 = "192.168.10.10"
pod2IPv4 = "192.168.11.10"
svc1IPv4 = "10.96.0.1"
dstIPv4 = "192.168.99.99"
pod1MAC, _ = net.ParseMAC("aa:bb:cc:dd:ee:0f")
pod2MAC, _ = net.ParseMAC("aa:bb:cc:dd:ee:00")
Expand Down Expand Up @@ -79,6 +81,23 @@ var (
Namespace: "default",
},
}

svc1 = v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-1",
Namespace: "default",
},
Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{
{
Port: 80,
Protocol: v1.ProtocolTCP,
},
},
Type: v1.ServiceTypeClusterIP,
ClusterIP: svc1IPv4,
},
}
)

type fakeTraceflowController struct {
Expand All @@ -88,13 +107,16 @@ type fakeTraceflowController struct {
mockOFClient *openflowtest.MockClient
crdClient *fakeversioned.Clientset
crdInformerFactory crdinformers.SharedInformerFactory
informerFactory informers.SharedInformerFactory
networkPolicyQuerier *queriertest.MockAgentNetworkPolicyInfoQuerier
egressQuerier *queriertest.MockEgressQuerier
}

func newFakeTraceflowController(t *testing.T, initObjects []runtime.Object, networkConfig *config.NetworkConfig, nodeConfig *config.NodeConfig) *fakeTraceflowController {
controller := gomock.NewController(t)
kubeClient := fake.NewSimpleClientset(&pod1, &pod2, &pod3)
kubeClient := fake.NewSimpleClientset(&pod1, &pod2, &pod3, &svc1)
informerFactory := informers.NewSharedInformerFactory(kubeClient, 0)
serviceInformer := informerFactory.Core().V1().Services()
mockOFClient := openflowtest.NewMockClient(controller)
crdClient := fakeversioned.NewSimpleClientset(initObjects...)
crdInformerFactory := crdinformers.NewSharedInformerFactory(crdClient, 0)
Expand All @@ -110,6 +132,8 @@ func newFakeTraceflowController(t *testing.T, initObjects []runtime.Object, netw

tfController := &Controller{
kubeClient: kubeClient,
serviceLister: serviceInformer.Lister(),
serviceListerSynced: serviceInformer.Informer().HasSynced,
crdClient: crdClient,
traceflowInformer: traceflowInformer,
traceflowLister: traceflowInformer.Lister(),
Expand All @@ -132,6 +156,7 @@ func newFakeTraceflowController(t *testing.T, initObjects []runtime.Object, netw
mockOFClient: mockOFClient,
crdClient: crdClient,
crdInformerFactory: crdInformerFactory,
informerFactory: informerFactory,
networkPolicyQuerier: npQuerier,
egressQuerier: egressQuerier,
}
Expand Down Expand Up @@ -473,11 +498,77 @@ func TestPreparePacket(t *testing.T) {
IPProto: protocol.Type_IPv6ICMP,
},
},
{
name: "Pod-to-service packet without port and protocol",
tf: &crdv1beta1.Traceflow{
ObjectMeta: metav1.ObjectMeta{Name: "tf7", UID: "uid7"},
Spec: crdv1beta1.TraceflowSpec{
Source: crdv1beta1.Source{
Namespace: pod1.Namespace,
Pod: pod1.Name,
},
Destination: crdv1beta1.Destination{
Namespace: svc1.Namespace,
Service: svc1.Name,
},
Packet: crdv1beta1.Packet{
IPHeader: &crdv1beta1.IPHeader{},
},
},
},
expectedPacket: &binding.Packet{
SourceIP: net.ParseIP(pod1IPv4),
SourceMAC: pod1MAC,
DestinationIP: net.ParseIP(svc1IPv4).To4(),
DestinationPort: 80,
IPProto: protocol.Type_TCP,
TTL: 64,
TCPFlags: uint8(2),
},
},
{
name: "Pod-to-service packet with port and protocol",
tf: &crdv1beta1.Traceflow{
ObjectMeta: metav1.ObjectMeta{Name: "tf8", UID: "uid8"},
Spec: crdv1beta1.TraceflowSpec{
Source: crdv1beta1.Source{
Namespace: pod1.Namespace,
Pod: pod1.Name,
},
Destination: crdv1beta1.Destination{
Namespace: svc1.Namespace,
Service: svc1.Name,
},
Packet: crdv1beta1.Packet{
IPHeader: &crdv1beta1.IPHeader{
Protocol: 17,
},
TransportHeader: crdv1beta1.TransportHeader{
UDP: &crdv1beta1.UDPHeader{
DstPort: 8080,
},
},
},
},
},
expectedPacket: &binding.Packet{
SourceIP: net.ParseIP(pod1IPv4),
SourceMAC: pod1MAC,
DestinationIP: net.ParseIP(svc1IPv4).To4(),
DestinationPort: 8080,
IPProto: protocol.Type_UDP,
TTL: 64,
},
},
}

for _, tt := range tcs {
t.Run(tt.name, func(t *testing.T) {
tfc := newFakeTraceflowController(t, []runtime.Object{tt.tf}, nil, nil)
stopCh := make(chan struct{})
defer close(stopCh)
tfc.informerFactory.Start(stopCh)
tfc.informerFactory.WaitForCacheSync(stopCh)
podInterfaces := tfc.interfaceStore.GetContainerInterfacesByPod(pod1.Name, pod1.Namespace)
if tt.intf != nil {
podInterfaces[0] = tt.intf
Expand Down