-
Notifications
You must be signed in to change notification settings - Fork 22
NETOBSERV-1974: use ovnk-lib to get net event messages #647
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
Changes from all commits
13898b7
f266363
4f76872
730880e
91b8974
9d15c65
d9ad426
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package decoders | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/netobserv/network-observability-console-plugin/pkg/model/fields" | ||
ovnmodel "github.com/ovn-org/ovn-kubernetes/go-controller/observability-lib/model" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var dlog = logrus.WithField("module", "decoders") | ||
|
||
func NetworkEventsToString(in string) string { | ||
line := make(map[string]any) | ||
if err := json.Unmarshal([]byte(in), &line); err != nil { | ||
dlog.Errorf("Could not decode NetworkEvent: %v", err) | ||
return in | ||
} | ||
if ne, found := line[fields.NetworkEvents]; found { | ||
if neList, isList := ne.([]any); isList { | ||
var messages []string | ||
for _, item := range neList { | ||
if neItem, isMap := item.(map[string]any); isMap { | ||
messages = append(messages, networkEventItemToString(neItem)) | ||
} | ||
} | ||
line[fields.NetworkEvents] = messages | ||
b, err := json.Marshal(line) | ||
if err != nil { | ||
dlog.Errorf("Could not reencode NetworkEvent: %v", err) | ||
return in | ||
} | ||
return string(b) | ||
} | ||
} | ||
return in | ||
} | ||
|
||
func networkEventItemToString(in map[string]any) string { | ||
if msg := getAsString(in, "Message"); msg != "" { | ||
return msg | ||
} | ||
if feat := getAsString(in, "Feature"); feat == "acl" { | ||
aclObj := ovnmodel.ACLEvent{ | ||
Action: getAsString(in, "Action"), | ||
Actor: getAsString(in, "Type"), | ||
Name: getAsString(in, "Name"), | ||
Namespace: getAsString(in, "Namespace"), | ||
Direction: getAsString(in, "Direction"), | ||
} | ||
return aclObj.String() | ||
} | ||
return "" | ||
} | ||
|
||
func getAsString(in map[string]any, key string) string { | ||
if anyV, hasKey := in[key]; hasKey { | ||
if v, isStr := anyV.(string); isStr { | ||
return v | ||
} | ||
} | ||
return "" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package decoders | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestReencode_NoChange(t *testing.T) { | ||
js := `{"SrcK8S_Name":"ip-10-0-1-7.ec2.internal","Bytes":66,"Packets":1,"Interfaces":["br-ex"]}` | ||
out := NetworkEventsToString(js) | ||
assert.Equal(t, js, out) | ||
} | ||
|
||
func TestReencode_UpdateEvent(t *testing.T) { | ||
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"}]}` | ||
out := NetworkEventsToString(js) | ||
assert.Equal( | ||
t, | ||
`{"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"}`, | ||
out, | ||
) | ||
|
||
js = `{"SrcK8S_Name":"ip-10-0-1-7.ec2.internal","Bytes":66,"Packets":1,"Interfaces":["br-ex"],"NetworkEvents":[{"Message":"custom message"}]}` | ||
out = NetworkEventsToString(js) | ||
assert.Equal( | ||
t, | ||
`{"Bytes":66,"Interfaces":["br-ex"],"NetworkEvents":["custom message"],"Packets":1,"SrcK8S_Name":"ip-10-0-1-7.ec2.internal"}`, | ||
out, | ||
) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think its worse add ovn pkg dependency to build the string, u can just build it urself using the same pattern and and avoid additional dependencies
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what I did at first (see 089040e#diff-ac6bc2fb6ae2ff5deb13e82b18d432e5473729ee0408fa2baf2860c204fa6c0a ) .. but came back from there to avoid duplicating the logic, and avoid having ovn logic here. Also when more types of events are added later, they'll land here more easily with just a lib upgrade.