Skip to content

Commit 809c499

Browse files
authored
NETOBSERV-1974: use ovnk-lib to get net event messages (#647)
* get net events messages - web reimpl * add ovnk deps * Use ovnk-lib to get event string * Revert "get net events messages - web reimpl" This reverts commit 8efee4c. * fix test and feature name * use released ovnk * fix test
1 parent 5497084 commit 809c499

File tree

480 files changed

+103242
-146
lines changed

Some content is hidden

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

480 files changed

+103242
-146
lines changed

cmd/plugin-backend.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import (
55
"flag"
66
"fmt"
77
"os"
8+
"slices"
89

910
"github.com/sirupsen/logrus"
1011

1112
"github.com/netobserv/network-observability-console-plugin/pkg/config"
13+
"github.com/netobserv/network-observability-console-plugin/pkg/decoders"
14+
"github.com/netobserv/network-observability-console-plugin/pkg/model"
1215
"github.com/netobserv/network-observability-console-plugin/pkg/server"
1316
)
1417

@@ -54,6 +57,11 @@ func main() {
5457
log.WithError(err).Fatal("auth checker error")
5558
}
5659

60+
if slices.Contains(cfg.Frontend.Features, "networkEvents") {
61+
// Add decoder hook
62+
model.AddFlowLineMapping(decoders.NetworkEventsToString)
63+
}
64+
5765
go server.StartMetrics(&server.MetricsConfig{
5866
Port: cfg.Server.MetricsPort,
5967
CertPath: cfg.Server.CertPath,

go.mod

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,32 @@ require (
88
github.com/gorilla/mux v1.8.1
99
github.com/json-iterator/go v1.1.12
1010
github.com/mitchellh/mapstructure v1.5.0
11+
github.com/ovn-org/ovn-kubernetes/go-controller v0.0.0-20241126140656-c95491e46334
1112
github.com/prometheus/client_golang v1.20.2
1213
github.com/prometheus/common v0.57.0
1314
github.com/sirupsen/logrus v1.9.3
1415
github.com/stretchr/testify v1.9.0
1516
gopkg.in/yaml.v3 v3.0.1
16-
k8s.io/api v0.31.0
17-
k8s.io/apimachinery v0.31.0
18-
k8s.io/client-go v0.31.0
17+
k8s.io/api v0.31.1
18+
k8s.io/apimachinery v0.31.1
19+
k8s.io/client-go v0.31.1
1920
)
2021

2122
require (
2223
github.com/beorn7/perks v1.0.1 // indirect
24+
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
25+
github.com/cenkalti/hub v1.0.1 // indirect
26+
github.com/cenkalti/rpc2 v0.0.0-20210604223624-c1acbc6ec984 // indirect
2327
github.com/cespare/xxhash/v2 v2.3.0 // indirect
28+
github.com/containernetworking/cni v1.1.2 // indirect
29+
github.com/containernetworking/plugins v1.2.0 // indirect
30+
github.com/coreos/go-iptables v0.6.0 // indirect
31+
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
2432
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
2533
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
2634
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
2735
github.com/go-logr/logr v1.4.2 // indirect
36+
github.com/go-logr/stdr v1.2.2 // indirect
2837
github.com/go-openapi/jsonpointer v0.19.6 // indirect
2938
github.com/go-openapi/jsonreference v0.20.2 // indirect
3039
github.com/go-openapi/swag v0.22.4 // indirect
@@ -42,19 +51,29 @@ require (
4251
github.com/modern-go/reflect2 v1.0.2 // indirect
4352
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
4453
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
54+
github.com/ovn-org/libovsdb v0.7.1-0.20240820095311-ce1951614a20 // indirect
4555
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
4656
github.com/prometheus/client_model v0.6.1 // indirect
4757
github.com/prometheus/procfs v0.15.1 // indirect
58+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
59+
github.com/safchain/ethtool v0.3.1-0.20231027162144-83e5e0097c91 // indirect
4860
github.com/stretchr/objx v0.5.2 // indirect
61+
github.com/urfave/cli/v2 v2.27.2 // indirect
62+
github.com/vishvananda/netlink v1.2.1-beta.2.0.20231024175852-77df5d35f725 // indirect
63+
github.com/vishvananda/netns v0.0.4 // indirect
4964
github.com/x448/float16 v0.8.4 // indirect
65+
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
5066
golang.org/x/net v0.27.0 // indirect
5167
golang.org/x/oauth2 v0.21.0 // indirect
5268
golang.org/x/sys v0.22.0 // indirect
5369
golang.org/x/term v0.22.0 // indirect
5470
golang.org/x/text v0.16.0 // indirect
5571
golang.org/x/time v0.3.0 // indirect
5672
google.golang.org/protobuf v1.34.2 // indirect
73+
gopkg.in/gcfg.v1 v1.2.3 // indirect
5774
gopkg.in/inf.v0 v0.9.1 // indirect
75+
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
76+
gopkg.in/warnings.v0 v0.1.2 // indirect
5877
gopkg.in/yaml.v2 v2.4.0 // indirect
5978
k8s.io/klog/v2 v2.130.1 // indirect
6079
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect

go.sum

Lines changed: 131 additions & 8 deletions
Large diffs are not rendered by default.

pkg/decoders/network-events.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package decoders
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/netobserv/network-observability-console-plugin/pkg/model/fields"
7+
ovnmodel "github.com/ovn-org/ovn-kubernetes/go-controller/observability-lib/model"
8+
"github.com/sirupsen/logrus"
9+
)
10+
11+
var dlog = logrus.WithField("module", "decoders")
12+
13+
func NetworkEventsToString(in string) string {
14+
line := make(map[string]any)
15+
if err := json.Unmarshal([]byte(in), &line); err != nil {
16+
dlog.Errorf("Could not decode NetworkEvent: %v", err)
17+
return in
18+
}
19+
if ne, found := line[fields.NetworkEvents]; found {
20+
if neList, isList := ne.([]any); isList {
21+
var messages []string
22+
for _, item := range neList {
23+
if neItem, isMap := item.(map[string]any); isMap {
24+
messages = append(messages, networkEventItemToString(neItem))
25+
}
26+
}
27+
line[fields.NetworkEvents] = messages
28+
b, err := json.Marshal(line)
29+
if err != nil {
30+
dlog.Errorf("Could not reencode NetworkEvent: %v", err)
31+
return in
32+
}
33+
return string(b)
34+
}
35+
}
36+
return in
37+
}
38+
39+
func networkEventItemToString(in map[string]any) string {
40+
if msg := getAsString(in, "Message"); msg != "" {
41+
return msg
42+
}
43+
if feat := getAsString(in, "Feature"); feat == "acl" {
44+
aclObj := ovnmodel.ACLEvent{
45+
Action: getAsString(in, "Action"),
46+
Actor: getAsString(in, "Type"),
47+
Name: getAsString(in, "Name"),
48+
Namespace: getAsString(in, "Namespace"),
49+
Direction: getAsString(in, "Direction"),
50+
}
51+
return aclObj.String()
52+
}
53+
return ""
54+
}
55+
56+
func getAsString(in map[string]any, key string) string {
57+
if anyV, hasKey := in[key]; hasKey {
58+
if v, isStr := anyV.(string); isStr {
59+
return v
60+
}
61+
}
62+
return ""
63+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package decoders
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestReencode_NoChange(t *testing.T) {
10+
js := `{"SrcK8S_Name":"ip-10-0-1-7.ec2.internal","Bytes":66,"Packets":1,"Interfaces":["br-ex"]}`
11+
out := NetworkEventsToString(js)
12+
assert.Equal(t, js, out)
13+
}
14+
15+
func TestReencode_UpdateEvent(t *testing.T) {
16+
js := `{"SrcK8S_Name":"ip-10-0-1-7.ec2.internal","Bytes":66,"Packets":1,"Interfaces":["br-ex"],"NetworkEvents":[{"Feature":"acl","Type":"NetpolNode","Action":"allow","Direction":"Ingress"}]}`
17+
out := NetworkEventsToString(js)
18+
assert.Equal(
19+
t,
20+
`{"Bytes":66,"Interfaces":["br-ex"],"NetworkEvents":["Allowed by default allow from local node policy, direction Ingress"],"Packets":1,"SrcK8S_Name":"ip-10-0-1-7.ec2.internal"}`,
21+
out,
22+
)
23+
24+
js = `{"SrcK8S_Name":"ip-10-0-1-7.ec2.internal","Bytes":66,"Packets":1,"Interfaces":["br-ex"],"NetworkEvents":[{"Message":"custom message"}]}`
25+
out = NetworkEventsToString(js)
26+
assert.Equal(
27+
t,
28+
`{"Bytes":66,"Interfaces":["br-ex"],"NetworkEvents":["custom message"],"Packets":1,"SrcK8S_Name":"ip-10-0-1-7.ec2.internal"}`,
29+
out,
30+
)
31+
}

pkg/model/loki.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ import (
1010
"github.com/prometheus/common/model"
1111
)
1212

13+
type FlowLineMapping = func(string) string
14+
15+
var (
16+
flowLineMappings = []FlowLineMapping{}
17+
)
18+
19+
func AddFlowLineMapping(f FlowLineMapping) {
20+
flowLineMappings = append(flowLineMappings, f)
21+
}
22+
1323
// QueryResponse represents the http json response to a logQL query
1424
type QueryResponse struct {
1525
Status string `json:"status"`
@@ -128,6 +138,13 @@ func unmarshalQueryResponseData(data []byte) (ResultType, ResultValue, interface
128138
case ResultTypeStream:
129139
var s Streams
130140
err = json.Unmarshal(unmarshal.Result, &s)
141+
for _, mapping := range flowLineMappings {
142+
for i := range s {
143+
for ii := range s[i].Entries {
144+
s[i].Entries[ii].Line = mapping(s[i].Entries[ii].Line)
145+
}
146+
}
147+
}
131148
value = s
132149
case ResultTypeMatrix:
133150
var m Matrix

pkg/model/loki_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package model
22

33
import (
44
"encoding/json"
5+
"strings"
56
"testing"
7+
"time"
68

79
"github.com/netobserv/network-observability-console-plugin/pkg/utils/constants"
810
"github.com/stretchr/testify/assert"
@@ -34,6 +36,57 @@ func TestQueryResponseUnmarshal(t *testing.T) {
3436
assert.IsType(t, expType, qr.Data.Result)
3537
}
3638

39+
func TestQueryResponseUnmarshalLineMapping(t *testing.T) {
40+
AddFlowLineMapping(func(in string) string {
41+
return strings.ReplaceAll(in, "Bytes", "Bytezz")
42+
})
43+
js := `
44+
{
45+
"status": "success",
46+
"data": {
47+
"resultType": "streams",
48+
"result": [
49+
{
50+
"stream": {
51+
"app": "netobserv-flowcollector"
52+
},
53+
"values": [
54+
[
55+
"1731930300000000000",
56+
"{\"SrcK8S_Name\":\"ip-10-0-1-7.ec2.internal\",\"Bytes\":66,\"Packets\":1,\"Interfaces\":[\"br-ex\"]}"
57+
]
58+
]
59+
}
60+
],
61+
"stats": {
62+
"summary": {}
63+
}
64+
}
65+
}
66+
`
67+
68+
var qr QueryResponse
69+
err := json.Unmarshal([]byte(js), &qr)
70+
require.NoError(t, err)
71+
assert.Equal(t, ResultTypeStream, string(qr.Data.ResultType))
72+
assert.NotNil(t, qr.Data.Result)
73+
var expType Streams
74+
assert.IsType(t, expType, qr.Data.Result)
75+
data := qr.Data.Result.(Streams)
76+
assert.Len(t, data, 1)
77+
assert.Equal(t, Stream{
78+
Labels: map[string]string{
79+
"app": "netobserv-flowcollector",
80+
},
81+
Entries: []Entry{
82+
{
83+
Timestamp: time.Unix(1731930300, 0),
84+
Line: `{"SrcK8S_Name":"ip-10-0-1-7.ec2.internal","Bytezz":66,"Packets":1,"Interfaces":["br-ex"]}`,
85+
},
86+
},
87+
}, data[0])
88+
}
89+
3790
func TestAggregatedQueryResponseMarshal(t *testing.T) {
3891
qr := AggregatedQueryResponse{
3992
ResultType: ResultTypeStream,

vendor/github.com/cenkalti/backoff/v4/.gitignore

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/cenkalti/backoff/v4/LICENSE

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/cenkalti/backoff/v4/README.md

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)