Skip to content

Commit

Permalink
adding stack to errors
Browse files Browse the repository at this point in the history
Signed-off-by: Ruslan Bayandinov <wazsone@ya.ru>
  • Loading branch information
wazsone committed Jan 26, 2023
1 parent 637ce70 commit 65a85f5
Show file tree
Hide file tree
Showing 57 changed files with 253 additions and 181 deletions.
4 changes: 2 additions & 2 deletions pkg/ipam/vl3ipam/server.go
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 Down Expand Up @@ -123,5 +123,5 @@ func (s *vl3IPAMServer) ManagePrefixes(prefixServer ipam.IPAM_ManagePrefixesServ
return nil
}

return err
return errors.WithStack(err)
}
8 changes: 5 additions & 3 deletions pkg/networkservice/chains/endpoint/combine_monitor_server.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2021 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 Down Expand Up @@ -53,7 +55,7 @@ func (m *combineMonitorServer) MonitorConnections(selector *networkservice.Monit
if rv := monitorErr.Load(); rv != nil {
err = rv.(error)
}
return err
return errors.WithStack(err)
}

func startMonitorConnectionsServer(
Expand Down Expand Up @@ -130,10 +132,10 @@ func (m *combineMonitorConnectionsServer) Send(event *networkservice.ConnectionE
m.initCh <- event
err = <-m.errCh
})
return err
return errors.WithStack(err)
default:
m.initWg.Wait()
return m.MonitorConnection_MonitorConnectionsServer.Send(event)
return errors.WithStack(m.MonitorConnection_MonitorConnectionsServer.Send(event))
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/networkservice/common/authorize/client.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 Down Expand Up @@ -94,7 +94,7 @@ func (a *authorizeClient) Request(ctx context.Context, request *networkservice.N
return nil, err
}

return conn, err
return conn, nil
}

func (a *authorizeClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/networkservice/common/cleanup/client.go
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 Down Expand Up @@ -105,7 +105,7 @@ func (c *cleanupClient) Request(ctx context.Context, request *networkservice.Net
atomic.AddInt32(&c.activeConns, -1)
}
}()
return conn, err
return conn, nil
}

