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
Related PRs:
cmd-nsmgr/488
sdk/1281

Signed-off-by: Laszlo Kiraly <[email protected]>
  • Loading branch information
ljkiraly committed May 10, 2022
1 parent e96c4ba commit 8767f0b
Showing 1 changed file with 4 additions and 23 deletions.
27 changes: 4 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package main

import (
"context"
"fmt"
"net"
"net/url"
"os"
Expand Down Expand Up @@ -55,6 +54,7 @@ import (
registrysendfd "github.com/networkservicemesh/sdk/pkg/registry/common/sendfd"
"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/opentelemetry"
Expand Down Expand Up @@ -230,7 +230,7 @@ func main() {
registrysendfd.NewNetworkServiceEndpointRegistryClient(),
),
)
nse := getNseEndpoint(listenOn, cfg)
nse := getNseEndpoint(listenOn, cfg, logger)

nse, err = nseRegistryClient.Register(ctx, nse)
logrus.Infof("nse: %+v", nse)
Expand Down Expand Up @@ -261,33 +261,14 @@ func exitOnErr(ctx context.Context, cancel context.CancelFunc, errCh <-chan erro
}(ctx, errCh)
}

func getPublicURL(u *url.URL) string {
if u.Port() == "" || len(u.Host) != len(":")+len(u.Port()) {
return u.String()
}
addrs, err := net.InterfaceAddrs()
if err != nil {
logrus.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("tcp://%v:%v", ipnet.IP.String(), u.Port())
}
}
}
return u.String()
}

func getNseEndpoint(listenOn *url.URL, cfg *config.Config) *registryapi.NetworkServiceEndpoint {
func getNseEndpoint(listenOn *url.URL, cfg *config.Config, logger log.Logger) *registryapi.NetworkServiceEndpoint {
expireTime := timestamppb.New(time.Now().Add(cfg.MaxTokenLifetime))

nse := &registryapi.NetworkServiceEndpoint{
Name: cfg.Name,
NetworkServiceNames: make([]string, len(cfg.Services)),
NetworkServiceLabels: make(map[string]*registryapi.NetworkServiceLabels, len(cfg.Services)),
Url: getPublicURL(listenOn),
Url: listenonurl.GetPublicURL(listenOn, logger),
ExpirationTime: expireTime,
}

Expand Down

0 comments on commit 8767f0b

Please sign in to comment.