diff --git a/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/ipaddress/client.go b/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/ipaddress/client.go index 8e2523f6..2a0164a2 100644 --- a/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/ipaddress/client.go +++ b/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/ipaddress/client.go @@ -2,6 +2,8 @@ // // Copyright (c) 2021 Nordix Foundation. // +// 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"); @@ -24,9 +26,12 @@ import ( "context" "github.com/golang/protobuf/ptypes/empty" + "github.com/pkg/errors" + "google.golang.org/grpc" + "github.com/networkservicemesh/api/pkg/api/networkservice" "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" - "google.golang.org/grpc" + "github.com/networkservicemesh/sdk/pkg/tools/closectx" "github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata" ) @@ -65,10 +70,18 @@ func (i *ipaddressClient) Request(ctx context.Context, request *networkservice.N if err != nil { return nil, err } + if err := create(ctx, conn, metadata.IsClient(i)); err != nil { - _, _ = i.Close(ctx, conn, opts...) + closeCtx, cancelClose := closectx.New(ctx) + defer cancelClose() + + if _, closeErr := i.Close(closeCtx, conn, opts...); closeErr != nil { + err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error()) + } + return nil, err } + return conn, nil } diff --git a/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/ipaddress/server.go b/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/ipaddress/server.go index 9f531df7..6d807481 100644 --- a/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/ipaddress/server.go +++ b/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/ipaddress/server.go @@ -2,6 +2,8 @@ // // Copyright (c) 2021 Nordix Foundation. // +// 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"); @@ -24,8 +26,11 @@ import ( "context" "github.com/golang/protobuf/ptypes/empty" + "github.com/pkg/errors" + "github.com/networkservicemesh/api/pkg/api/networkservice" "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" + "github.com/networkservicemesh/sdk/pkg/tools/closectx" "github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata" ) @@ -65,10 +70,18 @@ func (i *ipaddressServer) Request(ctx context.Context, request *networkservice.N if err != nil { return nil, err } + if err := create(ctx, conn, metadata.IsClient(i)); err != nil { - _, _ = i.Close(ctx, conn) + closeCtx, cancelClose := closectx.New(ctx) + defer cancelClose() + + if _, closeErr := i.Close(closeCtx, conn); closeErr != nil { + err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error()) + } + return nil, err } + return conn, nil } diff --git a/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/ipneighbors/client.go b/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/ipneighbors/client.go index 056e0e2c..e55853f9 100644 --- a/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/ipneighbors/client.go +++ b/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/ipneighbors/client.go @@ -1,5 +1,7 @@ // Copyright (c) 2021 Nordix Foundation. // +// 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"); @@ -22,9 +24,12 @@ import ( "context" "github.com/golang/protobuf/ptypes/empty" + "github.com/pkg/errors" + "google.golang.org/grpc" + "github.com/networkservicemesh/api/pkg/api/networkservice" "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" - "google.golang.org/grpc" + "github.com/networkservicemesh/sdk/pkg/tools/closectx" ) type ipNeighborsClient struct{} @@ -38,10 +43,18 @@ func (i *ipNeighborsClient) Request(ctx context.Context, request *networkservice if err != nil { return nil, err } + if err := create(ctx, conn); err != nil { - _, _ = i.Close(ctx, conn, opts...) + closeCtx, cancelClose := closectx.New(ctx) + defer cancelClose() + + if _, closeErr := i.Close(closeCtx, conn, opts...); closeErr != nil { + err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error()) + } + return nil, err } + return conn, nil } diff --git a/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/ipneighbors/server.go b/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/ipneighbors/server.go index e90832f3..b9a91fe3 100644 --- a/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/ipneighbors/server.go +++ b/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/ipneighbors/server.go @@ -1,5 +1,7 @@ // Copyright (c) 2021 Nordix Foundation. // +// 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"); @@ -22,8 +24,11 @@ import ( "context" "github.com/golang/protobuf/ptypes/empty" + "github.com/pkg/errors" + "github.com/networkservicemesh/api/pkg/api/networkservice" "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" + "github.com/networkservicemesh/sdk/pkg/tools/closectx" ) type ipNeighborsServer struct { @@ -38,10 +43,18 @@ func (i *ipNeighborsServer) Request(ctx context.Context, request *networkservice if err != nil { return nil, err } + if err := create(ctx, conn); err != nil { - _, _ = i.Close(ctx, conn) + closeCtx, cancelClose := closectx.New(ctx) + defer cancelClose() + + if _, closeErr := i.Close(closeCtx, conn); closeErr != nil { + err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error()) + } + return nil, err } + return conn, nil } diff --git a/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/routes/client.go b/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/routes/client.go index dad49eb4..c96ea376 100644 --- a/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/routes/client.go +++ b/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/routes/client.go @@ -2,6 +2,8 @@ // // Copyright (c) 2021 Nordix Foundation. // +// 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"); @@ -24,11 +26,13 @@ import ( "context" "github.com/golang/protobuf/ptypes/empty" - "github.com/networkservicemesh/api/pkg/api/networkservice" - "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" + "github.com/pkg/errors" "google.golang.org/grpc" + "github.com/networkservicemesh/api/pkg/api/networkservice" + "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" "github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata" + "github.com/networkservicemesh/sdk/pkg/tools/closectx" ) type routesClient struct{} @@ -64,10 +68,18 @@ func (i *routesClient) Request(ctx context.Context, request *networkservice.Netw if err != nil { return nil, err } + if err := create(ctx, conn, metadata.IsClient(i)); err != nil { - _, _ = i.Close(ctx, conn, opts...) + closeCtx, cancelClose := closectx.New(ctx) + defer cancelClose() + + if _, closeErr := i.Close(closeCtx, conn, opts...); closeErr != nil { + err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error()) + } + return nil, err } + return conn, nil } diff --git a/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/routes/server.go b/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/routes/server.go index 328fae73..65705ebe 100644 --- a/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/routes/server.go +++ b/pkg/kernel/networkservice/connectioncontextkernel/ipcontext/routes/server.go @@ -2,6 +2,8 @@ // // Copyright (c) 2021 Nordix Foundation. // +// 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"); @@ -24,10 +26,12 @@ import ( "context" "github.com/golang/protobuf/ptypes/empty" + "github.com/pkg/errors" + "github.com/networkservicemesh/api/pkg/api/networkservice" "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" - "github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata" + "github.com/networkservicemesh/sdk/pkg/tools/closectx" ) type routesServer struct { @@ -64,10 +68,18 @@ func (i *routesServer) Request(ctx context.Context, request *networkservice.Netw if err != nil { return nil, err } + if err := create(ctx, conn, metadata.IsClient(i)); err != nil { - _, _ = i.Close(ctx, conn) + closeCtx, cancelClose := closectx.New(ctx) + defer cancelClose() + + if _, closeErr := i.Close(closeCtx, conn); closeErr != nil { + err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error()) + } + return nil, err } + return conn, nil } diff --git a/pkg/kernel/networkservice/connectioncontextkernel/mtu/client.go b/pkg/kernel/networkservice/connectioncontextkernel/mtu/client.go index f09d9b86..39b10243 100644 --- a/pkg/kernel/networkservice/connectioncontextkernel/mtu/client.go +++ b/pkg/kernel/networkservice/connectioncontextkernel/mtu/client.go @@ -2,6 +2,8 @@ // // Copyright (c) 2021 Nordix Foundation. // +// 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"); @@ -24,10 +26,13 @@ import ( "context" "github.com/golang/protobuf/ptypes/empty" + "github.com/pkg/errors" + "google.golang.org/grpc" + "github.com/networkservicemesh/api/pkg/api/networkservice" "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" + "github.com/networkservicemesh/sdk/pkg/tools/closectx" "github.com/networkservicemesh/sdk/pkg/tools/log" - "google.golang.org/grpc" ) type mtuClient struct{} @@ -60,15 +65,26 @@ func NewClient() networkservice.NetworkServiceClient { } func (m *mtuClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) { + logger := log.FromContext(ctx).WithField("mtuClient", "Request") + conn, err := next.Client(ctx).Request(ctx, request, opts...) if err != nil { return nil, err } + if err := setMTU(ctx, conn); err != nil { - log.FromContext(ctx).Debugf("about to Close due to error: %+v", err) - _, _ = m.Close(ctx, conn, opts...) + logger.Debugf("about to Close due to error: %s", err.Error()) + + closeCtx, cancelClose := closectx.New(ctx) + defer cancelClose() + + if _, closeErr := m.Close(closeCtx, conn, opts...); closeErr != nil { + err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error()) + } + return nil, err } + return conn, nil } diff --git a/pkg/kernel/networkservice/connectioncontextkernel/mtu/server.go b/pkg/kernel/networkservice/connectioncontextkernel/mtu/server.go index 7034f7d4..9d01f0a2 100644 --- a/pkg/kernel/networkservice/connectioncontextkernel/mtu/server.go +++ b/pkg/kernel/networkservice/connectioncontextkernel/mtu/server.go @@ -2,6 +2,8 @@ // // Copyright (c) 2021 Nordix Foundation. // +// 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"); @@ -24,8 +26,11 @@ import ( "context" "github.com/golang/protobuf/ptypes/empty" + "github.com/pkg/errors" + "github.com/networkservicemesh/api/pkg/api/networkservice" "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" + "github.com/networkservicemesh/sdk/pkg/tools/closectx" "github.com/networkservicemesh/sdk/pkg/tools/log" ) @@ -60,13 +65,23 @@ func NewServer() networkservice.NetworkServiceServer { } func (m *mtuServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) { + logger := log.FromContext(ctx).WithField("mutServer", "Request") + conn, err := next.Server(ctx).Request(ctx, request) if err != nil { return nil, err } + if err := setMTU(ctx, conn); err != nil { - log.FromContext(ctx).Debugf("about to Close due to error: %+v", err) - _, _ = m.Close(ctx, conn) + logger.Debugf("about to Close due to error: %s", err.Error()) + + closeCtx, cancelClose := closectx.New(ctx) + defer cancelClose() + + if _, closeErr := m.Close(closeCtx, conn); closeErr != nil { + err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error()) + } + return nil, err } return conn, nil diff --git a/pkg/kernel/networkservice/ethernetcontext/vf_client.go b/pkg/kernel/networkservice/ethernetcontext/vf_client.go index 828116ff..560ad22a 100644 --- a/pkg/kernel/networkservice/ethernetcontext/vf_client.go +++ b/pkg/kernel/networkservice/ethernetcontext/vf_client.go @@ -1,5 +1,7 @@ // Copyright (c) 2021 Nordix Foundation. // +// 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"); @@ -20,9 +22,12 @@ import ( "context" "github.com/golang/protobuf/ptypes/empty" + "github.com/pkg/errors" + "google.golang.org/grpc" + "github.com/networkservicemesh/api/pkg/api/networkservice" "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" - "google.golang.org/grpc" + "github.com/networkservicemesh/sdk/pkg/tools/closectx" "github.com/networkservicemesh/sdk-kernel/pkg/kernel/networkservice/vfconfig" ) @@ -39,12 +44,20 @@ func (i *vfEthernetClient) Request(ctx context.Context, request *networkservice. if err != nil { return nil, err } + if vfConfig := vfconfig.Config(ctx); vfConfig != nil { - err := vfCreate(vfConfig, conn, true) - if err != nil { + if err := vfCreate(vfConfig, conn, true); err != nil { + closeCtx, cancelClose := closectx.New(ctx) + defer cancelClose() + + if _, closeErr := i.Close(closeCtx, conn, opts...); closeErr != nil { + err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error()) + } + return nil, err } } + return conn, nil }