Skip to content

Commit

Permalink
Merge pull request #249 from d-uzlov/icmp/246/miltuservice
Browse files Browse the repository at this point in the history
Add support for multiple network services
  • Loading branch information
denis-tingaikin authored Jul 16, 2021
2 parents 9ce6a05 + 3645188 commit 7265eaa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion auto_shutdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
)

func TestAutoShutdown(t *testing.T) {
err := os.Setenv("NSE_IDLE_TIMEOUT", "2s")
err := os.Setenv("NSM_IDLE_TIMEOUT", "2s")
require.NoError(t, err)
f := TestSuite{}
f.SetT(t)
Expand Down
42 changes: 22 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type Config struct {
BaseDir string `default:"./" desc:"base directory" split_words:"true"`
ConnectTo url.URL `default:"unix:///var/lib/networkservicemesh/nsm.io.sock" desc:"url to connect to" split_words:"true"`
MaxTokenLifetime time.Duration `default:"24h" desc:"maximum lifetime of tokens" split_words:"true"`
ServiceName string `default:"icmp-responder" desc:"Name of providing service" split_words:"true"`
ServiceNames []string `default:"icmp-responder" desc:"Name of provided services" split_words:"true"`
Payload string `default:"ETHERNET" desc:"Name of provided service payload" split_words:"true"`
Labels map[string]string `default:"" desc:"Endpoint labels"`
DNSConfigs dnstools.Decoder `default:"[]" desc:"DNSConfigs represents array of DNSConfig in json format. See at model definition: https://github.com/networkservicemesh/api/blob/main/pkg/api/networkservice/connectioncontext.pb.go#L426-L435" split_words:"true"`
Expand All @@ -81,10 +81,10 @@ type Config struct {

// Process prints and processes env to config
func (c *Config) Process() error {
if err := envconfig.Usage("nse", c); err != nil {
if err := envconfig.Usage("nsm", c); err != nil {
return errors.Wrap(err, "cannot show usage of envconfig nse")
}
if err := envconfig.Process("nse", c); err != nil {
if err := envconfig.Process("nsm", c); err != nil {
return errors.Wrap(err, "cannot process envconfig nse")
}
return nil
Expand Down Expand Up @@ -227,28 +227,30 @@ func main() {
)

if config.RegisterService {
nsRegistryClient := registryclient.NewNetworkServiceRegistryClient(ctx, &config.ConnectTo, registryclient.WithDialOptions(clientOptions...))
_, err = nsRegistryClient.Register(ctx, &registryapi.NetworkService{
Name: config.ServiceName,
Payload: config.Payload,
})
for _, serviceName := range config.ServiceNames {
nsRegistryClient := registryclient.NewNetworkServiceRegistryClient(ctx, &config.ConnectTo, registryclient.WithDialOptions(clientOptions...))
_, err = nsRegistryClient.Register(ctx, &registryapi.NetworkService{
Name: serviceName,
Payload: config.Payload,
})

if err != nil {
log.FromContext(ctx).Fatalf("unable to register ns %+v", err)
if err != nil {
log.FromContext(ctx).Fatalf("unable to register ns %+v", err)
}
}
}

nseRegistryClient := registryclient.NewNetworkServiceEndpointRegistryClient(ctx, &config.ConnectTo, registryclient.WithDialOptions(clientOptions...))
nse, err := nseRegistryClient.Register(ctx, &registryapi.NetworkServiceEndpoint{
Name: config.Name,
NetworkServiceNames: []string{config.ServiceName},
NetworkServiceLabels: map[string]*registryapi.NetworkServiceLabels{
config.ServiceName: {
Labels: config.Labels,
},
},
Url: listenOn.String(),
})
nse := &registryapi.NetworkServiceEndpoint{
Name: config.Name,
NetworkServiceNames: config.ServiceNames,
NetworkServiceLabels: make(map[string]*registryapi.NetworkServiceLabels),
Url: listenOn.String(),
}
for _, serviceName := range config.ServiceNames {
nse.NetworkServiceLabels[serviceName] = &registryapi.NetworkServiceLabels{Labels: config.Labels}
}
nse, err = nseRegistryClient.Register(ctx, nse)
logrus.Infof("nse: %+v", nse)

if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion suite_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,10 @@ func (f *TestSuite) SetupSuite() {
f.Require().NoError(err)
}(cancel, f.ListenAndServe(ctx, server))

f.Require().Greater(len(f.config.ServiceNames), 0)
recv, err := adapters.NetworkServiceEndpointServerToClient(memrg).Find(ctx, &registry.NetworkServiceEndpointQuery{
NetworkServiceEndpoint: &registry.NetworkServiceEndpoint{
NetworkServiceNames: []string{f.config.ServiceName},
NetworkServiceNames: []string{f.config.ServiceNames[0]},
},
Watch: true,
})
Expand Down

0 comments on commit 7265eaa

Please sign in to comment.