Skip to content

Commit

Permalink
Moved to using a generic map instead of generated maps
Browse files Browse the repository at this point in the history
Signed-off-by: Ruslan Bayandinov <[email protected]>
  • Loading branch information
wazsone committed Feb 15, 2023
1 parent 438ec05 commit d72942f
Show file tree
Hide file tree
Showing 41 changed files with 114 additions and 706 deletions.
8 changes: 5 additions & 3 deletions pkg/networkservice/chains/nsmgr/dns_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-2022 Doc.ai and/or its affiliates.
//
// Copyright (c) 2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -25,6 +27,7 @@ import (
"testing"
"time"

"github.com/edwarnicke/genericsync"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/cls"
kernelmech "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/kernel"
Expand All @@ -33,7 +36,6 @@ import (

"github.com/networkservicemesh/sdk/pkg/networkservice/chains/client"
"github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/dnscontext"
"github.com/networkservicemesh/sdk/pkg/tools/dnsconfig"
"github.com/networkservicemesh/sdk/pkg/tools/dnsutils"
"github.com/networkservicemesh/sdk/pkg/tools/dnsutils/cache"
"github.com/networkservicemesh/sdk/pkg/tools/dnsutils/dnsconfigs"
Expand Down Expand Up @@ -74,7 +76,7 @@ func Test_DNSUsecase(t *testing.T) {

nse := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken)

dnsConfigsMap := new(dnsconfig.Map)
dnsConfigsMap := new(genericsync.Map[string, []*networkservice.DNSConfig])
nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken, client.WithAdditionalFunctionality(dnscontext.NewClient(
dnscontext.WithChainContext(ctx),
dnscontext.WithDNSConfigsMap(dnsConfigsMap),
Expand All @@ -88,7 +90,7 @@ func Test_DNSUsecase(t *testing.T) {
}

// DNS server on nse side
dnsRecords := new(memory.Map)
dnsRecords := new(genericsync.Map[string, []net.IP])
dnsRecords.Store("my.domain.", []net.IP{net.ParseIP("4.4.4.4")})
dnsRecords.Store("my.domain.com.", []net.IP{net.ParseIP("5.5.5.5")})
dnsutils.ListenAndServe(ctx, memory.NewDNSHandler(dnsRecords), ":40053")
Expand Down
6 changes: 3 additions & 3 deletions pkg/networkservice/chains/nsmgr/vl3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"testing"
"time"

"github.com/edwarnicke/genericsync"
"github.com/google/uuid"
"github.com/networkservicemesh/api/pkg/api/ipam"
"github.com/networkservicemesh/api/pkg/api/networkservice"
Expand All @@ -37,7 +38,6 @@ import (
"github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/dnscontext/vl3dns"
"github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/ipcontext/vl3"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkconnection"
"github.com/networkservicemesh/sdk/pkg/tools/dnsconfig"
"github.com/networkservicemesh/sdk/pkg/tools/dnsutils"
"github.com/networkservicemesh/sdk/pkg/tools/dnsutils/memory"
"github.com/networkservicemesh/sdk/pkg/tools/sandbox"
Expand Down Expand Up @@ -137,7 +137,7 @@ func Test_vl3NSE_ConnectsTo_vl3NSE(t *testing.T) {
SetRegistryProxySupplier(nil).
Build()

var records memory.Map
var records genericsync.Map[string, []net.IP]
var dnsServer = memory.NewDNSHandler(&records)

records.Store("nsc1.vl3.", []net.IP{net.ParseIP("1.1.1.1")})
Expand All @@ -156,7 +156,7 @@ func Test_vl3NSE_ConnectsTo_vl3NSE(t *testing.T) {

serverPrefixCh <- &ipam.PrefixResponse{Prefix: "10.0.0.1/24"}

var dnsConfigs = new(dnsconfig.Map)
var dnsConfigs = new(genericsync.Map[string, []*networkservice.DNSConfig])
dnsServerIPCh := make(chan net.IP, 1)
dnsServerIPCh <- net.ParseIP("0.0.0.0")

Expand Down
11 changes: 7 additions & 4 deletions pkg/networkservice/common/authorize/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2020-2022 Doc.ai and/or its affiliates.
//
// Copyright (c) 2020-2022 Cisco Systems, Inc.
// Copyright (c) 2020-2023 Cisco Systems, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -18,11 +18,14 @@

package authorize

import "github.com/networkservicemesh/sdk/pkg/tools/spire"
import (
"github.com/edwarnicke/genericsync"
"github.com/spiffe/go-spiffe/v2/spiffeid"
)

type options struct {
policyPaths []string
spiffeIDConnectionMap *spire.SpiffeIDConnectionMap
spiffeIDConnectionMap *genericsync.Map[spiffeid.ID, *genericsync.Map[string, struct{}]]
}

// Option is authorization option for network service server
Expand All @@ -42,7 +45,7 @@ func WithPolicies(policyPaths ...string) Option {
}

// WithSpiffeIDConnectionMap sets map to keep spiffeIDConnectionMap to authorize connections with MonitorConnectionServer
func WithSpiffeIDConnectionMap(s *spire.SpiffeIDConnectionMap) Option {
func WithSpiffeIDConnectionMap(s *genericsync.Map[spiffeid.ID, *genericsync.Map[string, struct{}]]) Option {
return func(o *options) {
o.spiffeIDConnectionMap = s
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/networkservice/common/authorize/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2020-2022 Doc.ai and/or its affiliates.
//
// Copyright (c) 2020-2022 Cisco Systems, Inc.
// Copyright (c) 2020-2023 Cisco Systems, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -22,21 +22,22 @@ package authorize
import (
"context"

"github.com/edwarnicke/genericsync"
"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"github.com/spiffe/go-spiffe/v2/spiffeid"
"google.golang.org/grpc/peer"

"github.com/networkservicemesh/api/pkg/api/networkservice"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/opa"
"github.com/networkservicemesh/sdk/pkg/tools/spire"
"github.com/networkservicemesh/sdk/pkg/tools/stringset"
)

type authorizeServer struct {
policies policiesList
spiffeIDConnectionMap *spire.SpiffeIDConnectionMap
spiffeIDConnectionMap *genericsync.Map[spiffeid.ID, *genericsync.Map[string, struct{}]]
}

// NewServer - returns a new authorization networkservicemesh.NetworkServiceServers
Expand All @@ -47,7 +48,7 @@ func NewServer(opts ...Option) networkservice.NetworkServiceServer {
"etc/nsm/opa/common/.*.rego",
"etc/nsm/opa/server/.*.rego",
},
spiffeIDConnectionMap: &spire.SpiffeIDConnectionMap{},
spiffeIDConnectionMap: &genericsync.Map[spiffeid.ID, *genericsync.Map[string, struct{}]]{},
}
for _, opt := range opts {
opt(o)
Expand Down Expand Up @@ -86,7 +87,7 @@ func (a *authorizeServer) Request(ctx context.Context, request *networkservice.N
connID := conn.GetPath().GetPathSegments()[index-1].GetId()
ids, ok := a.spiffeIDConnectionMap.Load(spiffeID)
if !ok {
ids = new(stringset.StringSet)
ids = new(genericsync.Map[string, struct{}])
}
ids.Store(connID, struct{}{})
a.spiffeIDConnectionMap.Store(spiffeID, ids)
Expand Down
4 changes: 2 additions & 2 deletions pkg/networkservice/connectioncontext/dnscontext/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import (
"os"
"strings"

"github.com/edwarnicke/genericsync"
"github.com/golang/protobuf/ptypes/empty"
"github.com/miekg/dns"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/pkg/errors"
"google.golang.org/grpc"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/dnsconfig"
"github.com/networkservicemesh/sdk/pkg/tools/dnsutils"
"github.com/networkservicemesh/sdk/pkg/tools/log"
)
Expand All @@ -40,7 +40,7 @@ type dnsContextClient struct {
resolveConfigPath string
defaultNameServerIP string
resolvconfDNSConfig *networkservice.DNSConfig
dnsConfigsMap *dnsconfig.Map
dnsConfigsMap *genericsync.Map[string, []*networkservice.DNSConfig]
}

// NewClient creates a new DNS client chain component. Setups all DNS traffic to the localhost. Monitors DNS configs from connections.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
//
// Copyright (c) 2022 Cisco and/or its affiliates.
// Copyright (c) 2022-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -26,14 +26,14 @@ import (
"testing"
"time"

"github.com/edwarnicke/genericsync"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"

"github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/dnscontext"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
"github.com/networkservicemesh/sdk/pkg/tools/dnsconfig"
)

func Test_DNSContextClient_Usecases(t *testing.T) {
Expand All @@ -51,7 +51,7 @@ func Test_DNSContextClient_Usecases(t *testing.T) {
dnscontext.NewClient(
dnscontext.WithChainContext(ctx),
dnscontext.WithResolveConfigPath(resolveConfigPath),
dnscontext.WithDNSConfigsMap(new(dnsconfig.Map)),
dnscontext.WithDNSConfigsMap(new(genericsync.Map[string, []*networkservice.DNSConfig])),
),
)

Expand Down
7 changes: 4 additions & 3 deletions pkg/networkservice/connectioncontext/dnscontext/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
//
// Copyright (c) 2022 Cisco and/or its affiliates.
// Copyright (c) 2022-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -21,7 +21,8 @@ package dnscontext
import (
"context"

"github.com/networkservicemesh/sdk/pkg/tools/dnsconfig"
"github.com/edwarnicke/genericsync"
"github.com/networkservicemesh/api/pkg/api/networkservice"
)

// DNSOption is applying options for DNS client.
Expand Down Expand Up @@ -50,7 +51,7 @@ func WithDefaultNameServerIP(ip string) DNSOption {
}

// WithDNSConfigsMap sets configs map for DNS client.
func WithDNSConfigsMap(configsMap *dnsconfig.Map) DNSOption {
func WithDNSConfigsMap(configsMap *genericsync.Map[string, []*networkservice.DNSConfig]) DNSOption {
return applyFunc(func(c *dnsContextClient) {
c.dnsConfigsMap = configsMap
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Cisco and/or its affiliates.
// Copyright (c) 2022-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -20,23 +20,23 @@ import (
"context"
"net"

"github.com/edwarnicke/genericsync"
"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"google.golang.org/grpc"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/dnsconfig"
)

type vl3DNSClient struct {
dnsServerIP net.IP
dnsConfigs *dnsconfig.Map
dnsConfigs *genericsync.Map[string, []*networkservice.DNSConfig]
}

// NewClient - returns a new null client that does nothing but call next.Client(ctx).{Request/Close} and return the result
//
// This is very useful in testing
func NewClient(dnsServerIP net.IP, dnsConfigs *dnsconfig.Map) networkservice.NetworkServiceClient {
func NewClient(dnsServerIP net.IP, dnsConfigs *genericsync.Map[string, []*networkservice.DNSConfig]) networkservice.NetworkServiceClient {
return &vl3DNSClient{
dnsServerIP: dnsServerIP,
dnsConfigs: dnsConfigs,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Cisco and/or its affiliates.
// Copyright (c) 2022-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -21,15 +21,18 @@ import (
"fmt"
"text/template"

"github.com/networkservicemesh/sdk/pkg/tools/dnsconfig"
"github.com/edwarnicke/genericsync"

"github.com/networkservicemesh/api/pkg/api/networkservice"

"github.com/networkservicemesh/sdk/pkg/tools/dnsutils"
)

// Option configures vl3DNSServer
type Option func(*vl3DNSServer)

// WithConfigs sets initial list to fanout queries
func WithConfigs(m *dnsconfig.Map) Option {
func WithConfigs(m *genericsync.Map[string, []*networkservice.DNSConfig]) Option {
return func(vd *vl3DNSServer) {
vd.dnsConfigs = m
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import (
"sync/atomic"
"text/template"

"github.com/edwarnicke/genericsync"
"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/pkg/errors"

"github.com/networkservicemesh/sdk/pkg/networkservice/common/monitor"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
"github.com/networkservicemesh/sdk/pkg/tools/dnsconfig"
"github.com/networkservicemesh/sdk/pkg/tools/dnsutils"
dnschain "github.com/networkservicemesh/sdk/pkg/tools/dnsutils/chain"
"github.com/networkservicemesh/sdk/pkg/tools/dnsutils/dnsconfigs"
Expand All @@ -47,8 +47,8 @@ import (

type vl3DNSServer struct {
chainCtx context.Context
dnsServerRecords memory.Map
dnsConfigs *dnsconfig.Map
dnsServerRecords genericsync.Map[string, []net.IP]
dnsConfigs *genericsync.Map[string, []*networkservice.DNSConfig]
domainSchemeTemplates []*template.Template
dnsPort int
dnsServer dnsutils.Handler
Expand All @@ -71,7 +71,7 @@ func NewServer(chainCtx context.Context, dnsServerIPCh <-chan net.IP, opts ...Op
chainCtx: chainCtx,
dnsPort: 53,
listenAndServeDNS: dnsutils.ListenAndServe,
dnsConfigs: new(dnsconfig.Map),
dnsConfigs: new(genericsync.Map[string, []*networkservice.DNSConfig]),
dnsServerIPCh: dnsServerIPCh,
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/registry/common/authorize/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package authorize
import (
"context"

"github.com/edwarnicke/genericsync"
"github.com/golang-jwt/jwt/v4"
"github.com/pkg/errors"
"github.com/spiffe/go-spiffe/v2/spiffeid"
Expand Down Expand Up @@ -63,7 +64,7 @@ func (l *policiesList) check(ctx context.Context, input RegistryOpaInput) error
return nil
}

func getRawMap(m *PathIdsMap) map[string][]string {
func getRawMap(m *genericsync.Map[string, []string]) map[string][]string {
rawMap := make(map[string][]string)
m.Range(func(key string, value []string) bool {
rawMap[key] = value
Expand Down
26 changes: 0 additions & 26 deletions pkg/registry/common/authorize/gen.go

This file was deleted.

Loading

0 comments on commit d72942f

Please sign in to comment.