Skip to content

Commit

Permalink
refactored setlogoption chain element to not use adapters
Browse files Browse the repository at this point in the history
Signed-off-by: Mikhail Avramenko <[email protected]>
  • Loading branch information
Mixaster995 committed Nov 12, 2021
1 parent d801027 commit 58199f6
Show file tree
Hide file tree
Showing 13 changed files with 233 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ linters-settings:
dupl:
threshold: 150
funlen:
Lines: 100
Lines: 105
Statements: 50
goconst:
min-len: 2
Expand Down
3 changes: 1 addition & 2 deletions pkg/networkservice/chains/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/networkservicemesh/sdk/pkg/networkservice/common/refresh"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/setlogoption"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/updatepath"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/adapters"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
)
Expand All @@ -55,7 +54,7 @@ func NewClient(ctx context.Context, clientOpts ...Option) networkservice.Network
return chain.NewNetworkServiceClient(
append(
[]networkservice.NetworkServiceClient{
adapters.NewServerToClient(setlogoption.NewServer(map[string]string{"name": opts.name})),
setlogoption.NewClient(map[string]string{"name": opts.name}),
updatepath.NewClient(opts.name),
begin.NewClient(),
metadata.NewClient(),
Expand Down
5 changes: 2 additions & 3 deletions pkg/networkservice/chains/nsmgr/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package nsmgr

import (
"context"
"fmt"
"net/url"
"time"

Expand Down Expand Up @@ -168,15 +167,15 @@ func NewServer(ctx context.Context, tokenGenerator token.GeneratorFunc, options
}

nsRegistry = registrychain.NewNetworkServiceRegistryServer(
setlogoption.NewNetworkServiceRegistryServer(map[string]string{"name": opts.name}),
registryserialize.NewNetworkServiceRegistryServer(),
setlogoption.NewNetworkServiceRegistryServer(map[string]string{"name": "NetworkServiceRegistryServer." + opts.name}),
nsRegistry,
)

var nseInMemoryRegistry = memory.NewNetworkServiceEndpointRegistryServer()

var nseRegistry = registrychain.NewNetworkServiceEndpointRegistryServer(
setlogoption.NewNetworkServiceEndpointRegistryServer(map[string]string{"name": fmt.Sprintf("NetworkServiceRegistryServer.%v", opts.name)}),
setlogoption.NewNetworkServiceEndpointRegistryServer(map[string]string{"name": opts.name}),
registryclientinfo.NewNetworkServiceEndpointRegistryServer(),
registryserialize.NewNetworkServiceEndpointRegistryServer(),
expire.NewNetworkServiceEndpointRegistryServer(ctx, time.Minute),
Expand Down
67 changes: 67 additions & 0 deletions pkg/networkservice/common/setlogoption/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 setlogoption

import (
"context"

"google.golang.org/grpc"

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

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

type setLogOptionClient struct {
options map[string]string
}

// NewClient - construct a new set log option server to override some logging capabilities for context.
func NewClient(options map[string]string) networkservice.NetworkServiceClient {
return &setLogOptionClient{
options: options,
}
}

func (s *setLogOptionClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) {
ctx = s.withFields(ctx)
return next.Client(ctx).Request(ctx, request, opts...)
}

func (s *setLogOptionClient) withFields(ctx context.Context) context.Context {
ctxFields := log.Fields(ctx)
fields := make(map[string]interface{})
for k, v := range ctxFields {
fields[k] = v
}

fields["type"] = networkService
for k, v := range s.options {
fields[k] = v
}
if len(fields) > 0 {
ctx = log.WithFields(ctx, fields)
}
return ctx
}

func (s *setLogOptionClient) Close(ctx context.Context, connection *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
ctx = s.withFields(ctx)
return next.Client(ctx).Close(ctx, connection, opts...)
}
21 changes: 21 additions & 0 deletions pkg/networkservice/common/setlogoption/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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 setlogoption

const (
networkService = "NetworkService"
)
2 changes: 1 addition & 1 deletion pkg/networkservice/common/setlogoption/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (s *setLogOptionServer) withFields(ctx context.Context) context.Context {
fields[k] = v
}

fields["type"] = "NetworkService"
fields["type"] = networkService
for k, v := range s.options {
fields[k] = v
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/registry/chains/client/ns_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/networkservicemesh/sdk/pkg/registry/common/connect"
"github.com/networkservicemesh/sdk/pkg/registry/common/heal"
"github.com/networkservicemesh/sdk/pkg/registry/common/setlogoption"
"github.com/networkservicemesh/sdk/pkg/registry/core/adapters"
"github.com/networkservicemesh/sdk/pkg/registry/core/chain"
)

Expand All @@ -38,7 +37,7 @@ func NewNetworkServiceRegistryClient(ctx context.Context, connectTo *url.URL, op

c := new(registry.NetworkServiceRegistryClient)
*c = chain.NewNetworkServiceRegistryClient(
adapters.NetworkServiceServerToClient(setlogoption.NewNetworkServiceRegistryServer(map[string]string{})),
setlogoption.NewNetworkServiceRegistryClient(map[string]string{}),
connect.NewNetworkServiceRegistryClient(ctx, connectTo,
connect.WithNSAdditionalFunctionality(
append(
Expand Down
3 changes: 1 addition & 2 deletions pkg/registry/chains/client/nse_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/networkservicemesh/sdk/pkg/registry/common/sendfd"
"github.com/networkservicemesh/sdk/pkg/registry/common/serialize"
"github.com/networkservicemesh/sdk/pkg/registry/common/setlogoption"
"github.com/networkservicemesh/sdk/pkg/registry/core/adapters"
"github.com/networkservicemesh/sdk/pkg/registry/core/chain"
)

Expand All @@ -42,7 +41,7 @@ func NewNetworkServiceEndpointRegistryClient(ctx context.Context, connectTo *url

c := new(registry.NetworkServiceEndpointRegistryClient)
*c = chain.NewNetworkServiceEndpointRegistryClient(
adapters.NetworkServiceEndpointServerToClient(setlogoption.NewNetworkServiceEndpointRegistryServer(map[string]string{})),
setlogoption.NewNetworkServiceEndpointRegistryClient(map[string]string{}),
serialize.NewNetworkServiceEndpointRegistryClient(),
refresh.NewNetworkServiceEndpointRegistryClient(ctx),
connect.NewNetworkServiceEndpointRegistryClient(ctx, connectTo,
Expand Down
15 changes: 13 additions & 2 deletions pkg/registry/common/heal/find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ func TestHealClient_FindTest(t *testing.T) {
nsmgrCtx, nsmgrCancel := context.WithCancel(ctx)
defer nsmgrCancel()

fwdName := sandbox.UniqueName("forwarder")
domain := sandbox.NewBuilder(ctx, t).
SetRegistryProxySupplier(nil).
SetNSMgrProxySupplier(nil).
SetNodeSetup(func(ctx context.Context, node *sandbox.Node, nodeNum int) {
node.NewNSMgr(nsmgrCtx, sandbox.UniqueName("nsmgr"), nil, sandbox.GenerateTestToken, nsmgr.NewServer)
node.NewForwarder(ctx, &registry.NetworkServiceEndpoint{
Name: sandbox.UniqueName("forwarder"),
Name: fwdName,
NetworkServiceNames: []string{"forwarder"},
NetworkServiceLabels: map[string]*registry.NetworkServiceLabels{
"forwarder": {
Expand Down Expand Up @@ -107,9 +108,19 @@ func TestHealClient_FindTest(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "ns", nsResp.NetworkService.Name)

m := map[string]struct{}{
"nse": {},
fwdName: {},
}

nseResp, err := nseRespStream.Recv()
require.NoError(t, err)
require.Equal(t, "nse", nseResp.NetworkServiceEndpoint.Name)
require.Contains(t, m, nseResp.NetworkServiceEndpoint.Name)
delete(m, nseResp.NetworkServiceEndpoint.Name)

nseResp, err = nseRespStream.Recv()
require.NoError(t, err)
require.Contains(t, m, nseResp.NetworkServiceEndpoint.Name)

// 5. Close NS, NSE streams
findCancel()
Expand Down
58 changes: 58 additions & 0 deletions pkg/registry/common/setlogoption/ns_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2020-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 setlogoption

import (
"context"

"google.golang.org/grpc"

"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/registry"

"github.com/networkservicemesh/sdk/pkg/registry/core/next"
)

const (
nsRegistryClient = "NetworkServiceRegistryClient"
)

type setNSLogOptionClient struct {
options map[string]string
}

func (s *setNSLogOptionClient) Register(ctx context.Context, ns *registry.NetworkService, opts ...grpc.CallOption) (*registry.NetworkService, error) {
ctx = withFields(ctx, s.options, nsRegistryClient)
return next.NetworkServiceRegistryClient(ctx).Register(ctx, ns, opts...)
}

func (s *setNSLogOptionClient) Find(ctx context.Context, query *registry.NetworkServiceQuery, opts ...grpc.CallOption) (registry.NetworkServiceRegistry_FindClient, error) {
ctx = withFields(ctx, s.options, nsRegistryClient)
return next.NetworkServiceRegistryClient(ctx).Find(ctx, query, opts...)
}

func (s *setNSLogOptionClient) Unregister(ctx context.Context, ns *registry.NetworkService, opts ...grpc.CallOption) (*empty.Empty, error) {
ctx = withFields(ctx, s.options, nsRegistryClient)
return next.NetworkServiceRegistryClient(ctx).Unregister(ctx, ns, opts...)
}

// NewNetworkServiceRegistryClient creates new instance of NetworkServiceRegistryClient which sets the passed options
func NewNetworkServiceRegistryClient(options map[string]string) registry.NetworkServiceRegistryClient {
return &setNSLogOptionClient{
options: options,
}
}
8 changes: 4 additions & 4 deletions pkg/registry/common/setlogoption/ns_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

const (
registryType = "NetworkServiceRegistry"
nsRegistryServer = "NetworkServiceRegistryServer"
)

type setNSLogOption struct {
Expand All @@ -47,17 +47,17 @@ func (s *setLogOptionFindServer) Context() context.Context {
}

func (s *setNSLogOption) Register(ctx context.Context, ns *registry.NetworkService) (*registry.NetworkService, error) {
ctx = withFields(ctx, s.options, registryType)
ctx = withFields(ctx, s.options, nsRegistryServer)
return next.NetworkServiceRegistryServer(ctx).Register(ctx, ns)
}

func (s *setNSLogOption) Find(query *registry.NetworkServiceQuery, server registry.NetworkServiceRegistry_FindServer) error {
ctx := withFields(server.Context(), s.options, registryType)
ctx := withFields(server.Context(), s.options, nsRegistryServer)
return next.NetworkServiceRegistryServer(ctx).Find(query, &setLogOptionFindServer{ctx: ctx, NetworkServiceRegistry_FindServer: server})
}

func (s *setNSLogOption) Unregister(ctx context.Context, ns *registry.NetworkService) (*empty.Empty, error) {
ctx = withFields(ctx, s.options, registryType)
ctx = withFields(ctx, s.options, nsRegistryServer)
return next.NetworkServiceRegistryServer(ctx).Unregister(ctx, ns)
}

Expand Down
59 changes: 59 additions & 0 deletions pkg/registry/common/setlogoption/nse_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2020-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 setlogoption implements a chain element to set log options before full chain
package setlogoption

import (
"context"

"google.golang.org/grpc"

"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/registry"

"github.com/networkservicemesh/sdk/pkg/registry/core/next"
)

const (
nseRegistryClient = "NetworkServiceEndpointRegistryClient"
)

type setNSELogOptionClient struct {
options map[string]string
}

func (s *setNSELogOptionClient) Register(ctx context.Context, endpoint *registry.NetworkServiceEndpoint, opts ...grpc.CallOption) (*registry.NetworkServiceEndpoint, error) {
ctx = withFields(ctx, s.options, nseRegistryClient)
return next.NetworkServiceEndpointRegistryClient(ctx).Register(ctx, endpoint, opts...)
}

func (s *setNSELogOptionClient) Find(ctx context.Context, query *registry.NetworkServiceEndpointQuery, opts ...grpc.CallOption) (registry.NetworkServiceEndpointRegistry_FindClient, error) {
ctx = withFields(ctx, s.options, nseRegistryClient)
return next.NetworkServiceEndpointRegistryClient(ctx).Find(ctx, query, opts...)
}

func (s *setNSELogOptionClient) Unregister(ctx context.Context, endpoint *registry.NetworkServiceEndpoint, opts ...grpc.CallOption) (*empty.Empty, error) {
ctx = withFields(ctx, s.options, nseRegistryClient)
return next.NetworkServiceEndpointRegistryClient(ctx).Unregister(ctx, endpoint, opts...)
}

// NewNetworkServiceEndpointRegistryClient creates new instance of NetworkServiceEndpointRegistryClient which sets the passed options
func NewNetworkServiceEndpointRegistryClient(options map[string]string) registry.NetworkServiceEndpointRegistryClient {
return &setNSELogOptionClient{
options: options,
}
}
Loading

0 comments on commit 58199f6

Please sign in to comment.