diff --git a/pkg/networkservice/common/excludedprefixes/metadata.go b/pkg/networkservice/common/excludedprefixes/metadata.go index cb48d198a..67fc04064 100644 --- a/pkg/networkservice/common/excludedprefixes/metadata.go +++ b/pkg/networkservice/common/excludedprefixes/metadata.go @@ -27,6 +27,7 @@ type keyType struct{} type prefixesInfo struct { previousFilePrefixes []string previousClientPrefixes []string + previousDiff []string } func load(ctx context.Context) (prefixesInfo, bool) { @@ -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{}) +} diff --git a/pkg/networkservice/common/excludedprefixes/server.go b/pkg/networkservice/common/excludedprefixes/server.go index 2c1b62213..ef3a88323 100644 --- a/pkg/networkservice/common/excludedprefixes/server.go +++ b/pkg/networkservice/common/excludedprefixes/server.go @@ -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...)) @@ -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). @@ -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) } diff --git a/pkg/networkservice/common/excludedprefixes/server_test.go b/pkg/networkservice/common/excludedprefixes/server_test.go index 01d024618..8499e4f49 100644 --- a/pkg/networkservice/common/excludedprefixes/server_test.go +++ b/pkg/networkservice/common/excludedprefixes/server_test.go @@ -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) diff --git a/pkg/networkservice/common/excludedprefixes/utils.go b/pkg/networkservice/common/excludedprefixes/utils.go index 709a82585..3aef876fa 100644 --- a/pkg/networkservice/common/excludedprefixes/utils.go +++ b/pkg/networkservice/common/excludedprefixes/utils.go @@ -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 +}