Skip to content

Commit e9b1d44

Browse files
author
Mario Macias
authored
Allow configuration from env (#15)
1 parent 9c2d07b commit e9b1d44

File tree

248 files changed

+49004
-8053
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

248 files changed

+49004
-8053
lines changed

cmd/netobserv-ebpf-agent.go

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,32 @@ package main
44

55
import (
66
"context"
7-
"flag"
7+
"fmt"
88
"os"
99
"os/signal"
1010
"syscall"
11-
"time"
1211

13-
"github.com/netobserv/netobserv-ebpf-agent/pkg/agent"
12+
"github.com/caarlos0/env/v6"
1413
"github.com/sirupsen/logrus"
15-
)
1614

17-
// TODO: make configurable. NETOBSERV-201
18-
const (
19-
maxStoredFlowEntries = 1000
20-
maxFlowEvictionPeriod = 5 * time.Second
21-
communicationBufferLength = 20
15+
"github.com/netobserv/netobserv-ebpf-agent/pkg/agent"
2216
)
2317

2418
func main() {
25-
flag.Parse()
26-
27-
logrus.SetLevel(logrus.DebugLevel)
28-
// temporary hack until NETOBSERV-201
29-
flowsTargetHost := os.Getenv("FLOWS_TARGET_HOST")
30-
if flowsTargetHost == "" {
31-
panic("expecting a collector target host in the FLOWS_TARGET_HOST env var")
19+
logrus.Infof("starting NetObserv eBPF Agent")
20+
config := agent.Config{}
21+
if err := env.Parse(&config); err != nil {
22+
logrus.WithError(err).Fatal("can't load configuration from environment")
3223
}
33-
flowsTargetPort := os.Getenv("FLOWS_TARGET_PORT")
34-
if flowsTargetPort == "" {
35-
panic("expecting a collector target port in the FLOWS_TARGET_PORT env var")
24+
if config.Verbose {
25+
logrus.SetLevel(logrus.DebugLevel)
3626
}
37-
logrus.WithFields(logrus.Fields{
38-
"FLOWS_TARGET_HOST": flowsTargetHost,
39-
"FLOWS_TARGET_PORT": flowsTargetPort,
40-
}).Infof("Starting eBFP flows' agent")
4127

42-
flowsTarget := flowsTargetHost + ":" + flowsTargetPort
28+
logrus.WithField("configuration", fmt.Sprintf("%#v", config)).Debugf("configuration loaded")
4329

44-
flowsAgent, err := agent.FlowsAgent(&agent.Config{
45-
FlowsTarget: flowsTarget,
46-
ExcludeIfaces: []string{"lo"},
47-
BuffersLen: communicationBufferLength,
48-
CacheMaxFlows: maxStoredFlowEntries,
49-
CacheActiveTimeout: maxFlowEvictionPeriod,
50-
})
30+
flowsAgent, err := agent.FlowsAgent(&config)
5131
if err != nil {
52-
logrus.WithError(err).Fatal("can't instantiate netobserv-ebpf-agent")
32+
logrus.WithError(err).Fatal("can't instantiate NetObserv eBPF Agent")
5333
}
5434

5535
logrus.Infof("push CTRL+C or send SIGTERM to interrupt execution")

go.mod

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/netobserv/netobserv-ebpf-agent
33
go 1.17
44

55
require (
6+
github.com/caarlos0/env/v6 v6.9.1
67
github.com/cilium/ebpf v0.8.1
78
github.com/mariomac/guara v0.0.0-20220321135847-54b7fb6a8657
89
github.com/netobserv/gopipes v0.1.1
@@ -11,17 +12,20 @@ require (
1112
github.com/stretchr/testify v1.7.1
1213
github.com/vishvananda/netlink v1.1.0
1314
google.golang.org/grpc v1.45.0
14-
google.golang.org/protobuf v1.27.1
15+
google.golang.org/protobuf v1.28.0
1516
)
1617

1718
require (
1819
github.com/davecgh/go-spew v1.1.1 // indirect
1920
github.com/golang/protobuf v1.5.2 // indirect
21+
github.com/google/go-cmp v0.5.7 // indirect
2022
github.com/pmezard/go-difflib v1.0.0 // indirect
2123
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df // indirect
22-
golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect
23-
golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34 // indirect
24-
golang.org/x/text v0.3.0 // indirect
25-
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
26-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
24+
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 // indirect
25+
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
26+
golang.org/x/text v0.3.7 // indirect
27+
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
28+
google.golang.org/genproto v0.0.0-20220407144326-9054f6ed7bac // indirect
29+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
30+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
2731
)

go.sum

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
22
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
33
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
44
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
5+
github.com/caarlos0/env/v6 v6.9.1 h1:zOkkjM0F6ltnQ5eBX6IPI41UP/KDGEK7rRPwGCNos8k=
6+
github.com/caarlos0/env/v6 v6.9.1/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc=
57
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
68
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
79
github.com/cilium/ebpf v0.8.1 h1:bLSSEbBLqGPXxls55pGr5qWZaTqcmfDJHhou7t254ao=
@@ -48,8 +50,9 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
4850
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
4951
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
5052
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
51-
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
5253
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
54+
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
55+
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
5356
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
5457
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
5558
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
@@ -96,8 +99,10 @@ golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73r
9699
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
97100
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
98101
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
99-
golang.org/x/net v0.0.0-20200822124328-c89045814202 h1:VvcQYSHwXgi7W+TpUR6A9g6Up98WAHf3f/ulnJ62IyA=
100102
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
103+
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
104+
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 h1:bRb386wvrE+oBNdF1d/Xh9mQrfQ4ecYhW5qJ5GvTGT4=
105+
golang.org/x/net v0.0.0-20220412020605-290c469a71a5/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
101106
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
102107
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
103108
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -110,24 +115,38 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
110115
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
111116
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
112117
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
113-
golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34 h1:GkvMjFtXUmahfDtashnc1mnrCtuBVcwse5QV2lUk/tI=
118+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
119+
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
120+
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
121+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
114122
golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
115-
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
123+
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
124+
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0=
125+
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
126+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
127+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
116128
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
129+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
130+
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
131+
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
132+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
133+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
117134
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
118135
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
119136
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
120137
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
121138
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
122-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
123139
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
140+
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f h1:GGU+dLjvlC3qDwqYgL6UgRmHXhOOgns0bZu2Ty5mm6U=
141+
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
124142
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
125143
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
126144
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
127145
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
128146
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
129-
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY=
130147
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
148+
google.golang.org/genproto v0.0.0-20220407144326-9054f6ed7bac h1:qSNTkEN+L2mvWcLgJOR+8bdHX9rN/IdU3A1Ghpfb1Rg=
149+
google.golang.org/genproto v0.0.0-20220407144326-9054f6ed7bac/go.mod h1:8w6bsBMX6yCPbAVTeqQHvzxW0EIFigd5lZyahWgyfDo=
131150
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
132151
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
133152
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
@@ -147,15 +166,17 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
147166
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
148167
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
149168
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
150-
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
151-
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
169+
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
170+
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
152171
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
153-
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
154172
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
173+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
174+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
155175
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
156176
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
157177
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
158-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
159178
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
179+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
180+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
160181
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
161182
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

pkg/agent/agent.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package agent
22

33
import (
44
"context"
5+
"fmt"
6+
"net"
57

68
"github.com/netobserv/gopipes/pkg/node"
79
"github.com/netobserv/netobserv-ebpf-agent/pkg/ebpf"
@@ -34,22 +36,25 @@ type flowExporter func(in <-chan []*flow.Record)
3436
// FlowsAgent instantiates a new agent, given a configuration.
3537
func FlowsAgent(cfg *Config) (*Flows, error) {
3638
alog.Info("initializing Flows agent")
37-
interfaces, err := getInterfaces(cfg)
39+
interfaces, err := getInterfaces(cfg, net.Interfaces)
3840
if err != nil {
3941
return nil, err
4042
}
4143
tracers := map[string]flowTracer{}
4244
for iface := range interfaces {
4345
tracers[iface] = ebpf.NewFlowTracer(iface)
4446
}
45-
grpcExporter, err := exporter.StartGRPCProto(cfg.FlowsTarget)
47+
target := fmt.Sprintf("%s:%d", cfg.TargetHost, cfg.TargetPort)
48+
grpcExporter, err := exporter.StartGRPCProto(target)
4649
if err != nil {
4750
return nil, err
4851
}
4952
return &Flows{
50-
tracers: tracers,
51-
accounter: flow.NewAccounter(cfg.CacheMaxFlows, cfg.BuffersLen, cfg.CacheActiveTimeout),
52-
exporter: grpcExporter.ExportFlows,
53+
tracers: tracers,
54+
accounter: flow.NewAccounter(cfg.CacheMaxFlows,
55+
cfg.BuffersLength,
56+
cfg.CacheActiveTimeout),
57+
exporter: grpcExporter.ExportFlows,
5358
}, nil
5459
}
5560

pkg/agent/agent_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package agent
22

33
import (
44
"context"
5-
"fmt"
65
"testing"
76
"time"
87

@@ -27,11 +26,12 @@ func TestFlowsAgent(t *testing.T) {
2726

2827
// GIVEN a flows agent
2928
flowsAgent, err := FlowsAgent(&Config{
30-
FlowsTarget: fmt.Sprintf("127.0.0.1:%d", port),
29+
TargetHost: "127.0.0.1",
30+
TargetPort: port,
3131
Verbose: true,
3232
CacheMaxFlows: 1,
3333
CacheActiveTimeout: 5 * time.Second,
34-
BuffersLen: 10,
34+
BuffersLength: 10,
3535
})
3636
require.NoError(t, err)
3737
// replacing the real eBPF tracer (requires running as root in kernel space)

pkg/agent/config.go

Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,91 @@
11
package agent
22

33
import (
4+
"fmt"
45
"net"
6+
"regexp"
7+
"strings"
58
"time"
69
)
710

8-
// TODO: NETOBSERV-201: fill from CLI and env
911
type Config struct {
10-
// FlowsTarget is the host:port target of the Flow's collector
11-
FlowsTarget string
12-
// Ifaces contains the interface names where flow traces will be attached. If empty, the agent
13-
// will fetch all the interfaces in the system, excepting the ones listed in ExcludeIfaces
14-
Ifaces []string
15-
// ExcludeIfaces contains the interface names that will be excluded from flow tracing. Default:
16-
// "lo" (loopback)
17-
ExcludeIfaces []string
18-
// BuffersLen establishes the length of communication channels between the different processing
12+
// TargetHost is the host name or IP of the target Flow collector
13+
TargetHost string `env:"FLOWS_TARGET_HOST,notEmpty"`
14+
// TargetHost is the port the target Flow collector
15+
TargetPort int `env:"FLOWS_TARGET_PORT,notEmpty"`
16+
// Interfaces contains the interface names from where flows will be collected. If empty, the agent
17+
// will fetch all the interfaces in the system, excepting the ones listed in ExcludeInterfaces.
18+
// If an entry is enclosed by slashes (e.g. `/br-/`), it will match as regular expression,
19+
// otherwise it will be matched as a case-sensitive string.
20+
Interfaces []string `env:"INTERFACES" envSeparator:","`
21+
// ExcludeInterfaces contains the interface names that will be excluded from flow tracing. Default:
22+
// "lo" (loopback).
23+
// If an entry is enclosed by slashes (e.g. `/br-/`), it will match as regular expression,
24+
// otherwise it will be matched as a case-sensitive string.
25+
ExcludeInterfaces []string `env:"EXCLUDE_INTERFACES" envSeparator:"," envDefault:"lo"`
26+
// BuffersLength establishes the length of communication channels between the different processing
1927
// stages
20-
BuffersLen int
28+
BuffersLength int `env:"BUFFERS_LENGTH" envDefault:"50"`
2129
// CacheMaxFlows specifies how many flows can be accumulated in the accounting cache before
2230
// being flushing the cache for its later export
23-
CacheMaxFlows int
31+
CacheMaxFlows int `env:"CACHE_MAX_FLOWS" envDefault:"1000"`
2432
// CacheActiveTimeout specifies the maximum duration in which a flow is kept in the accounting
2533
// cache before being flushed for its later export
26-
CacheActiveTimeout time.Duration
27-
// Verbose mode
28-
Verbose bool
34+
CacheActiveTimeout time.Duration `env:"CACHE_ACTIVE_TIMEOUT" envDefault:"5s"`
35+
// Verbose logs mode
36+
Verbose bool `env:"VERBOSE" envDefault:"false"`
2937
}
3038

31-
// TODO: Ifaces and ExcludeIfaces should admit regexpes e.g. "exclude all the /^br-/ interfaces"
32-
func getInterfaces(cfg *Config) (map[string]struct{}, error) {
39+
func getInterfaces(cfg *Config, interfaces func() ([]net.Interface, error)) (map[string]struct{}, error) {
3340
// get interfaces from configuration or acquire them from the system
34-
ifaces := map[string]struct{}{}
35-
if cfg.Ifaces != nil {
36-
for _, iface := range cfg.Ifaces {
37-
ifaces[iface] = struct{}{}
38-
}
39-
return ifaces, nil
40-
}
41-
nifaces, err := net.Interfaces()
41+
actual, err := interfaces()
4242
if err != nil {
43-
return nil, err
43+
return nil, fmt.Errorf("can't get network interfaces: %w", err)
4444
}
45-
for _, iface := range nifaces {
46-
ifaces[iface.Name] = struct{}{}
45+
accepted := map[string]struct{}{}
46+
47+
// Accept only defined interfaces, or all if the interfaces section is not defined
48+
if len(cfg.Interfaces) > 0 {
49+
for _, definition := range cfg.Interfaces {
50+
for _, iface := range actual {
51+
if m, err := isMatch(iface.Name, definition); err != nil {
52+
return nil, fmt.Errorf("wrong definition of interface: %w", err)
53+
} else if m {
54+
accepted[iface.Name] = struct{}{}
55+
}
56+
}
57+
}
58+
} else {
59+
for _, iface := range actual {
60+
accepted[iface.Name] = struct{}{}
61+
}
4762
}
63+
4864
// exclude interfaces
49-
for _, iface := range cfg.ExcludeIfaces {
50-
delete(ifaces, iface)
65+
for _, definition := range cfg.ExcludeInterfaces {
66+
for iface := range accepted {
67+
if m, err := isMatch(iface, definition); err != nil {
68+
return nil, fmt.Errorf("wrong definition of excluded interfaces: %w", err)
69+
} else if m {
70+
delete(accepted, iface)
71+
}
72+
}
73+
}
74+
return accepted, nil
75+
}
76+
77+
func isMatch(iface, definition string) (bool, error) {
78+
var isRegexp = regexp.MustCompile("^/(.*)/$")
79+
definition = strings.Trim(definition, " ")
80+
81+
// the user defined a /regexp/ between slashes: check if matches
82+
if sm := isRegexp.FindStringSubmatch(definition); len(sm) > 0 {
83+
m, err := regexp.MatchString(sm[1], iface)
84+
if err != nil {
85+
return false, fmt.Errorf("wrong pattern %s: %w", definition, err)
86+
}
87+
return m, nil
5188
}
52-
return ifaces, nil
89+
// the user defined a plain string: check exact match
90+
return iface == definition, nil
5391
}

0 commit comments

Comments
 (0)