diff --git a/internal/imports/imports_linux.go b/internal/imports/imports_linux.go index e9fdea5..c7c92bb 100644 --- a/internal/imports/imports_linux.go +++ b/internal/imports/imports_linux.go @@ -19,6 +19,7 @@ import ( _ "k8s.io/client-go/kubernetes" _ "k8s.io/client-go/kubernetes/fake" _ "k8s.io/client-go/rest" + _ "net" _ "os" _ "os/signal" _ "path/filepath" diff --git a/internal/mapipwriter/mapipwriter.go b/internal/mapipwriter/mapipwriter.go index 24cb88b..f63ca94 100644 --- a/internal/mapipwriter/mapipwriter.go +++ b/internal/mapipwriter/mapipwriter.go @@ -98,6 +98,7 @@ func (m *MapIPWriter) Start(ctx context.Context, eventCh <-chan watch.Event) { delete(m.internalToExternalIP, internalIP) default: m.internalToExternalIP[internalIP] = externalIP + m.internalToExternalIP[externalIP] = internalIP } m.exec.AsyncExec(func() { m.writeToFile(ctx) diff --git a/internal/mapipwriter/mapipwriter_test.go b/internal/mapipwriter/mapipwriter_test.go index bf2c42b..d8f59d0 100644 --- a/internal/mapipwriter/mapipwriter_test.go +++ b/internal/mapipwriter/mapipwriter_test.go @@ -109,6 +109,6 @@ func Test_MapWriter(t *testing.T) { return false } s := strings.TrimSpace(string(b)) - return s == "127.0.0.1: 148.142.120.1" + return s == "127.0.0.1: 148.142.120.1\n148.142.120.1: 127.0.0.1" }, time.Second, time.Millisecond*100) } diff --git a/main.go b/main.go index b269d9e..0b01d90 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ package main import ( "context" + "net" "os" "os/signal" "syscall" @@ -31,6 +32,7 @@ import ( "github.com/networkservicemesh/sdk/pkg/tools/log" "github.com/networkservicemesh/sdk/pkg/tools/log/logruslogger" + corev1 "k8s.io/api/core/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/watch" "k8s.io/client-go/kubernetes" @@ -40,6 +42,7 @@ import ( // Config represents the configuration for cmd-map-ip-k8s application type Config struct { OutputPath string `default:"OutputPath" desc:"Path to writing map of internal to extenrnal ips"` + NodeName string `default:"" desc:"The name of node where application is running"` } func main() { @@ -111,7 +114,19 @@ func main() { go func() { for i := 0; i < len(list.Items); i++ { eventsCh <- watch.Event{Type: watch.Added, Object: &list.Items[i]} + if list.Items[i].Name == conf.NodeName { + n := list.Items[i] + for j := 0; j < len(n.Status.Addresses); j++ { + addr := &n.Status.Addresses[j] + if addr.Type == corev1.NodeInternalIP { + addr.Address = getPublicIP(ctx) + break + } + } + eventsCh <- watch.Event{Type: watch.Added, Object: &n} + } } + for event := range watchClient.ResultChan() { eventsCh <- event } @@ -121,3 +136,23 @@ func main() { <-ctx.Done() } + +func getPublicIP(ctx context.Context) string { + addrs, err := net.InterfaceAddrs() + if err != nil { + log.FromContext(ctx).Errorf("InterfaceAddrs: %v", err.Error()) + return "" + } + for _, a := range addrs { + if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { + if ip := ipnet.IP.To4(); ip != nil { + return ip.String() + } + if ip := ipnet.IP.To16(); ip != nil { + return ip.String() + } + } + } + log.FromContext(ctx).Warn("not found public ip") + return "" +}