Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ronensc committed Feb 8, 2023
1 parent 90338ca commit 827678c
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 54 deletions.
6 changes: 3 additions & 3 deletions pkg/api/conntrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ type ConnTrackOperationEnum struct {
}

type ConnTrackSchedulingGroup struct {
Selector map[string]string `yaml:"selector,omitempty" doc:"key-value map to match against connection fields to apply this scheduling"`
EndConnectionTimeout Duration `yaml:"endConnectionTimeout,omitempty" doc:"duration of time to wait from the last flow log to end a connection"`
UpdateConnectionInterval Duration `yaml:"updateConnectionInterval,omitempty" doc:"duration of time to wait between update reports of a connection"`
Selector map[string]interface{} `yaml:"selector,omitempty" doc:"key-value map to match against connection fields to apply this scheduling"`
EndConnectionTimeout Duration `yaml:"endConnectionTimeout,omitempty" doc:"duration of time to wait from the last flow log to end a connection"`
UpdateConnectionInterval Duration `yaml:"updateConnectionInterval,omitempty" doc:"duration of time to wait between update reports of a connection"`
}

func ConnTrackOperationName(operation string) string {
Expand Down
10 changes: 5 additions & 5 deletions pkg/api/conntrack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func TestConnTrackValidate(t *testing.T) {
},
Scheduling: []ConnTrackSchedulingGroup{
{
Selector: map[string]string{
Selector: map[string]interface{}{
"srcIP": "value",
"undefined_key": "0",
},
Expand All @@ -174,10 +174,10 @@ func TestConnTrackValidate(t *testing.T) {
},
Scheduling: []ConnTrackSchedulingGroup{
{
Selector: map[string]string{},
Selector: map[string]interface{}{},
},
{
Selector: map[string]string{
Selector: map[string]interface{}{
"srcIP": "value",
},
},
Expand Down Expand Up @@ -206,8 +206,8 @@ func TestConnTrackValidate(t *testing.T) {
},
},
Scheduling: []ConnTrackSchedulingGroup{
{Selector: map[string]string{}},
{Selector: map[string]string{}},
{Selector: map[string]interface{}{}},
{Selector: map[string]interface{}{}},
},
},
conntrackInvalidError{defaultGroupAndNotLast: true},
Expand Down
17 changes: 2 additions & 15 deletions pkg/pipeline/extract/conntrack/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type connection interface {
// for this connection (i.e. newConnection, updateConnection, endConnection).
// It returns true on the first invocation to indicate the first report. Otherwise, it returns false.
markReported() bool
isMatchSelectorStrings(map[string]string) bool
isMatchSelector(map[string]interface{}) bool
}

type connType struct {
Expand Down Expand Up @@ -109,20 +109,7 @@ func (c *connType) markReported() bool {
return isFirst
}

func (c *connType) isMatchSelectorStrings(selector map[string]string) bool {
for k, v := range selector {
connValueRaw, found := c.keys[k]
if !found {
return false
}
if fmt.Sprintf("%v", connValueRaw) != v {
return false
}
}
return true
}

func (c *connType) isMatchSelectorGeneric(selector map[string]interface{}) bool {
func (c *connType) isMatchSelector(selector map[string]interface{}) bool {
for k, v := range selector {
connValueRaw, found := c.keys[k]
if !found {
Expand Down
28 changes: 2 additions & 26 deletions pkg/pipeline/extract/conntrack/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package conntrack

import (
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -106,36 +105,13 @@ var table = []struct {
},
}

func BenchmarkIsMatchSelectorStrings(b *testing.B) {
for _, tt := range table {
stringsSelector := map[string]string{}
for k, v := range tt.selector {
stringsSelector[k] = fmt.Sprintf("%v", v)
}
b.Run(tt.name, func(bb *testing.B) {
var r bool
bb.StartTimer()
for i := 0; i < bb.N; i++ {
r = conn.isMatchSelectorStrings(stringsSelector)
}
bb.StopTimer()
require.Equal(bb, tt.expectedResult, r)

// always store the result to a package level variable
// so the compiler cannot eliminate the Benchmark itself.
// https://dave.cheney.net/2013/06/30/how-to-write-benchmarks-in-go
result = r
})
}
}

func BenchmarkIsMatchSelectorGeneric(b *testing.B) {
for _, tt := range table {
b.Run(tt.name, func(bb *testing.B) {
var r bool
bb.StartTimer()
for i := 0; i < bb.N; i++ {
r = conn.isMatchSelectorGeneric(tt.selector)
r = conn.isMatchSelector(tt.selector)
}
bb.StopTimer()
require.Equal(bb, tt.expectedResult, r)
Expand All @@ -151,7 +127,7 @@ func BenchmarkIsMatchSelectorGeneric(b *testing.B) {
func TestIsMatchSelectorGeneric(t *testing.T) {
for _, test := range table {
t.Run(test.name, func(tt *testing.T) {
actual := conn.isMatchSelectorGeneric(test.selector)
actual := conn.isMatchSelector(test.selector)
require.Equal(tt, test.expectedResult, actual)
})
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/pipeline/extract/conntrack/conntrack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func buildMockConnTrackConfig(isBidirectional bool, outputRecordType []string,
OutputRecordTypes: outputRecordType,
Scheduling: []api.ConnTrackSchedulingGroup{
{
Selector: map[string]string{},
Selector: map[string]interface{}{},
UpdateConnectionInterval: api.Duration{Duration: updateConnectionInterval},
EndConnectionTimeout: api.Duration{Duration: endConnectionTimeout},
},
Expand Down Expand Up @@ -737,7 +737,7 @@ func TestScheduling(t *testing.T) {
conf.Extract.ConnTrack.Scheduling = append(
[]api.ConnTrackSchedulingGroup{
{
Selector: map[string]string{"Proto": "1"}, // ICMP
Selector: map[string]interface{}{"Proto": 1}, // ICMP
UpdateConnectionInterval: api.Duration{Duration: 30 * time.Second},
EndConnectionTimeout: api.Duration{Duration: 20 * time.Second},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/pipeline/extract/conntrack/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type groupType struct {

func (cs *connectionStore) getGroupIdx(conn connection) (groupIdx int) {
for i, group := range cs.groups {
if conn.isMatchSelectorStrings(group.scheduling.Selector) {
if conn.isMatchSelector(group.scheduling.Selector) {
// connection belongs to scheduling group i
return i
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/pipeline/extract/conntrack/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func TestSchedulingGroupToLabelValue(t *testing.T) {
"Non-default scheduling group",
0,
api.ConnTrackSchedulingGroup{
Selector: map[string]string{
"Proto": "1",
Selector: map[string]interface{}{
"Proto": 1,
"ip": "10.0.0.0",
},
},
Expand Down

0 comments on commit 827678c

Please sign in to comment.