Skip to content

Commit

Permalink
Merge pull request #10 from denis-tingaikin/add-pod-ip-mapping
Browse files Browse the repository at this point in the history
fix: add mapping of current POD IP to external IP
  • Loading branch information
edwarnicke authored Jul 14, 2021
2 parents bcdbe43 + 030e4ab commit 78f849b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions internal/imports/imports_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions internal/mapipwriter/mapipwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/mapipwriter/mapipwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
35 changes: 35 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"context"
"net"
"os"
"os/signal"
"syscall"
Expand All @@ -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"
Expand All @@ -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() {
Expand Down Expand Up @@ -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
}
Expand All @@ -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 ""
}

0 comments on commit 78f849b

Please sign in to comment.