generated from networkservicemesh/cmd-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Nordix/vlansup-dev
update endpoint implementation (see networkservicemesh/cmd-forwarder-vpp#429)
- Loading branch information
Showing
11 changed files
with
1,010 additions
and
11 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
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,3 +1,17 @@ | ||
module github.com/networkservicemesh/cmd-template | ||
module github.com/networkservicemesh/cmd-nse-remote-vlan | ||
|
||
go 1.16 | ||
|
||
require ( | ||
github.com/antonfisher/nested-logrus-formatter v1.3.1 | ||
github.com/edwarnicke/grpcfd v0.1.1 | ||
github.com/golang/protobuf v1.4.3 | ||
github.com/kelseyhightower/envconfig v1.4.0 | ||
github.com/networkservicemesh/api v1.0.1-0.20211110183123-3038992da61a | ||
github.com/networkservicemesh/sdk v0.5.1-0.20211209170740-8e964705e604 | ||
github.com/pkg/errors v0.9.1 | ||
github.com/sirupsen/logrus v1.7.0 | ||
github.com/spiffe/go-spiffe/v2 v2.0.0-alpha.4.0.20200528145730-dc11d0c74e85 | ||
github.com/stretchr/testify v1.7.0 | ||
google.golang.org/grpc v1.35.0 | ||
) |
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,158 @@ | ||
// Copyright (c) 2021 Nordix Foundation. | ||
// | ||
// 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 config provides methods to get configuration parameters from environment variables | ||
package config | ||
|
||
import ( | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/kelseyhightower/envconfig" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
vlanPrefix = "vlan:" | ||
labelsPrefix = "labels:" | ||
viaPrefix = "via:" | ||
domainPrefix = "domain:" | ||
|
||
tcpSchema = "tcp" | ||
) | ||
|
||
// Config holds configuration parameters from environment variables | ||
type Config struct { | ||
Name string `default:"vlan-server" desc:"Name of the endpoint"` | ||
ConnectTo url.URL `default:"nsm-registry-svc:5002" desc:"url of registry service to connect to" split_words:"true"` | ||
MaxTokenLifetime time.Duration `default:"24h" desc:"maximum lifetime of tokens" split_words:"true"` | ||
CidrPrefix string `default:"169.254.0.0/16" desc:"CIDR Prefix to assign IPs from" split_words:"true"` | ||
Ipv6Prefix string `default:"" desc:"Ipv6 Prefix for dual-stack" split_words:"true"` | ||
RegisterService bool `default:"true" desc:"if true then registers network service on startup" split_words:"true"` | ||
ListenOn url.URL `default:"tcp://:5003" desc:"tcp:// url to be listen on. It will be used as public to register NSM" split_words:"true"` | ||
|
||
Services []ServiceConfig `default:"" desc:"list of supported services"` | ||
} | ||
|
||
// Process prints and processes env to config | ||
func (c *Config) Process() error { | ||
if err := envconfig.Usage("nsm", c); err != nil { | ||
return errors.Wrap(err, "cannot show usage of envconfig nse") | ||
} | ||
if err := envconfig.Process("nsm", c); err != nil { | ||
return errors.Wrap(err, "cannot process envconfig nse") | ||
} | ||
if err := validateConfig(c); err != nil { | ||
return errors.Wrap(err, "configuration validation failed") | ||
} | ||
return nil | ||
} | ||
func validateConfig(cfg *Config) error { | ||
if cfg.ListenOn.Scheme != tcpSchema { | ||
return errors.New("only tcp schema is supported for this type of endpoint") | ||
} | ||
return nil | ||
} | ||
|
||
// ServiceConfig is a per-service config | ||
type ServiceConfig struct { | ||
Name string | ||
Domain string | ||
Via string | ||
VLANTag int32 | ||
Labels map[string]string | ||
} | ||
|
||
// InitValues set initial values for ServiceConfig | ||
func (s *ServiceConfig) InitValues() { | ||
s.Domain = "" | ||
s.VLANTag = 0 | ||
s.Via = "" | ||
} | ||
|
||
// UnmarshalBinary expects string(bytes) to be in format: | ||
// Name { domain: Domain; vlan: VLANTag; labels: Labels; via: Via; } | ||
// Labels = label_1=value_1&label_2=value_2 | ||
func (s *ServiceConfig) UnmarshalBinary(bytes []byte) (err error) { | ||
text := string(bytes) | ||
|
||
split := strings.Split(text, "{") | ||
if len(split) < 2 { | ||
return errors.Errorf("invalid format: %s", text) | ||
} | ||
s.Name = strings.TrimSpace(split[0]) | ||
s.InitValues() | ||
split = strings.Split(split[1], "}") | ||
for _, part := range strings.Split(split[0], ";") { | ||
part = strings.TrimSpace(part) | ||
switch { | ||
case strings.HasPrefix(part, vlanPrefix): | ||
s.VLANTag, err = parseInt32(trimPrefix(part, vlanPrefix)) | ||
case strings.HasPrefix(part, labelsPrefix): | ||
s.Labels, err = parseMap(trimPrefix(part, labelsPrefix)) | ||
case strings.HasPrefix(part, viaPrefix): | ||
s.Via = trimPrefix(part, viaPrefix) | ||
case strings.HasPrefix(part, domainPrefix): | ||
s.Domain = trimPrefix(part, domainPrefix) | ||
default: | ||
err = errors.Errorf("invalid format: %s", text) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return s.validate() | ||
} | ||
|
||
func trimPrefix(s, prefix string) string { | ||
s = strings.TrimPrefix(s, prefix) | ||
return strings.TrimSpace(s) | ||
} | ||
|
||
func parseInt32(s string) (int32, error) { | ||
i, err := strconv.ParseInt(s, 0, 32) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return int32(i), nil | ||
} | ||
|
||
func parseMap(s string) (map[string]string, error) { | ||
m := make(map[string]string) | ||
for _, keyValue := range strings.Split(s, "&") { | ||
split := strings.Split(keyValue, "=") | ||
if len(split) != 2 { | ||
return nil, errors.Errorf("invalid key-value pair: %s", keyValue) | ||
} | ||
m[split[0]] = split[1] | ||
} | ||
return m, nil | ||
} | ||
|
||
func (s *ServiceConfig) validate() error { | ||
if s.Name == "" { | ||
return errors.New("name is empty") | ||
} | ||
if s.Via == "" { | ||
return errors.New("via is empty") | ||
} | ||
if s.VLANTag < 0 || s.VLANTag > 4095 { | ||
return errors.New("Invalid VLAN ID") | ||
} | ||
return nil | ||
} |
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,48 @@ | ||
// Copyright (c) 2021 Nordix Foundation. | ||
// | ||
// 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 config_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/networkservicemesh/cmd-nse-remote-vlan/internal/pkg/config" | ||
) | ||
|
||
func TestServiceConfig_UnmarshalBinary(t *testing.T) { | ||
cfg := new(config.ServiceConfig) | ||
|
||
err := cfg.UnmarshalBinary([]byte("finance-bridge { domain: service-domain.2; vlan: 100; via: gw-1 }")) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, &config.ServiceConfig{ | ||
Name: "finance-bridge", | ||
Domain: "service-domain.2", | ||
Via: "gw-1", | ||
VLANTag: 100, | ||
}, cfg) | ||
|
||
err = cfg.UnmarshalBinary([]byte("finance-bridge { vlan: 200; via: service-domain.1 }")) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, &config.ServiceConfig{ | ||
Name: "finance-bridge", | ||
Via: "service-domain.1", | ||
VLANTag: 200, | ||
}, cfg) | ||
} |
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,22 @@ | ||
// Copyright (c) 2020-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 imports is used for generating list of imports to optimize use of docker build cache | ||
package imports | ||
|
||
//go:generate bash -c "rm -rf imports*.go" | ||
//go:generate bash -c "cd $(mktemp -d) && GO111MODULE=on go get github.com/edwarnicke/[email protected]" | ||
//go:generate bash -c "GOOS=linux ${GOPATH}/bin/imports-gen" |
Oops, something went wrong.