Skip to content

Commit

Permalink
Set default kernel interface name (#1014)
Browse files Browse the repository at this point in the history
* Set default kernel interface name

Signed-off-by: Artem Glazychev <artem.glazychev@xored.com>

* fix review

Signed-off-by: Artem Glazychev <artem.glazychev@xored.com>
  • Loading branch information
glazychev-art authored Jul 13, 2021
1 parent 7750810 commit d11653e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
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 {
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

0 comments on commit d11653e

Please sign in to comment.