-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add cluster-info chain elements (#1326)
Signed-off-by: Denis Tingaikin <denis.tingajkin@xored.com>
- Loading branch information
1 parent
7c9832b
commit b8209fc
Showing
7 changed files
with
289 additions
and
0 deletions.
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 |
---|---|---|
@@ -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 | ||
} | ||
} |
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,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) | ||
} |
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,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"]) | ||
} |
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,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 | ||
} |
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,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(), ®istry.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"]) | ||
} |
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,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 | ||
} | ||
} |