Skip to content
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

Set default kernel interface name #1014

Merged
merged 2 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions pkg/networkservice/common/mechanisms/kernel/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,18 @@ func NewClient(options ...Option) networkservice.NetworkServiceClient {

func (k *kernelMechanismClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) {
if !k.updateMechanismPreferences(request) {
request.MechanismPreferences = append(request.GetMechanismPreferences(), &networkservice.Mechanism{
mechanism := &networkservice.Mechanism{
Cls: cls.LOCAL,
Type: kernel.MECHANISM,
Parameters: map[string]string{
kernel.NetNSURL: (&url.URL{Scheme: "file", Path: netNSFilename}).String(),
kernel.InterfaceNameKey: k.interfaceName,
kernel.InterfaceNameKey: GetNameFromConnection(request.GetConnection()),
},
})
}
if k.interfaceName != "" {
mechanism.Parameters[kernel.InterfaceNameKey] = k.interfaceName
}
request.MechanismPreferences = append(request.GetMechanismPreferences(), mechanism)
}
return next.Client(ctx).Request(ctx, request, opts...)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/networkservice/common/mechanisms/kernel/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020 Doc.ai and/or its affiliates.
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -39,6 +39,7 @@ func NewServer() networkservice.NetworkServiceServer {
func (m *kernelMechanismServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
if mechanism := kernel.ToMechanism(request.GetConnection().GetMechanism()); mechanism != nil {
mechanism.SetNetNSURL((&url.URL{Scheme: "file", Path: netNSFilename}).String())
mechanism.SetInterfaceName(GetNameFromConnection(request.GetConnection()))
}
return next.Server(ctx).Request(ctx, request)
}
Expand Down
38 changes: 38 additions & 0 deletions pkg/networkservice/common/mechanisms/kernel/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 kernel

import (
"fmt"

"github.com/networkservicemesh/api/pkg/api/networkservice"
kernelmech "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/kernel"
)

// GetNameFromConnection - returns a name computed from networkservice.Connection 'conn'
func GetNameFromConnection(conn *networkservice.Connection) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@glazychev-art Why is this public?

ns := conn.GetNetworkService()
nsMaxLength := kernelmech.LinuxIfMaxLength - 5
if len(ns) > nsMaxLength {
ns = ns[:nsMaxLength]
}
name := fmt.Sprintf("%s-%s", ns, conn.GetId())
if len(name) > kernelmech.LinuxIfMaxLength {
name = name[:kernelmech.LinuxIfMaxLength]
}
return name
}
8 changes: 6 additions & 2 deletions pkg/networkservice/common/mechanismtranslation/client_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020 Doc.ai and/or its affiliates.
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -37,7 +37,11 @@ import (
)

func kernelMechanism() *networkservice.Mechanism {
request := new(networkservice.NetworkServiceRequest)
request := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Id: "id",
},
}
_, _ = kernel.NewClient().Request(context.TODO(), request)
return request.MechanismPreferences[0]
}
Expand Down