Skip to content

Commit

Permalink
modified updating logic, added diff cutting of prefixes
Browse files Browse the repository at this point in the history
Signed-off-by: Mikhail Avramenko <avramenkomihail15@gmail.com>
  • Loading branch information
Mixaster995 committed Dec 23, 2021
1 parent c0ccdd0 commit df2bfcf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pkg/networkservice/common/excludedprefixes/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type keyType struct{}
type prefixesInfo struct {
previousFilePrefixes []string
previousClientPrefixes []string
previousDiff []string
}

func load(ctx context.Context) (prefixesInfo, bool) {
Expand All @@ -41,3 +42,7 @@ func load(ctx context.Context) (prefixesInfo, bool) {
func store(ctx context.Context, info prefixesInfo) {
metadata.Map(ctx, false).Store(keyType{}, info)
}

func del(ctx context.Context) {
metadata.Map(ctx, false).Delete(keyType{})
}
5 changes: 4 additions & 1 deletion pkg/networkservice/common/excludedprefixes/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (eps *excludedPrefixesServer) Request(ctx context.Context, request *network

finalPrefixes := clientPrefixes
if !clientPrefixesChanged && filePrefixesChanged {
finalPrefixes = removePrefixes(clientPrefixes, prefixesInfo.previousFilePrefixes)
finalPrefixes = removePrefixes(clientPrefixes, prefixesInfo.previousDiff)
}

finalPrefixes = removeDuplicates(append(finalPrefixes, prefixesFromFile...))
Expand All @@ -123,6 +123,8 @@ func (eps *excludedPrefixesServer) Request(ctx context.Context, request *network
copy(prefixesInfo.previousClientPrefixes, finalPrefixes)
}

prefixesInfo.previousDiff = Subtract(clientPrefixes, prefixesFromFile)

log.FromContext(ctx).
WithField("excludedPrefixes", "server").
WithField("prefixesFromFile", prefixesFromFile).
Expand All @@ -135,6 +137,7 @@ func (eps *excludedPrefixesServer) Request(ctx context.Context, request *network
}

func (eps *excludedPrefixesServer) Close(ctx context.Context, connection *networkservice.Connection) (*empty.Empty, error) {
del(ctx)
return next.Server(ctx).Close(ctx, connection)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/networkservice/common/excludedprefixes/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func TestFilePrefixesChanged(t *testing.T) {
newExcludedPrefixes := []string{"172.16.2.0/24"}
require.NoError(t, ioutil.WriteFile(configPath, []byte(strings.Join(append([]string{"prefixes:"}, newExcludedPrefixes...), "\n- ")), os.ModePerm))

newDiffPrefixes := []string{"100.1.1.0/13", "10.20.0.0/24", "10.20.16.0/20", "10.20.2.0/23", "172.16.2.0/24"}
newDiffPrefixes := []string{"10.20.0.0/24", "10.20.128.0/17", "10.20.64.0/18", "10.20.16.0/20", "10.20.2.0/23", "10.32.0.0/12", "10.96.0.0/12", "100.1.1.0/13", "172.16.2.0/24"}

require.Eventually(t, func() bool {
_, err = server.Request(context.Background(), req)
Expand Down
14 changes: 14 additions & 0 deletions pkg/networkservice/common/excludedprefixes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,17 @@ func IsEqual(s1, s2 []string) bool {

return reflect.DeepEqual(s1copy, s2copy)
}

// Subtract calculates setB - setA
func Subtract(setA, setB []string) []string {
sliceMap := toMap(setA)

var rv []string
for _, s := range setB {
if _, ok := sliceMap[s]; !ok {
rv = append(rv, s)
}
}

return rv
}

0 comments on commit df2bfcf

Please sign in to comment.