func (c *cleanupClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/networkservice/common/connect/client.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2021 Cisco 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 Down Expand Up @@ -39,8 +41,7 @@ func (c *connectClient) Request(ctx context.Context, request *networkservice.Net
if !loaded {
return nil, errors.New("no grpc.ClientConnInterface provided")
}
conn, err := networkservice.NewNetworkServiceClient(cc).Request(ctx, request, opts...)
return conn, err
return networkservice.NewNetworkServiceClient(cc).Request(ctx, request, opts...)
}

func (c *connectClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*emptypb.Empty, error) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/networkservice/common/discover/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2020-2022 Cisco Systems, Inc.
//
// Copyright (c) 2021-2022 Doc.ai and/or its affiliates.
//
// Copyright (c) 2020-2023 Cisco Systems, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -116,7 +116,7 @@ func (d *discoverCandidatesServer) discoverNetworkServiceEndpoint(ctx context.Co

nseRespStream, err := d.nseClient.Find(ctx, query)
if err != nil {
return nil, errors.WithStack(err)
return nil, err
}
nseList := registry.ReadNetworkServiceEndpointList(nseRespStream)

Expand All @@ -140,7 +140,7 @@ func (d *discoverCandidatesServer) discoverNetworkServiceEndpoints(ctx context.C

nseRespStream, err := d.nseClient.Find(ctx, query)
if err != nil {
return nil, errors.WithStack(err)
return nil, err
}
nseList := registry.ReadNetworkServiceEndpointList(nseRespStream)

Expand All @@ -162,7 +162,7 @@ func (d *discoverCandidatesServer) discoverNetworkService(ctx context.Context, n

nsRespStream, err := d.nsClient.Find(ctx, query)
if err != nil {
return nil, errors.WithStack(err)
return nil, err
}
nsList := registry.ReadNetworkServiceList(nsRespStream)

Expand Down
25 changes: 8 additions & 17 deletions pkg/networkservice/common/discoverforwarder/server.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2021-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 Down Expand Up @@ -81,14 +83,12 @@ func (d *discoverForwarderServer) Request(ctx context.Context, request *networks
Url: d.nsmgrURL,
},
})

if err != nil {
logger.Errorf("can not open registry nse stream by networkservice. Error: %v", err.Error())
return nil, errors.WithStack(err)
return nil, err
}

nses := d.matchForwarders(request.Connection.GetLabels(), ns, registry.ReadNetworkServiceEndpointList(stream))

if len(nses) == 0 {
return nil, errors.New("no candidates found")
}
Expand All @@ -110,14 +110,12 @@ func (d *discoverForwarderServer) Request(ctx context.Context, request *networks
// https://github.com/networkservicemesh/sdk/issues/790
for i, candidate := range nses {
u, err := url.Parse(candidate.Url)

if err != nil {
logger.Errorf("can not parse forwarder=%v url=%v error=%v", candidate.Name, candidate.Url, err.Error())
continue
}

resp, err := next.Server(ctx).Request(clienturlctx.WithClientURL(ctx, u), request.Clone())

if err == nil {
storeForwarderName(ctx, candidate.Name)
return resp, nil
Expand All @@ -128,27 +126,25 @@ func (d *discoverForwarderServer) Request(ctx context.Context, request *networks

return nil, candidatesErr
}

stream, err := d.nseClient.Find(ctx, &registry.NetworkServiceEndpointQuery{
NetworkServiceEndpoint: &registry.NetworkServiceEndpoint{
Name: forwarderName,
Url: d.nsmgrURL,
},
})

if err != nil {
logger.Errorf("can not open registry nse stream by forwarder name. Error: %v", err.Error())
return nil, errors.WithStack(err)
return nil, err
}

nses := registry.ReadNetworkServiceEndpointList(stream)

if len(nses) == 0 {
storeForwarderName(ctx, "")
return nil, errors.New("forwarder not found")
}

u, err := url.Parse(nses[0].Url)

if err != nil {
logger.Errorf("can not parse forwarder=%v url=%v error=%v", nses[0].Name, u, err.Error())
return nil, errors.WithStack(err)
Expand All @@ -164,7 +160,6 @@ func (d *discoverForwarderServer) Request(ctx context.Context, request *networks
func (d *discoverForwarderServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
var forwarderName = loadForwarderName(ctx)
var logger = log.FromContext(ctx).WithField("discoverForwarderServer", "request")

if forwarderName == "" {
return nil, errors.New("forwarder is not selected")
}
Expand All @@ -175,20 +170,17 @@ func (d *discoverForwarderServer) Close(ctx context.Context, conn *networkservic
Url: d.nsmgrURL,
},
})

if err != nil {
logger.Errorf("can not open registry nse stream by forwarder name. Error: %v", err.Error())
return nil, errors.WithStack(err)
return nil, err
}

nses := registry.ReadNetworkServiceEndpointList(stream)

if len(nses) == 0 {
return nil, errors.New("forwarder not found")
}

u, err := url.Parse(nses[0].Url)

if err != nil {
logger.Errorf("can not parse forwarder url %v", err.Error())
return nil, errors.WithStack(err)
Expand All @@ -211,7 +203,6 @@ func (d *discoverForwarderServer) matchForwarders(nsLabels map[string]string, ns
}

var matchLabels = match.GetMetadata().GetLabels()

if matchLabels == nil {
matchLabels = map[string]string{
"p2p": "true",
Expand Down Expand Up @@ -247,10 +238,10 @@ func (d *discoverForwarderServer) discoverNetworkService(ctx context.Context, na

nsRespStream, err := d.nsClient.Find(ctx, query)
if err != nil {
return nil, errors.WithStack(err)
return nil, err
}
nsList := registry.ReadNetworkServiceList(nsRespStream)

nsList := registry.ReadNetworkServiceList(nsRespStream)
for _, ns := range nsList {
if ns.Name == name {
return ns, nil
Expand Down
8 changes: 5 additions & 3 deletions pkg/networkservice/common/excludedprefixes/client.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2021-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 Down Expand Up @@ -158,7 +160,7 @@ func (epc *excludedPrefixesClient) Request(ctx context.Context, request *network

ipCtx.ExcludedPrefixes = oldExcludedPrefixes

return resp, err
return resp, nil
}

func (epc *excludedPrefixesClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
Expand Down Expand Up @@ -207,7 +209,7 @@ func validateIPs(ipContext *networkservice.IPContext, excludedPrefixes []string)
for _, prefix := range excludedPrefixes {
_, ipNet, err := net.ParseCIDR(prefix)
if err != nil {
return err
return errors.WithStack(err)
}
// TODO: Think about validating routes with prefixes size less than /32 and /128
if prefixLen, maxLen := ipNet.Mask.Size(); prefixLen != maxLen {
Expand All @@ -225,7 +227,7 @@ func validateIPs(ipContext *networkservice.IPContext, excludedPrefixes []string)
for _, prefix := range prefixes {
ip, _, err := net.ParseCIDR(prefix)
if err != nil {
return err
return errors.WithStack(err)
}

if ip4Pool.Contains(ip) || ip6Pool.Contains(ip) {
Expand Down
8 changes: 5 additions & 3 deletions pkg/networkservice/common/journal/server.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-2021 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 @@ -22,13 +24,13 @@ package journal
import (
"context"
"encoding/json"
"errors"
"strings"
"time"

"github.com/golang/protobuf/ptypes/empty"
stan "github.com/nats-io/stan.go"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/pkg/errors"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/clock"
Expand Down Expand Up @@ -101,12 +103,12 @@ func (srv *journalServer) Close(ctx context.Context, connection *networkservice.
func (srv *journalServer) publish(entry *Entry) error {
js, err := json.Marshal(entry)
if err != nil {
return err
return errors.WithStack(err)
}

err = srv.nats.Publish(srv.journalID, js)
if err != nil {
return err
return errors.WithStack(err)
}
return nil
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/networkservice/connectioncontext/dnscontext/client.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 @@ -26,6 +26,7 @@ import (
"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"
Expand Down Expand Up @@ -87,7 +88,7 @@ func (c *dnsContextClient) Request(ctx context.Context, request *networkservice.

c.dnsConfigsMap.Store(rv.Id, configs)

return rv, err
return rv, nil
}

func (c *dnsContextClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
Expand Down Expand Up @@ -136,11 +137,11 @@ func (c *dnsContextClient) storeOriginalResolvConf() {
func (c *dnsContextClient) appendResolvConf(resolvConf string) error {
bytes, err := os.ReadFile(c.resolveConfigPath)
if err != nil {
return err
return errors.WithStack(err)
}
originalResolvConfig := string(bytes)

return os.WriteFile(c.resolveConfigPath, []byte(originalResolvConfig+resolvConf), os.ModePerm)
return errors.WithStack(os.WriteFile(c.resolveConfigPath, []byte(originalResolvConfig+resolvConf), os.ModePerm))
}

func (c *dnsContextClient) initialize() {
Expand Down
6 changes: 4 additions & 2 deletions pkg/networkservice/connectioncontext/dnscontext/resolvconf.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) 2022 Cisco and/or its affiliates.
// Copyright (c) 2022-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -21,6 +21,8 @@ package dnscontext
import (
"io/ioutil"
"strings"

"github.com/pkg/errors"
)

// resolveConfig provides API for editing / reading resolv.conf
Expand All @@ -44,7 +46,7 @@ func openResolveConfig(p string) (*resolveConfig, error) {
func (r *resolveConfig) readProperties() error {
b, err := ioutil.ReadFile(r.path)
if err != nil {
return err
return errors.Wrapf(err, "failed to read resolv.conf file: %s", r.path)
}
for _, l := range strings.Split(string(b), "\n") {
if !strings.HasPrefix(l, "#") {
Expand Down
Loading

0 comments on commit 65a85f5

Please sign in to comment.