Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for IPv6 cluster #488

Merged
merged 1 commit into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/imports/imports_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
56 changes: 24 additions & 32 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
@@ -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
//
Expand All @@ -19,7 +20,6 @@ package manager

import (
"context"
"fmt"
"net"
"net/url"
"os"
Expand All @@ -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"
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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++ {
Expand All @@ -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]
}