From 7b627df09850fde74c0f6ba1206e8ef5f19f765e Mon Sep 17 00:00:00 2001 From: Laszlo Kiraly Date: Tue, 10 May 2022 11:58:02 +0200 Subject: [PATCH] Fix for IPv6 cluster Related issue: deployments-k8s/5775 Related PRs: networkservicemesh/sdk/1281 networkservicemesh/cmd-nse-remote-vlan/49 Signed-off-by: Laszlo Kiraly --- internal/imports/imports_linux.go | 1 + internal/manager/manager.go | 56 +++++++++++++------------------ 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/internal/imports/imports_linux.go b/internal/imports/imports_linux.go index c7102464..758e6aa4 100644 --- a/internal/imports/imports_linux.go +++ b/internal/imports/imports_linux.go @@ -27,6 +27,7 @@ import ( _ "github.com/networkservicemesh/sdk/pkg/tools/clienturlctx" _ "github.com/networkservicemesh/sdk/pkg/tools/debug" _ "github.com/networkservicemesh/sdk/pkg/tools/grpcutils" + _ "github.com/networkservicemesh/sdk/pkg/tools/listenonurl" _ "github.com/networkservicemesh/sdk/pkg/tools/log" _ "github.com/networkservicemesh/sdk/pkg/tools/log/logruslogger" _ "github.com/networkservicemesh/sdk/pkg/tools/log/spanlogger" diff --git a/internal/manager/manager.go b/internal/manager/manager.go index 90590031..c1600cea 100644 --- a/internal/manager/manager.go +++ b/internal/manager/manager.go @@ -1,4 +1,5 @@ // Copyright (c) 2020-2022 Doc.ai and/or its affiliates. +// Copyright (c) 2022 Nordix and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -19,7 +20,6 @@ package manager import ( "context" - "fmt" "net" "net/url" "os" @@ -41,6 +41,7 @@ import ( "github.com/networkservicemesh/sdk/pkg/networkservice/chains/nsmgr" "github.com/networkservicemesh/sdk/pkg/networkservice/common/authorize" "github.com/networkservicemesh/sdk/pkg/tools/grpcutils" + "github.com/networkservicemesh/sdk/pkg/tools/listenonurl" "github.com/networkservicemesh/sdk/pkg/tools/log" "github.com/networkservicemesh/sdk/pkg/tools/spiffejwt" "github.com/networkservicemesh/sdk/pkg/tools/token" @@ -108,9 +109,11 @@ func RunNsmgr(ctx context.Context, configuration *config.Config) error { return err } + u := genPublishableURL(configuration.ListenOn, m.logger) + mgrOptions := []nsmgr.Option{ nsmgr.WithName(configuration.Name), - nsmgr.WithURL(m.getPublicURL()), + nsmgr.WithURL(u.String()), nsmgr.WithAuthorizeServer(authorize.NewServer()), nsmgr.WithDialTimeout(configuration.DialTimeout), nsmgr.WithForwarderServiceName(configuration.ForwarderNetworkServiceName), @@ -185,36 +188,6 @@ func waitErrChan(ctx context.Context, errChan <-chan error, m *manager) { } } -func (m *manager) defaultURL() *url.URL { - for i := 0; i < len(m.configuration.ListenOn); i++ { - u := &m.configuration.ListenOn[i] - if u.Scheme == tcpSchema { - return u - } - } - return &m.configuration.ListenOn[0] -} - -func (m *manager) getPublicURL() string { - u := m.defaultURL() - if u.Port() == "" || len(u.Host) != len(":")+len(u.Port()) { - return u.String() - } - addrs, err := net.InterfaceAddrs() - if err != nil { - m.logger.Warn(err.Error()) - return u.String() - } - 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(), u.Port()) - } - } - } - return u.String() -} - func (m *manager) startServers(server *grpc.Server) { var wg sync.WaitGroup for i := 0; i < len(m.configuration.ListenOn); i++ { @@ -233,3 +206,22 @@ func (m *manager) startServers(server *grpc.Server) { } wg.Wait() } + +func genPublishableURL(listenOn []url.URL, logger log.Logger) *url.URL { + u := defaultURL(listenOn) + addrs, err := net.InterfaceAddrs() + if err != nil { + logger.Warn(err.Error()) + return u + } + return listenonurl.GetPublicURL(addrs, u) +} +func defaultURL(listenOn []url.URL) *url.URL { + for i := 0; i < len(listenOn); i++ { + u := &listenOn[i] + if u.Scheme == tcpSchema { + return u + } + } + return &listenOn[0] +}