Skip to content

Commit

Permalink
fix NSMgr adds NSE in registry with wrong URL
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-tingaikin committed Nov 12, 2020
1 parent df148ba commit 87d408e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 30 deletions.
51 changes: 27 additions & 24 deletions internal/imports/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,40 @@ import (
_ "context"
_ "errors"
_ "fmt"
_ "io/ioutil"
_ "net"
_ "net/url"
_ "os"
_ "path"
_ "path/filepath"
_ "strings"
_ "sync"
_ "testing"
_ "time"

_ "github.com/antonfisher/nested-logrus-formatter"
_ "github.com/edwarnicke/serialize"
_ "github.com/kelseyhightower/envconfig"
_ "github.com/opentracing/opentracing-go"
_ "github.com/sirupsen/logrus"
_ "github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
_ "github.com/spiffe/go-spiffe/v2/svid/x509svid"
_ "github.com/spiffe/go-spiffe/v2/workloadapi"
_ "github.com/stretchr/testify/assert"
_ "github.com/stretchr/testify/require"
_ "github.com/stretchr/testify/suite"
_ "google.golang.org/grpc"
_ "google.golang.org/grpc/credentials"
_ "google.golang.org/grpc/grpclog"
_ "google.golang.org/grpc/health/grpc_health_v1"
_ "google.golang.org/grpc/metadata"
_ "google.golang.org/grpc/peer"

_ "github.com/networkservicemesh/api/pkg/api/networkservice"
_ "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/cls"
_ "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/kernel"
_ "github.com/networkservicemesh/api/pkg/api/registry"

_ "github.com/networkservicemesh/sdk/pkg/networkservice/chains/client"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/chains/endpoint"
_ "github.com/networkservicemesh/sdk/pkg/networkservice/chains/nsmgr"
Expand All @@ -34,28 +61,4 @@ import (
_ "github.com/networkservicemesh/sdk/pkg/tools/spiffejwt"
_ "github.com/networkservicemesh/sdk/pkg/tools/spire"
_ "github.com/networkservicemesh/sdk/pkg/tools/token"
_ "github.com/opentracing/opentracing-go"
_ "github.com/sirupsen/logrus"
_ "github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
_ "github.com/spiffe/go-spiffe/v2/svid/x509svid"
_ "github.com/spiffe/go-spiffe/v2/workloadapi"
_ "github.com/stretchr/testify/assert"
_ "github.com/stretchr/testify/require"
_ "github.com/stretchr/testify/suite"
_ "google.golang.org/grpc"
_ "google.golang.org/grpc/credentials"
_ "google.golang.org/grpc/grpclog"
_ "google.golang.org/grpc/health/grpc_health_v1"
_ "google.golang.org/grpc/metadata"
_ "google.golang.org/grpc/peer"
_ "io/ioutil"
_ "net"
_ "net/url"
_ "os"
_ "path"
_ "path/filepath"
_ "strings"
_ "sync"
_ "testing"
_ "time"
)
50 changes: 44 additions & 6 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ package manager

import (
"context"
"fmt"
"net"
"os"
"path"
"strconv"
"strings"
"sync"
"time"

"github.com/networkservicemesh/sdk/pkg/networkservice/chains/nsmgr"

"github.com/networkservicemesh/cmd-nsmgr/internal/authz"

"github.com/networkservicemesh/sdk/pkg/networkservice/common/authorize"
"github.com/networkservicemesh/sdk/pkg/tools/callback"
"github.com/networkservicemesh/sdk/pkg/tools/grpcutils"
Expand All @@ -39,6 +44,7 @@ import (
"google.golang.org/grpc/credentials"

"github.com/networkservicemesh/api/pkg/api/registry"

"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/networkservicemesh/sdk/pkg/tools/spanhelper"
"github.com/networkservicemesh/sdk/pkg/tools/spiffejwt"
Expand Down Expand Up @@ -108,7 +114,7 @@ func RunNsmgr(ctx context.Context, configuration *config.Config) error {

nsmMgr := &registry.NetworkServiceEndpoint{
Name: configuration.Name,
Url: configuration.ListenOn[0].String(),
Url: m.getPublicURL(),
}

// Construct callback server
Expand Down Expand Up @@ -148,7 +154,7 @@ func RunNsmgr(ctx context.Context, configuration *config.Config) error {
callback.RegisterCallbackServiceServer(m.server, callbackServer)

// Create GRPC server
m.startServers(nsmMgr, m.server)
m.startServers(m.server)

log.Entry(m.ctx).Infof("Startup completed in %v", time.Since(starttime))
starttime = time.Now()
Expand Down Expand Up @@ -201,7 +207,33 @@ func (m *manager) connectRegistry() (err error) {
return
}

func (m *manager) startServers(nsmMgr *registry.NetworkServiceEndpoint, server *grpc.Server) {
func (m *manager) defaultURL() string {
for i := 0; i < len(m.configuration.ListenOn); i++ {
u := &m.configuration.ListenOn[i]
if u.Scheme == tcpSchema {
return u.String()
}
}
return "tcp://127.0.0.1:5001"
}

func (m *manager) getPublicURL() string {
u := m.defaultURL()
addrs, err := net.InterfaceAddrs()
if err != nil {
return u
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return fmt.Sprintf("%v://%v:%v", tcpSchema, ipnet.IP.String(), portFromURL(u))
}
}
}
return u
}

func (m *manager) startServers(server *grpc.Server) {
var wg sync.WaitGroup
for i := 0; i < len(m.configuration.ListenOn); i++ {
listenURL := &m.configuration.ListenOn[i]
Expand All @@ -211,9 +243,6 @@ func (m *manager) startServers(nsmMgr *registry.NetworkServiceEndpoint, server *
// Create a required number of servers
errChan := grpcutils.ListenAndServe(m.ctx, listenURL, server)
logrus.Infof("NSMGR Listening on: %v", listenURL.String())
if listenURL.Scheme == tcpSchema {
nsmMgr.Url = listenURL.String()
}
// For public schemas we need to perform registation of nsmgr into registry.
wg.Done()

Expand All @@ -222,3 +251,12 @@ func (m *manager) startServers(nsmMgr *registry.NetworkServiceEndpoint, server *
}
wg.Wait()
}

func portFromURL(s string) int {
pieces := strings.Split(s, ":")
p, err := strconv.Atoi(pieces[len(pieces)-1])
if err != nil {
logrus.Fatalf("URL has wrong port: %v, err: %v", s, err)
}
return p
}
1 change: 1 addition & 0 deletions test/client_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import (
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/kernel"
"github.com/networkservicemesh/api/pkg/api/registry"
"github.com/networkservicemesh/cmd-nsmgr/internal/authz"

"github.com/networkservicemesh/sdk/pkg/networkservice/chains/client"
"github.com/networkservicemesh/sdk/pkg/tools/callback"
"github.com/networkservicemesh/sdk/pkg/tools/spiffejwt"
Expand Down

0 comments on commit 87d408e

Please sign in to comment.