From 1bae6066a8ec42dae690329c697a2754ee763aa5 Mon Sep 17 00:00:00 2001 From: Andrey Sobolev Date: Wed, 7 Oct 2020 17:24:04 +0700 Subject: [PATCH] Fix client parameters parse Fix client parameters parse Signed-off-by: Andrey Sobolev --- main.go | 7 ++++--- main_test.go | 6 +++--- pkg/config/config.go | 14 +++++++------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index ef74b41..65f545b 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,8 @@ import ( "path" "time" + "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/sendfd" + "github.com/edwarnicke/grpcfd" "github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig" @@ -42,7 +44,6 @@ import ( "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/memif" "github.com/networkservicemesh/cmd-nsc/pkg/config" "github.com/networkservicemesh/sdk/pkg/networkservice/chains/client" - "github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanisms/sendfd" "github.com/networkservicemesh/sdk/pkg/tools/grpcutils" "github.com/networkservicemesh/sdk/pkg/tools/spiffejwt" @@ -147,7 +148,7 @@ func NewNSMClient(ctx context.Context, rootConf *config.Config) networkservice.N logrus.Infof("NSC: Connecting to Network Service Manager %v", rootConf.ConnectTo.String()) var clientCC *grpc.ClientConn clientCC, err = grpc.DialContext(ctx, - grpcutils.URLToTarget(rootConf.ConnectTo), + grpcutils.URLToTarget(&rootConf.ConnectTo), grpc.WithTransportCredentials( grpcfd.TransportCredentials( credentials.NewTLS( @@ -177,7 +178,7 @@ func NewNSMClient(ctx context.Context, rootConf *config.Config) networkservice.N // RunClient - runs a client application with passed configuration over a client to Network Service Manager func RunClient(ctx context.Context, rootConf *config.Config, nsmClient networkservice.NetworkServiceClient) ([]*networkservice.Connection, error) { // Validate config parameters - if err := rootConf.Validate(); err != nil { + if err := rootConf.IsValid(); err != nil { return []*networkservice.Connection{}, err } diff --git a/main_test.go b/main_test.go index e6548b9..3edf8ff 100644 --- a/main_test.go +++ b/main_test.go @@ -122,7 +122,7 @@ func TestConnectNSM(t *testing.T) { testClient := &nsmTestClient{} cfg := &config.Config{ Name: "nsc", - ConnectTo: &url.URL{ + ConnectTo: url.URL{ Scheme: "unix", Path: "/file.sock", }, @@ -143,7 +143,7 @@ func TestConnectNSMGRPC(t *testing.T) { testClient := &nsmTestClient{} cfg := &config.Config{ Name: "nsc", - ConnectTo: &url.URL{ + ConnectTo: url.URL{ Scheme: "unix", Path: "/file.sock", }, @@ -179,7 +179,7 @@ func TestSendFd(t *testing.T) { cfg := &config.Config{ Name: "nsc", - ConnectTo: &url.URL{Scheme: "tcp", Host: "127.0.0.1:0"}, + ConnectTo: url.URL{Scheme: "tcp", Host: "127.0.0.1:0"}, NetworkServices: []*config.NetworkServiceConfig{ parse(t, "kernel://my-service/nsmKernel?"), }, diff --git a/pkg/config/config.go b/pkg/config/config.go index d6449e2..cd6e0de 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -36,7 +36,7 @@ var configMechanisms = map[string]string{ // Config - configuration for cmd-nsmgr type Config struct { Name string `default:"nsc" desc:"Name of Network Service Client"` - ConnectTo *url.URL `default:"unix:///var/lib/networkservicemesh/nsm.io.sock" desc:"url to connect to NSM" split_words:"true"` + ConnectTo url.URL `default:"unix:///var/lib/networkservicemesh/nsm.io.sock" desc:"url to connect to NSM" split_words:"true"` MaxTokenLifetime time.Duration `default:"24h" desc:"maximum lifetime of tokens" split_words:"true"` Routes []string `default:"" desc:"A list of routes asked by client" split_words:"true"` @@ -46,15 +46,15 @@ type Config struct { NetworkServices []*NetworkServiceConfig `default:"" desc:"A list of Network Service Requests with format [{mechanism}]?:${nsName}[@domainName]?/${interfaceName/memIfSocketName}?${label1}=${value1}&${label2}=${value2}" split_words:"true"` } -// Validate - check if configuration is valid -func (c *Config) Validate() error { +// IsValid - check if configuration is valid +func (c *Config) IsValid() error { if len(c.NetworkServices) == 0 { return errors.New("no network services are specified") } if c.Name == "" { return errors.New("no cleint name specified") } - if c.ConnectTo == nil { + if c.ConnectTo.String() == "" { return errors.New("no NSMGr ConnectTO URL are sepecified") } return nil @@ -100,8 +100,8 @@ func (cfg *NetworkServiceConfig) UnmarshalBinary(text []byte) error { return nil } -// Validate - check if network service request is correct. -func (cfg *NetworkServiceConfig) Validate() error { +// IsValid - check if network service request is correct. +func (cfg *NetworkServiceConfig) IsValid() error { if cfg.Mechanism == "" { return errors.New("invalid mechanism specified") } @@ -149,5 +149,5 @@ func (cfg *NetworkServiceConfig) MergeWithConfigOptions(conf *Config) error { cfg.Labels[key] = strings.Trim(keyValue[1], " ") } } - return cfg.Validate() + return cfg.IsValid() }