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

Always include Pod labels in FlowAggregator IPFIX template #6418

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
4 changes: 2 additions & 2 deletions pkg/agent/flowexporter/exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func sendTemplateSet(t *testing.T, ctrl *gomock.Controller, mockIPFIXExpProc *ip
}
mockIPFIXExpProc.EXPECT().SendSet(mockTempSet).Return(0, nil)
_, err := flowExp.sendTemplateSet(isIPv6)
assert.NoError(t, err, "Error in sending template set")
assert.NoError(t, err, "Error when sending template set")

eL := flowExp.elementsListv4
if isIPv6 {
Expand Down Expand Up @@ -254,7 +254,7 @@ func testSendDataSet(t *testing.T, v4Enabled bool, v6Enabled bool) {
err := flowExp.addConnToSet(&conn)
assert.NoError(t, err, "Error when adding record to data set")
_, err = flowExp.sendDataSet()
assert.NoError(t, err, "Error in sending data set")
assert.NoError(t, err, "Error when sending data set")
}

if v4Enabled {
Expand Down
22 changes: 7 additions & 15 deletions pkg/flowaggregator/exporter/ipfix.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ type IPFIXExporter struct {
externalFlowCollectorProto string
exportingProcess ipfix.IPFIXExportingProcess
sendJSONRecord bool
includePodLabels bool
observationDomainID uint32
templateIDv4 uint16
templateIDv6 uint16
Expand Down Expand Up @@ -95,7 +94,6 @@ func NewIPFIXExporter(
externalFlowCollectorAddr: opt.ExternalFlowCollectorAddr,
externalFlowCollectorProto: opt.ExternalFlowCollectorProto,
sendJSONRecord: sendJSONRecord,
includePodLabels: opt.Config.RecordContents.PodLabels,
observationDomainID: observationDomainID,
registry: registry,
set: ipfixentities.NewSet(false),
Expand Down Expand Up @@ -127,7 +125,7 @@ func (e *IPFIXExporter) AddRecord(record ipfixentities.Record, isRecordIPv6 bool

func (e *IPFIXExporter) UpdateOptions(opt *options.Options) {
config := opt.Config.FlowCollector
if reflect.DeepEqual(config, e.config) && opt.Config.RecordContents.PodLabels == e.includePodLabels {
if reflect.DeepEqual(config, e.config) {
return
}

Expand All @@ -144,12 +142,8 @@ func (e *IPFIXExporter) UpdateOptions(opt *options.Options) {
} else {
e.observationDomainID = genObservationDomainID(e.k8sClient)
}
e.includePodLabels = opt.Config.RecordContents.PodLabels
klog.InfoS("New IPFIXExporter configuration", "collectorAddress", e.externalFlowCollectorAddr, "collectorProtocol", e.externalFlowCollectorProto, "sendJSON", e.sendJSONRecord, "domainID", e.observationDomainID, "includePodLabels", e.includePodLabels)
klog.InfoS("New IPFIXExporter configuration", "collectorAddress", e.externalFlowCollectorAddr, "collectorProtocol", e.externalFlowCollectorProto, "sendJSON", e.sendJSONRecord, "domainID", e.observationDomainID)

// In theory, a change to e.includePodLabels does not require opening a new connection, it
// just requires sending new templates. But it is easier to treat all configuration changes
// uniformly.
if e.exportingProcess != nil {
e.exportingProcess.CloseConnToCollector()
e.exportingProcess = nil
Expand Down Expand Up @@ -327,14 +321,12 @@ func (e *IPFIXExporter) sendTemplateSet(isIPv6 bool) (int, error) {
}
elements = append(elements, ie)
}
if e.includePodLabels {
for _, ie := range infoelements.AntreaLabelsElementList {
ie, err := e.createInfoElementForTemplateSet(ie, ipfixregistry.AntreaEnterpriseID)
if err != nil {
return 0, err
}
elements = append(elements, ie)
for _, ie := range infoelements.AntreaLabelsElementList {
ie, err := e.createInfoElementForTemplateSet(ie, ipfixregistry.AntreaEnterpriseID)
if err != nil {
return 0, err
}
elements = append(elements, ie)
}
e.set.ResetSet()
if err := e.set.PrepareSet(ipfixentities.Template, templateID); err != nil {
Expand Down
52 changes: 19 additions & 33 deletions pkg/flowaggregator/exporter/ipfix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,59 +51,41 @@ func createElement(name string, enterpriseID uint32) ipfixentities.InfoElementWi
}

func TestIPFIXExporter_sendTemplateSet(t *testing.T) {
ctrl := gomock.NewController(t)
runTest := func(t *testing.T, isIPv6 bool) {
ctrl := gomock.NewController(t)

mockIPFIXExpProc := ipfixtesting.NewMockIPFIXExportingProcess(ctrl)
mockIPFIXRegistry := ipfixtesting.NewMockIPFIXRegistry(ctrl)
mockTempSet := ipfixentitiestesting.NewMockSet(ctrl)
mockIPFIXExpProc := ipfixtesting.NewMockIPFIXExportingProcess(ctrl)
mockIPFIXRegistry := ipfixtesting.NewMockIPFIXRegistry(ctrl)
mockTempSet := ipfixentitiestesting.NewMockSet(ctrl)

newIPFIXExporter := func(includePodLabels bool) *IPFIXExporter {
return &IPFIXExporter{
exporter := &IPFIXExporter{
externalFlowCollectorAddr: "",
externalFlowCollectorProto: "",
exportingProcess: mockIPFIXExpProc,
templateIDv4: testTemplateIDv4,
templateIDv6: testTemplateIDv6,
registry: mockIPFIXRegistry,
set: mockTempSet,
includePodLabels: includePodLabels,
observationDomainID: testObservationDomainID,
}
}

testcases := []struct {
isIPv6 bool
includePodLabels bool
}{
{false, true},
{true, true},
{false, false},
{true, false},
}

for _, tc := range testcases {
exporter := newIPFIXExporter(tc.includePodLabels)
elemList := createElementList(tc.isIPv6, mockIPFIXRegistry)
elemList := createElementList(isIPv6, mockIPFIXRegistry)
testTemplateID := exporter.templateIDv4
if tc.isIPv6 {
if isIPv6 {
testTemplateID = exporter.templateIDv6
}
if tc.includePodLabels {
for _, ie := range infoelements.AntreaLabelsElementList {
elemList = append(elemList, createElement(ie, ipfixregistry.AntreaEnterpriseID))
mockIPFIXRegistry.EXPECT().GetInfoElement(ie, ipfixregistry.AntreaEnterpriseID).Return(elemList[len(elemList)-1].GetInfoElement(), nil)
}
}
mockTempSet.EXPECT().ResetSet()
mockTempSet.EXPECT().PrepareSet(ipfixentities.Template, testTemplateID).Return(nil)
mockTempSet.EXPECT().AddRecord(elemList, testTemplateID).Return(nil)
// Passing 0 for sentBytes as it is not used anywhere in the test. If this not a call to mock, the actual sentBytes
// above elements: ianaInfoElements, ianaReverseInfoElements and antreaInfoElements.
mockIPFIXExpProc.EXPECT().SendSet(mockTempSet).Return(0, nil)

_, err := exporter.sendTemplateSet(tc.isIPv6)
assert.NoErrorf(t, err, "Error in sending template record: %v, isIPv6: %v", err, tc.isIPv6)
_, err := exporter.sendTemplateSet(isIPv6)
assert.NoErrorf(t, err, "Error when sending template record")
}

t.Run("IPv4", func(t *testing.T) { runTest(t, false) })
t.Run("IPv6", func(t *testing.T) { runTest(t, true) })
}

func TestIPFIXExporter_UpdateOptions(t *testing.T) {
Expand Down Expand Up @@ -163,7 +145,7 @@ func TestIPFIXExporter_UpdateOptions(t *testing.T) {
const newAddr = "newAddr"
const newProto = "newProto"
config.FlowCollector.Address = fmt.Sprintf("%s:%s", newAddr, newProto)
config.RecordContents.PodLabels = true
config.FlowCollector.RecordFormat = "JSON"

ipfixExporter.UpdateOptions(&options.Options{
Config: config,
Expand All @@ -173,7 +155,7 @@ func TestIPFIXExporter_UpdateOptions(t *testing.T) {

assert.Equal(t, newAddr, ipfixExporter.externalFlowCollectorAddr)
assert.Equal(t, newProto, ipfixExporter.externalFlowCollectorProto)
assert.True(t, ipfixExporter.includePodLabels)
assert.True(t, ipfixExporter.sendJSONRecord)

require.NoError(t, ipfixExporter.AddRecord(mockRecord, false))
assert.Equal(t, 2, setCount, "Invalid number of flow sets sent by exporter")
Expand Down Expand Up @@ -305,6 +287,10 @@ func createElementList(isIPv6 bool, mockIPFIXRegistry *ipfixtesting.MockIPFIXReg
elemList = append(elemList, createElement(infoelements.AntreaDestinationThroughputElementList[i], ipfixregistry.AntreaEnterpriseID))
mockIPFIXRegistry.EXPECT().GetInfoElement(infoelements.AntreaDestinationThroughputElementList[i], ipfixregistry.AntreaEnterpriseID).Return(elemList[len(elemList)-1].GetInfoElement(), nil)
}
for _, ie := range infoelements.AntreaLabelsElementList {
elemList = append(elemList, createElement(ie, ipfixregistry.AntreaEnterpriseID))
mockIPFIXRegistry.EXPECT().GetInfoElement(ie, ipfixregistry.AntreaEnterpriseID).Return(elemList[len(elemList)-1].GetInfoElement(), nil)
}
return elemList
}

Expand Down
36 changes: 22 additions & 14 deletions pkg/flowaggregator/flowaggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,8 @@ func (fa *flowAggregator) sendFlowKeyRecord(key ipfixintermediate.FlowKey, recor
fa.fillK8sMetadata(key, record.Record, *startTime)
fa.aggregationProcess.SetCorrelatedFieldsFilled(record, true)
}
if fa.includePodLabels && !fa.aggregationProcess.AreExternalFieldsFilled(*record) {
// Even if fa.includePodLabels is false, we still need to add an empty IE to match the template.
if !fa.aggregationProcess.AreExternalFieldsFilled(*record) {
fa.fillPodLabels(key, record.Record, *startTime)
fa.aggregationProcess.SetExternalFieldsFilled(record, true)
}
Expand Down Expand Up @@ -502,7 +503,11 @@ func (fa *flowAggregator) fetchPodLabels(ip string, startTime time.Time) string
klog.ErrorS(nil, "Error when getting Pod information from podInformer", "ip", ip, "startTime", startTime)
return ""
}
labelsJSON, err := json.Marshal(pod.GetLabels())
labels := pod.GetLabels()
if labels == nil {
labels = map[string]string{}
}
labelsJSON, err := json.Marshal(labels)
if err != nil {
klog.ErrorS(err, "Error when JSON encoding of Pod labels")
return ""
Expand All @@ -512,22 +517,25 @@ func (fa *flowAggregator) fetchPodLabels(ip string, startTime time.Time) string

func (fa *flowAggregator) fillPodLabelsForSide(ip string, record ipfixentities.Record, startTime time.Time, podNamespaceIEName, podNameIEName, podLabelsIEName string) error {
podLabelsString := ""
if podName, _, ok := record.GetInfoElementWithValue(podNameIEName); ok {
podNameString := podName.GetStringValue()
if podNamespace, _, ok := record.GetInfoElementWithValue(podNamespaceIEName); ok {
podNamespaceString := podNamespace.GetStringValue()
if podNameString != "" && podNamespaceString != "" {
podLabelsString = fa.fetchPodLabels(ip, startTime)
// If fa.includePodLabels is false, we always use an empty string.
// If fa.includePodLabels is true, we use an empty string in case of error or if the
// endpoint is not a Pod, and a valid JSON dictionary otherwise (which will be empty if the
// Pod has no labels).
if fa.includePodLabels {
if podName, _, ok := record.GetInfoElementWithValue(podNameIEName); ok {
podNameString := podName.GetStringValue()
if podNamespace, _, ok := record.GetInfoElementWithValue(podNamespaceIEName); ok {
podNamespaceString := podNamespace.GetStringValue()
if podNameString != "" && podNamespaceString != "" {
podLabelsString = fa.fetchPodLabels(ip, startTime)
}
}
}
}

podLabelsElement, err := fa.registry.GetInfoElement(podLabelsIEName, ipfixregistry.AntreaEnterpriseID)
if err == nil {
podLabelsIE, err := ipfixentities.DecodeAndCreateInfoElementWithValue(podLabelsElement, bytes.NewBufferString(podLabelsString).Bytes())
if err != nil {
return fmt.Errorf("error when creating podLabels InfoElementWithValue: %v", err)
}
podLabelsIE := ipfixentities.NewStringInfoElement(podLabelsElement, podLabelsString)
if err := record.AddInfoElement(podLabelsIE); err != nil {
return fmt.Errorf("error when adding podLabels InfoElementWithValue: %v", err)
}
Expand All @@ -540,10 +548,10 @@ func (fa *flowAggregator) fillPodLabelsForSide(ip string, record ipfixentities.R

func (fa *flowAggregator) fillPodLabels(key ipfixintermediate.FlowKey, record ipfixentities.Record, startTime time.Time) {
if err := fa.fillPodLabelsForSide(key.SourceAddress, record, startTime, "sourcePodNamespace", "sourcePodName", "sourcePodLabels"); err != nil {
klog.ErrorS(err, "Error when filling pod labels", "side", "source")
klog.ErrorS(err, "Error when filling Pod labels", "side", "source")
}
if err := fa.fillPodLabelsForSide(key.DestinationAddress, record, startTime, "destinationPodNamespace", "destinationPodName", "destinationPodLabels"); err != nil {
klog.ErrorS(err, "Error when filling pod labels", "side", "destination")
klog.ErrorS(err, "Error when filling Pod labels", "side", "destination")
}
}

Expand Down
Loading
Loading