Skip to content

Commit

Permalink
add cluster-info chain elements (#1326)
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Tingaikin <denis.tingajkin@xored.com>
  • Loading branch information
denis-tingaikin authored Jul 14, 2022
1 parent 7c9832b commit b8209fc
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/networkservice/chains/nsmgrproxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/networkservicemesh/sdk/pkg/networkservice/chains/endpoint"
"github.com/networkservicemesh/sdk/pkg/networkservice/chains/nsmgr"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/authorize"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/clusterinfo"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/connect"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/discover"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/interdomainbypass"
Expand All @@ -41,6 +42,7 @@ import (
"github.com/networkservicemesh/sdk/pkg/registry/common/begin"
"github.com/networkservicemesh/sdk/pkg/registry/common/clientconn"
"github.com/networkservicemesh/sdk/pkg/registry/common/clienturl"
registryclusterinfo "github.com/networkservicemesh/sdk/pkg/registry/common/clusterinfo"
registryconnect "github.com/networkservicemesh/sdk/pkg/registry/common/connect"
"github.com/networkservicemesh/sdk/pkg/registry/common/dial"
registryswapip "github.com/networkservicemesh/sdk/pkg/registry/common/swapip"
Expand Down Expand Up @@ -187,6 +189,7 @@ func NewServer(ctx context.Context, regURL, proxyURL *url.URL, tokenGenerator to
interdomainbypass.NewServer(&interdomainBypassNSEServer, opts.listenOn),
discover.NewServer(nsClient, nseClient),
swapip.NewServer(opts.openMapIPChannel(ctx)),
clusterinfo.NewServer(),
connect.NewServer(
client.NewClient(
ctx,
Expand Down Expand Up @@ -219,6 +222,7 @@ func NewServer(ctx context.Context, regURL, proxyURL *url.URL, tokenGenerator to
clienturl.NewNetworkServiceEndpointRegistryServer(proxyURL),
interdomainBypassNSEServer,
registryswapip.NewNetworkServiceEndpointRegistryServer(opts.openMapIPChannel(ctx)),
registryclusterinfo.NewNetworkServiceEndpointRegistryServer(),
registryconnect.NewNetworkServiceEndpointRegistryServer(
chain.NewNetworkServiceEndpointRegistryClient(
clientconn.NewNetworkServiceEndpointRegistryClient(),
Expand Down
27 changes: 27 additions & 0 deletions pkg/networkservice/common/clusterinfo/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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 clusterinfo

// Option overrides default configuration
type Option = func(*clusterinfoServer)

// WithConfigPath replaces defeault clusterinfo config file path
func WithConfigPath(p string) func(o *clusterinfoServer) {
return func(o *clusterinfoServer) {
o.configPath = p
}
}
66 changes: 66 additions & 0 deletions pkg/networkservice/common/clusterinfo/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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 clusterinfo provides a chain element that appends clusterinfo labels into the request.
package clusterinfo

import (
"context"
"io/ioutil"

"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"gopkg.in/yaml.v2"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
)

type clusterinfoServer struct {
configPath string
}

// NewServer - returns a new clusterinfo NetworkServiceServer that adds clusterinfo labels into request from the cluterinfo configuration.
func NewServer(opts ...Option) networkservice.NetworkServiceServer {
var r = &clusterinfoServer{
configPath: "/etc/clusterinfo/config.yaml",
}
for _, opt := range opts {
opt(r)
}
return r
}

func (n *clusterinfoServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
if request.GetConnection().GetLabels() == nil {
request.GetConnection().Labels = make(map[string]string)
}

var m = make(map[string]string)

if b, err := ioutil.ReadFile(n.configPath); err == nil {
_ = yaml.Unmarshal(b, &m)
}

for k, v := range m {
request.GetConnection().GetLabels()[k] = v
}

return next.Server(ctx).Request(ctx, request)
}

func (n *clusterinfoServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
return next.Server(ctx).Close(ctx, conn)
}
46 changes: 46 additions & 0 deletions pkg/networkservice/common/clusterinfo/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 clusterinfo_test

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"

"github.com/networkservicemesh/sdk/pkg/networkservice/common/clusterinfo"
)

func TestReadClusterName(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })

var path = filepath.Join(t.TempDir(), "clusterinfo.yaml")
require.NoError(t, ioutil.WriteFile(path, []byte("CLUSTER_NAME: my-cluster1"), os.ModePerm))

var s = clusterinfo.NewServer(clusterinfo.WithConfigPath(path))

var resp, err = s.Request(context.Background(), &networkservice.NetworkServiceRequest{Connection: &networkservice.Connection{}})
require.NoError(t, err)

require.Len(t, resp.Labels, 1)
require.Equal(t, "my-cluster1", resp.GetLabels()["CLUSTER_NAME"])
}
70 changes: 70 additions & 0 deletions pkg/registry/common/clusterinfo/nse_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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 clusterinfo provides a chain element that appends clusterinfo labels into the request.
package clusterinfo

import (
"context"
"io/ioutil"

"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/registry"
"gopkg.in/yaml.v2"

"github.com/networkservicemesh/sdk/pkg/registry/core/next"
)

type clusterinfoNSEServer struct {
configPath string
}

func (n *clusterinfoNSEServer) Register(ctx context.Context, nse *registry.NetworkServiceEndpoint) (*registry.NetworkServiceEndpoint, error) {
var m = make(map[string]string)

if b, err := ioutil.ReadFile(n.configPath); err == nil {
_ = yaml.Unmarshal(b, &m)
}

for k, v := range m {
for _, labels := range nse.GetNetworkServiceLabels() {
if labels.GetLabels() == nil {
labels.Labels = make(map[string]string)
}
labels.GetLabels()[k] = v
}
}
return next.NetworkServiceEndpointRegistryServer(ctx).Register(ctx, nse)
}

func (n *clusterinfoNSEServer) Find(query *registry.NetworkServiceEndpointQuery, server registry.NetworkServiceEndpointRegistry_FindServer) error {
return next.NetworkServiceEndpointRegistryServer(server.Context()).Find(query, server)
}

func (n *clusterinfoNSEServer) Unregister(ctx context.Context, nse *registry.NetworkServiceEndpoint) (*empty.Empty, error) {
return next.NetworkServiceEndpointRegistryServer(ctx).Unregister(ctx, nse)
}

// NewNetworkServiceEndpointRegistryServer - returns a new clusterinfo server that adds clusterinfo labels into nse registration
func NewNetworkServiceEndpointRegistryServer(opts ...Option) registry.NetworkServiceEndpointRegistryServer {
var r = &clusterinfoNSEServer{
configPath: "/etc/clusterinfo/config.yaml",
}
for _, opt := range opts {
opt(r)
}
return r
}
49 changes: 49 additions & 0 deletions pkg/registry/common/clusterinfo/nse_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 clusterinfo_test

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/networkservicemesh/api/pkg/api/registry"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"

"github.com/networkservicemesh/sdk/pkg/registry/common/clusterinfo"
)

func TestReadClusterName(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })

var path = filepath.Join(t.TempDir(), "clusterinfo.yaml")
require.NoError(t, ioutil.WriteFile(path, []byte("CLUSTER_NAME: my-cluster1"), os.ModePerm))

var s = clusterinfo.NewNetworkServiceEndpointRegistryServer(clusterinfo.WithConfigPath(path))

var resp, err = s.Register(context.Background(), &registry.NetworkServiceEndpoint{NetworkServiceLabels: map[string]*registry.NetworkServiceLabels{
"ns-1": {},
}})
require.NoError(t, err)

require.Len(t, resp.GetNetworkServiceLabels(), 1)
require.Len(t, resp.GetNetworkServiceLabels()["ns-1"].GetLabels(), 1)
require.Equal(t, "my-cluster1", resp.GetNetworkServiceLabels()["ns-1"].GetLabels()["CLUSTER_NAME"])
}
27 changes: 27 additions & 0 deletions pkg/registry/common/clusterinfo/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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 clusterinfo

// Option overrides default configuration
type Option = func(*clusterinfoNSEServer)

// WithConfigPath replaces defeault clusterinfo config file path
func WithConfigPath(p string) func(o *clusterinfoNSEServer) {
return func(o *clusterinfoNSEServer) {
o.configPath = p
}
}

0 comments on commit b8209fc

Please sign in to comment.