Skip to content

Commit

Permalink
Fix for IPv6 cluster
Browse files Browse the repository at this point in the history
Related issue: deployments-k8s/5775

Signed-off-by: Laszlo Kiraly <laszlo.kiraly@est.tech>
  • Loading branch information
ljkiraly committed May 10, 2022
1 parent 568195c commit 6e51ad2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
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
47 changes: 14 additions & 33 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,8 +20,6 @@ package manager

import (
"context"
"fmt"
"net"
"net/url"
"os"
"path"
Expand All @@ -41,6 +40,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 @@ -107,10 +107,11 @@ func RunNsmgr(ctx context.Context, configuration *config.Config) error {
m.logger.Errorf("failed to create new spiffe TLS Peer %v", err)
return err
}
u := defaultURL(configuration.ListenOn)

mgrOptions := []nsmgr.Option{
nsmgr.WithName(configuration.Name),
nsmgr.WithURL(m.getPublicURL()),
nsmgr.WithURL(listenonurl.GetPublicURL(u, m.logger)),
nsmgr.WithAuthorizeServer(authorize.NewServer()),
nsmgr.WithDialTimeout(configuration.DialTimeout),
nsmgr.WithForwarderServiceName(configuration.ForwarderNetworkServiceName),
Expand Down Expand Up @@ -185,36 +186,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 +204,13 @@ func (m *manager) startServers(server *grpc.Server) {
}
wg.Wait()
}

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]
}

0 comments on commit 6e51ad2

Please sign in to comment.