Skip to content

Commit 5e378ca

Browse files
committed
Add network service to MonitorScopeSelector (#178)
Allow MonitorScopeSelector to filter connections based on network service as well. Signed-off-by: Lugossy Zoltan <[email protected]>
1 parent df76555 commit 5e378ca

File tree

4 files changed

+247
-28
lines changed

4 files changed

+247
-28
lines changed

pkg/api/networkservice/connection.pb.go

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

pkg/api/networkservice/connection.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright 2018 Red Hat, Inc.
22
// Copyright (c) 2018 Cisco and/or its affiliates.
3+
// Copyright (c) 2024 Nordix Foundation.
34
//
45
// Licensed under the Apache License, Version 2.0 (the "License");
56
// you may not use this file except in compliance with the License.
@@ -91,6 +92,7 @@ message ConnectionEvent {
9192

9293
message MonitorScopeSelector {
9394
repeated PathSegment path_segments = 1;
95+
repeated string network_services = 2;
9496
}
9597

9698
service MonitorConnection {

pkg/api/networkservice/connection_helpers.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//
55
// Copyright (c) 2023-2024 Cisco and/or its affiliates.
66
//
7+
// Copyright (c) 2024 Nordix Foundation.
8+
//
79
// SPDX-License-Identifier: Apache-2.0
810
//
911
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -37,12 +39,38 @@ func (x *Connection) Clone() *Connection {
3739
return proto.Clone(x).(*Connection)
3840
}
3941

42+
// Iterates through the selector.NetworkServices array looking for a network service
43+
// that matches the Connection.NetworkService. Treats an empty selector.NetworkService
44+
// array as a wildcard - Returns true on match
45+
func (x *Connection) matchesNetworkService(networkServices []string) bool {
46+
if len(networkServices) == 0 {
47+
return true
48+
}
49+
50+
for _, v := range networkServices {
51+
if v == x.GetNetworkService() {
52+
// matched network service
53+
return true
54+
}
55+
}
56+
57+
return false
58+
}
59+
4060
// MatchesMonitorScopeSelector - Returns true of the connection matches the selector
4161
func (x *Connection) MatchesMonitorScopeSelector(selector *MonitorScopeSelector) bool {
4262
if x == nil {
4363
return false
4464
}
4565
// Note: Empty selector always matches a non-nil Connection
66+
if len(selector.GetPathSegments()) == 0 && len(selector.GetNetworkServices()) == 0 {
67+
return true
68+
}
69+
// Compare network service names first
70+
if !x.matchesNetworkService(selector.GetNetworkServices()) {
71+
return false
72+
}
73+
// Empty PathSegments in selector, treat as wildcard
4674
if len(selector.GetPathSegments()) == 0 {
4775
return true
4876
}

pkg/api/networkservice/connection_helpers_test.go

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Copyright (c) 2023 Cisco and/or its affiliates.
22
//
3+
// Copyright (c) 2024 Nordix Foundation.
4+
//
35
// SPDX-License-Identifier: Apache-2.0
46
//
57
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -99,3 +101,161 @@ func TestMonitorScopeSelector(t *testing.T) {
99101
})
100102
}
101103
}
104+
105+
// nolint: funlen
106+
func TestNetworkServiceMonitorScopeSelector(t *testing.T) {
107+
cases := []struct {
108+
testname string
109+
conn *networkservice.Connection
110+
selector *networkservice.MonitorScopeSelector
111+
matches bool
112+
}{
113+
{
114+
testname: "EmptySelector",
115+
conn: &networkservice.Connection{
116+
Path: &networkservice.Path{
117+
PathSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}},
118+
},
119+
NetworkService: "ns1",
120+
},
121+
selector: &networkservice.MonitorScopeSelector{},
122+
matches: true,
123+
},
124+
{
125+
testname: "IdenticalPathsAndNetworkService",
126+
conn: &networkservice.Connection{
127+
Path: &networkservice.Path{
128+
PathSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}},
129+
},
130+
NetworkService: "ns1",
131+
},
132+
selector: &networkservice.MonitorScopeSelector{
133+
PathSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}},
134+
NetworkServices: []string{"ns1"},
135+
},
136+
matches: true,
137+
},
138+
{
139+
testname: "IdenticalPathsAndDifferentNetworkService",
140+
conn: &networkservice.Connection{
141+
Path: &networkservice.Path{
142+
PathSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}},
143+
},
144+
NetworkService: "ns1",
145+
},
146+
selector: &networkservice.MonitorScopeSelector{
147+
PathSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}},
148+
NetworkServices: []string{"ns2"},
149+
},
150+
matches: false,
151+
},
152+
{
153+
testname: "IdenticalPathsAndMatchingNetworkServiceList",
154+
conn: &networkservice.Connection{
155+
Path: &networkservice.Path{
156+
PathSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}},
157+
},
158+
NetworkService: "ns1",
159+
},
160+
selector: &networkservice.MonitorScopeSelector{
161+
PathSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}},
162+
NetworkServices: []string{"ns2", "ns1", "ns3"},
163+
},
164+
matches: true,
165+
},
166+
{
167+
testname: "IdenticalPathsAndNonMatchingNetworkServiceList",
168+
conn: &networkservice.Connection{
169+
Path: &networkservice.Path{
170+
PathSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}},
171+
},
172+
NetworkService: "ns1",
173+
},
174+
selector: &networkservice.MonitorScopeSelector{
175+
PathSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}},
176+
NetworkServices: []string{"ns2", "ns3", "ns0"},
177+
},
178+
matches: false,
179+
},
180+
{
181+
testname: "IdenticalNetworkService",
182+
conn: &networkservice.Connection{
183+
Path: &networkservice.Path{
184+
PathSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}},
185+
},
186+
NetworkService: "ns1",
187+
},
188+
selector: &networkservice.MonitorScopeSelector{
189+
NetworkServices: []string{"ns1"},
190+
},
191+
matches: true,
192+
},
193+
{
194+
testname: "IdenticalNetworkServiceEmptyConnectionPath",
195+
conn: &networkservice.Connection{
196+
NetworkService: "ns1",
197+
},
198+
selector: &networkservice.MonitorScopeSelector{
199+
NetworkServices: []string{"ns1"},
200+
},
201+
matches: true,
202+
},
203+
{
204+
testname: "DifferentNetworkService",
205+
conn: &networkservice.Connection{
206+
Path: &networkservice.Path{
207+
PathSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}},
208+
},
209+
NetworkService: "ns1",
210+
},
211+
selector: &networkservice.MonitorScopeSelector{
212+
NetworkServices: []string{"ns2"},
213+
},
214+
matches: false,
215+
},
216+
{
217+
testname: "DifferentNetworkServiceEmptyConnectionPath",
218+
conn: &networkservice.Connection{
219+
NetworkService: "ns1",
220+
},
221+
selector: &networkservice.MonitorScopeSelector{
222+
NetworkServices: []string{"ns2"},
223+
},
224+
matches: false,
225+
},
226+
{
227+
testname: "MatchingNetworkServiceList",
228+
conn: &networkservice.Connection{
229+
Path: &networkservice.Path{
230+
PathSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}},
231+
},
232+
NetworkService: "ns1",
233+
},
234+
selector: &networkservice.MonitorScopeSelector{
235+
NetworkServices: []string{"ns2", "ns1", "ns3"},
236+
},
237+
matches: true,
238+
},
239+
{
240+
testname: "NonMatchingNetworkServiceList",
241+
conn: &networkservice.Connection{
242+
Path: &networkservice.Path{
243+
PathSegments: []*networkservice.PathSegment{{Name: "s1", Id: "id1", Token: "t1"}, {Name: "s2", Id: "id2", Token: "t2"}},
244+
},
245+
NetworkService: "ns1",
246+
},
247+
selector: &networkservice.MonitorScopeSelector{
248+
NetworkServices: []string{"ns2", "ns3", "ns0"},
249+
},
250+
matches: false,
251+
},
252+
}
253+
254+
for _, testCase := range cases {
255+
c := testCase
256+
t.Run(c.testname, func(t *testing.T) {
257+
258+
require.Equal(t, c.conn.MatchesMonitorScopeSelector(c.selector), c.matches)
259+
})
260+
}
261+
}

0 commit comments

Comments
 (0)