-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add replaceNSEName chain element #1328
Merged
denis-tingaikin
merged 2 commits into
networkservicemesh:main
from
glazychev-art:replace_chain_elements
Jul 20, 2022
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// Copyright (c) 2021 Doc.ai and/or its affiliates. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we return this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, done |
||
// Copyright (c) 2022 Cisco and/or its affiliates. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
@@ -29,45 +29,30 @@ import ( | |
) | ||
|
||
type replaceLabelsClient struct { | ||
labels map[string]string | ||
oldConnLabels map[string]string | ||
oldNseName string | ||
labels map[string]string | ||
} | ||
|
||
func (s *replaceLabelsClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (conn *networkservice.Connection, err error) { | ||
s.oldConnLabels = request.Connection.Labels | ||
s.oldNseName = request.Connection.NetworkServiceEndpointName | ||
// NewClient creates new instance of NetworkServiceClient chain element, which replaces labels in the connection | ||
func NewClient(labels map[string]string) networkservice.NetworkServiceClient { | ||
return &replaceLabelsClient{ | ||
labels: labels, | ||
} | ||
} | ||
|
||
func (s *replaceLabelsClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (conn *networkservice.Connection, err error) { | ||
prevConnLabels := request.Connection.Labels | ||
request.Connection.Labels = s.labels | ||
request.Connection.NetworkServiceEndpointName = "" | ||
|
||
conn, err = next.Client(ctx).Request(ctx, request, opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
conn.Labels = s.oldConnLabels | ||
conn.NetworkServiceEndpointName = s.oldNseName | ||
conn.Labels = prevConnLabels | ||
|
||
return conn, nil | ||
} | ||
|
||
func (s *replaceLabelsClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) { | ||
s.oldConnLabels = conn.Labels | ||
s.oldNseName = conn.NetworkServiceEndpointName | ||
|
||
rv, err := next.Client(ctx).Close(ctx, conn, opts...) | ||
|
||
conn.Labels = s.oldConnLabels | ||
conn.NetworkServiceEndpointName = s.oldNseName | ||
|
||
return rv, err | ||
} | ||
|
||
// NewClient creates new instance of NetworkServiceClient chain element, which replaces labels in the connection | ||
func NewClient(labels map[string]string) networkservice.NetworkServiceClient { | ||
return &replaceLabelsClient{ | ||
labels: labels, | ||
oldConnLabels: make(map[string]string), | ||
} | ||
conn.Labels = s.labels | ||
return next.Client(ctx).Close(ctx, conn, opts...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) 2022 Cisco 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 replacelabels_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"go.uber.org/goleak" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/networkservice/common/replacelabels" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkclose" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkrequest" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata" | ||
) | ||
|
||
func TestClient(t *testing.T) { | ||
t.Cleanup(func() { goleak.VerifyNone(t) }) | ||
|
||
chainLabels := map[string]string{"1": "A", "2": "B"} | ||
client := chain.NewNetworkServiceClient( | ||
metadata.NewClient(), | ||
replacelabels.NewClient(chainLabels), | ||
checkrequest.NewClient(t, func(t *testing.T, r *networkservice.NetworkServiceRequest) { | ||
require.Equal(t, chainLabels, r.Connection.Labels) | ||
}), | ||
checkclose.NewClient(t, func(t *testing.T, c *networkservice.Connection) { | ||
require.Equal(t, chainLabels, c.Labels) | ||
}), | ||
) | ||
|
||
// Create the request with any labels | ||
req := &networkservice.NetworkServiceRequest{ | ||
Connection: &networkservice.Connection{Id: "nsc-1", Labels: map[string]string{"3": "C"}}, | ||
} | ||
conn, err := client.Request(context.Background(), req) | ||
require.NoError(t, err) | ||
require.Equal(t, map[string]string{"3": "C"}, conn.Labels) | ||
|
||
_, err = client.Close(context.Background(), conn) | ||
require.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) 2022 Cisco 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 replacensename replaces NetworkServiceEndpointName if it was discovered before | ||
package replacensename | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/golang/protobuf/ptypes/empty" | ||
"google.golang.org/grpc" | ||
|
||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next" | ||
) | ||
|
||
type replaceNSEClient struct{} | ||
|
||
// NewClient creates new instance of NetworkServiceClient chain element, which replaces NetworkServiceEndpointName in the connection | ||
func NewClient() networkservice.NetworkServiceClient { | ||
return &replaceNSEClient{} | ||
} | ||
|
||
func (s *replaceNSEClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (conn *networkservice.Connection, err error) { | ||
prevNseName := request.Connection.NetworkServiceEndpointName | ||
request.Connection.NetworkServiceEndpointName, _ = load(ctx) | ||
|
||
conn, err = next.Client(ctx).Request(ctx, request, opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
store(ctx, conn.NetworkServiceEndpointName) | ||
conn.NetworkServiceEndpointName = prevNseName | ||
|
||
return conn, nil | ||
} | ||
|
||
func (s *replaceNSEClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) { | ||
conn.NetworkServiceEndpointName, _ = loadAndDelete(ctx) | ||
return next.Client(ctx).Close(ctx, conn, opts...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright (c) 2022 Cisco 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 replacensename_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"go.uber.org/goleak" | ||
"google.golang.org/grpc" | ||
|
||
"github.com/golang/protobuf/ptypes/empty" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/networkservice/common/replacensename" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkclose" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkrequest" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata" | ||
) | ||
|
||
func TestClient(t *testing.T) { | ||
t.Cleanup(func() { goleak.VerifyNone(t) }) | ||
|
||
client := chain.NewNetworkServiceClient( | ||
metadata.NewClient(), | ||
replacensename.NewClient(), | ||
&setNSENameClient{name: "nse-name1"}, | ||
checkrequest.NewClient(t, func(t *testing.T, r *networkservice.NetworkServiceRequest) { | ||
require.Equal(t, "nse-name1", r.Connection.NetworkServiceEndpointName) | ||
}), | ||
checkclose.NewClient(t, func(t *testing.T, c *networkservice.Connection) { | ||
require.Equal(t, "nse-name1", c.NetworkServiceEndpointName) | ||
}), | ||
) | ||
req := &networkservice.NetworkServiceRequest{ | ||
Connection: &networkservice.Connection{Id: "nsc-1"}, | ||
} | ||
conn, err := client.Request(context.Background(), req) | ||
require.NoError(t, err) | ||
|
||
// Change NetworkServiceEndpointName to another name | ||
conn.NetworkServiceEndpointName = "nse-name2" | ||
conn, err = client.Request(context.Background(), req) | ||
require.Equal(t, "nse-name2", conn.NetworkServiceEndpointName) | ||
require.NoError(t, err) | ||
|
||
_, err = client.Close(context.Background(), conn) | ||
require.NoError(t, err) | ||
} | ||
|
||
// setNSENameClient sets NetworkServiceEndpointName only if it is empty | ||
type setNSENameClient struct { | ||
name string | ||
} | ||
|
||
func (s *setNSENameClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) { | ||
if request.GetConnection().NetworkServiceEndpointName == "" { | ||
request.GetConnection().NetworkServiceEndpointName = s.name | ||
} | ||
return next.Client(ctx).Request(ctx, request, opts...) | ||
} | ||
|
||
func (s *setNSENameClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) { | ||
return next.Client(ctx).Close(ctx, conn, opts...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) 2022 Cisco 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 replacensename | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata" | ||
) | ||
|
||
type keyType struct{} | ||
|
||
// store - stores the next network service endpoint name | ||
func store(ctx context.Context, nseName string) { | ||
metadata.Map(ctx, true).Store(keyType{}, nseName) | ||
} | ||
|
||
// load - returns the next network service endpoint name | ||
func load(ctx context.Context) (value string, ok bool) { | ||
rawValue, ok := metadata.Map(ctx, true).Load(keyType{}) | ||
if !ok { | ||
return | ||
} | ||
value, ok = rawValue.(string) | ||
return value, ok | ||
} | ||
|
||
// loadAndDelete - returns the next network service endpoint name and deletes it from the metadata | ||
func loadAndDelete(ctx context.Context) (value string, ok bool) { | ||
rawValue, ok := metadata.Map(ctx, true).LoadAndDelete(keyType{}) | ||
if !ok { | ||
return | ||
} | ||
value, ok = rawValue.(string) | ||
return value, ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) 2022 Cisco 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 checkclose - provides networkservice chain elements to check the Close received from the previous element in the chain | ||
package checkclose | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"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" | ||
) | ||
|
||
type checkCloseClient struct { | ||
*testing.T | ||
check func(*testing.T, *networkservice.Connection) | ||
} | ||
|
||
// NewClient - returns NetworkServiceClient chain elements to check the Close received from the previous element in the chain | ||
// t - *testing.T for checks | ||
// check - function to check the Connection | ||
func NewClient(t *testing.T, check func(*testing.T, *networkservice.Connection)) networkservice.NetworkServiceClient { | ||
return &checkCloseClient{ | ||
T: t, | ||
check: check, | ||
} | ||
} | ||
|
||
func (c *checkCloseClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) { | ||
return next.Client(ctx).Request(ctx, request, opts...) | ||
} | ||
|
||
func (c *checkCloseClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) { | ||
c.check(c.T, conn) | ||
return next.Client(ctx).Close(ctx, conn, opts...) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Im mostly sure that we could face with the similar issue in future (when we need to replace something in pass through requests)
Should we create a pkg like
passthrough
and concatenate all needed replace elements?Something like
And use
passthrough
chain in firewall nses?/cc @edwarnicke
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a good idea, thanks!
Done