Skip to content

Commit

Permalink
fixed updates of excluded 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 1, 2021
1 parent e33b665 commit 7a8881c
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 10 deletions.
29 changes: 21 additions & 8 deletions pkg/networkservice/common/excludedprefixes/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ import (
)

type excludedPrefixesServer struct {
ctx context.Context
prefixPool atomic.Value
once sync.Once
configPath string
ctx context.Context
prefixPool atomic.Value
once sync.Once
configPath string
previousPrefixes map[string]struct{}
}

func (eps *excludedPrefixesServer) init(ctx context.Context) {
Expand Down Expand Up @@ -88,9 +89,20 @@ func (eps *excludedPrefixesServer) Request(ctx context.Context, request *network
conn.Context.IpContext = &networkservice.IPContext{}
}
prefixes := eps.prefixPool.Load().(*ippool.PrefixPool).GetPrefixes()
log.FromContext(ctx).Debugf("ExcludedPrefixesService: adding excluded prefixes to connection: %v", prefixes)

ipCtx := conn.GetContext().GetIpContext()
ipCtx.ExcludedPrefixes = removeDuplicates(append(ipCtx.GetExcludedPrefixes(), prefixes...))
withoutPrevious := removePrefixes(ipCtx.GetExcludedPrefixes(), eps.previousPrefixes)
ipCtx.ExcludedPrefixes = removeDuplicates(append(withoutPrevious, prefixes...))

for _, p := range prefixes {
eps.previousPrefixes[p] = struct{}{}
}

log.FromContext(ctx).
WithField("excludedPrefixes", "server").
WithField("loadedPrefixes", prefixes).
WithField("previousPrefixes", eps.previousPrefixes).
Debugf("adding excluded prefixes to connection")

return next.Server(ctx).Request(ctx, request)
}
Expand All @@ -104,8 +116,9 @@ func (eps *excludedPrefixesServer) Close(ctx context.Context, connection *networ
// Note: request.Connection and Connection.Context should not be nil when calling Request
func NewServer(ctx context.Context, setters ...ServerOption) networkservice.NetworkServiceServer {
server := &excludedPrefixesServer{
configPath: PrefixesFilePathDefault,
ctx: ctx,
configPath: PrefixesFilePathDefault,
ctx: ctx,
previousPrefixes: map[string]struct{}{},
}
for _, setter := range setters {
setter(server)
Expand Down
49 changes: 49 additions & 0 deletions pkg/networkservice/common/excludedprefixes/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,55 @@ func TestUniqueRequestPrefixes(t *testing.T) {
require.NoError(t, err)
}

func TestChangedPrefixes(t *testing.T) {
prefixes := []string{"172.16.1.0/24", "10.32.0.0/12", "10.96.0.0/12", "10.20.128.0/17", "10.20.64.0/18", "10.20.8.0/21", "10.20.4.0/22"}
reqPrefixes := []string{"100.1.1.0/13", "10.32.0.0/12", "10.96.0.0/12", "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"}
diffPrefxies := []string{"100.1.1.0/13", "10.32.0.0/12", "10.96.0.0/12", "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.20.4.0/22", "10.20.8.0/21", "172.16.1.0/24"}

dir := filepath.Join(os.TempDir(), t.Name())
defer func() { _ = os.RemoveAll(dir) }()
require.NoError(t, os.MkdirAll(dir, os.ModePerm))

testConfig := strings.Join(append([]string{"prefixes:"}, prefixes...), "\n- ")
configPath := filepath.Join(dir, defaultPrefixesFileName)
require.NoError(t, ioutil.WriteFile(configPath, []byte(testConfig), os.ModePerm))
defer func() { _ = os.Remove(configPath) }()

isFirst := true
server := chain.NewNetworkServiceServer(excludedprefixes.NewServer(context.Background(), excludedprefixes.WithConfigPath(configPath)), checkrequest.NewServer(t, func(t *testing.T, request *networkservice.NetworkServiceRequest) {
if isFirst {
require.Equal(t, diffPrefxies, request.Connection.Context.IpContext.ExcludedPrefixes)
isFirst = false
}
}))
req := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Context: &networkservice.ConnectionContext{
IpContext: &networkservice.IPContext{
ExcludedPrefixes: reqPrefixes,
},
},
},
}

_, err := server.Request(context.Background(), req)

newExcludedPrefixes := []string{"172.16.2.0/24"}
require.NoError(t, ioutil.WriteFile(configPath, []byte(strings.Join(append([]string{"prefixes:"}, newExcludedPrefixes...), "\n- ")), os.ModePerm))

require.Eventually(t, func() bool {
var prev []string
copy(prev, req.Connection.Context.IpContext.ExcludedPrefixes)
_, err = server.Request(context.Background(), req)
return len(req.Connection.Context.IpContext.ExcludedPrefixes) != len(prev)
}, time.Minute, time.Millisecond*100)

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"}
require.Equal(t, newDiffPrefixes, req.Connection.Context.IpContext.ExcludedPrefixes)

require.NoError(t, err)
}

func testWaitForFile(t *testing.T, filePath string) {
prefixes := []string{"10.80.0.0/12", "172.16.1.0/24"}

Expand Down
19 changes: 17 additions & 2 deletions pkg/networkservice/common/excludedprefixes/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2020 Doc.ai and/or its affiliates.
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
//
// Copyright (c) 2020 Cisco and/or its affiliates.
// Copyright (c) 2020-2021 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -31,3 +31,18 @@ func removeDuplicates(elements []string) []string {
}
return result
}

func removePrefixes(origin []string, prefixesToRemove map[string]struct{}) []string {
if len(origin) == 0 || len(prefixesToRemove) == 0 {
return origin
}

var rv []string
for _, p := range origin {
if _, ok := prefixesToRemove[p]; !ok {
rv = append(rv, p)
}
}

return rv
}

0 comments on commit 7a8881c

Please sign in to comment.