Skip to content

Commit

Permalink
added metadata
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 50483d3 commit 02815db
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 26 deletions.
38 changes: 38 additions & 0 deletions pkg/networkservice/common/excludedprefixes/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package excludedprefixes

import (
"context"

"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
)

type keyType struct{}

func load(ctx context.Context) (map[string]struct{}, bool) {
val, ok := metadata.Map(ctx, false).Load(keyType{})
if !ok {
return map[string]struct{}{}, false
}

return val.(map[string]struct{}), true
}

func store(ctx context.Context, prefixes map[string]struct{}) {
metadata.Map(ctx, false).Store(keyType{}, prefixes)
}
23 changes: 12 additions & 11 deletions pkg/networkservice/common/excludedprefixes/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ import (
)

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

func (eps *excludedPrefixesServer) init(ctx context.Context) {
Expand Down Expand Up @@ -90,18 +89,21 @@ func (eps *excludedPrefixesServer) Request(ctx context.Context, request *network
}
prefixes := eps.prefixPool.Load().(*ippool.PrefixPool).GetPrefixes()

previousPrefixes, _ := load(ctx)

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

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

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

return next.Server(ctx).Request(ctx, request)
Expand All @@ -116,9 +118,8 @@ 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,
previousPrefixes: map[string]struct{}{},
configPath: PrefixesFilePathDefault,
ctx: ctx,
}
for _, setter := range setters {
setter(server)
Expand Down
56 changes: 41 additions & 15 deletions pkg/networkservice/common/excludedprefixes/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/networkservicemesh/sdk/pkg/networkservice/common/excludedprefixes"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkrequest"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
)

const defaultPrefixesFileName = "excluded_prefixes.yaml"
Expand All @@ -49,9 +50,13 @@ func TestNewExcludedPrefixesService(t *testing.T) {
configPath := filepath.Join(dir, defaultPrefixesFileName)
require.NoError(t, ioutil.WriteFile(configPath, []byte(testConfig), os.ModePerm))

server := chain.NewNetworkServiceServer(excludedprefixes.NewServer(context.Background(), excludedprefixes.WithConfigPath(configPath)), checkrequest.NewServer(t, func(t *testing.T, request *networkservice.NetworkServiceRequest) {
require.Equal(t, request.Connection.Context.IpContext.ExcludedPrefixes, prefixes)
}))
server := chain.NewNetworkServiceServer(
metadata.NewServer(),
excludedprefixes.NewServer(context.Background(), excludedprefixes.WithConfigPath(configPath)),
checkrequest.NewServer(t, func(t *testing.T, request *networkservice.NetworkServiceRequest) {
require.Equal(t, request.Connection.Context.IpContext.ExcludedPrefixes, prefixes)
}),
)
req := request()

_, err := server.Request(context.Background(), req)
Expand All @@ -71,7 +76,13 @@ func TestCheckReloadedPrefixes(t *testing.T) {

defer func() { _ = os.Remove(configPath) }()

server := chain.NewNetworkServiceServer(excludedprefixes.NewServer(context.Background(), excludedprefixes.WithConfigPath(configPath)))
server := chain.NewNetworkServiceServer(
metadata.NewServer(),
excludedprefixes.NewServer(
context.Background(),
excludedprefixes.WithConfigPath(configPath),
),
)
req := request()

err := ioutil.WriteFile(configPath, []byte(testConfig), 0600)
Expand Down Expand Up @@ -114,11 +125,17 @@ func TestUniqueRequestPrefixes(t *testing.T) {
require.NoError(t, ioutil.WriteFile(configPath, []byte(testConfig), os.ModePerm))
defer func() { _ = os.Remove(configPath) }()

server := chain.NewNetworkServiceServer(excludedprefixes.NewServer(context.Background(), excludedprefixes.WithConfigPath(configPath)), checkrequest.NewServer(t, func(t *testing.T, request *networkservice.NetworkServiceRequest) {
require.Equal(t, uniquePrefixes, request.Connection.Context.IpContext.ExcludedPrefixes)
}))
server := chain.NewNetworkServiceServer(
metadata.NewServer(),
excludedprefixes.NewServer(context.Background(), excludedprefixes.WithConfigPath(configPath)),
checkrequest.NewServer(t, func(t *testing.T, request *networkservice.NetworkServiceRequest) {
require.Equal(t, uniquePrefixes, request.Connection.Context.IpContext.ExcludedPrefixes)
}),
)

req := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Id: "1",
Context: &networkservice.ConnectionContext{
IpContext: &networkservice.IPContext{
ExcludedPrefixes: reqPrefixes,
Expand Down Expand Up @@ -146,14 +163,20 @@ func TestChangedPrefixes(t *testing.T) {
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
}
}))
server := chain.NewNetworkServiceServer(
metadata.NewServer(),
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{
Id: "1",
Context: &networkservice.ConnectionContext{
IpContext: &networkservice.IPContext{
ExcludedPrefixes: reqPrefixes,
Expand Down Expand Up @@ -185,8 +208,10 @@ func testWaitForFile(t *testing.T, filePath string) {

testConfig := strings.Join(append([]string{"prefixes:"}, prefixes...), "\n- ")

server := chain.NewNetworkServiceServer(excludedprefixes.NewServer(context.Background(),
excludedprefixes.WithConfigPath(filePath)))
server := chain.NewNetworkServiceServer(
metadata.NewServer(),
excludedprefixes.NewServer(context.Background(), excludedprefixes.WithConfigPath(filePath)),
)

req := request()
_, err := server.Request(context.Background(), req)
Expand Down Expand Up @@ -216,6 +241,7 @@ func testWaitForFile(t *testing.T, filePath string) {
func request() *networkservice.NetworkServiceRequest {
return &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Id: "1",
Context: &networkservice.ConnectionContext{},
},
}
Expand Down

0 comments on commit 02815db

Please sign in to comment.