Skip to content

Commit

Permalink
Merge pull request #173 from d-uzlov/fix-reverse-translation
Browse files Browse the repository at this point in the history
Remove reverse IP translation
  • Loading branch information
denis-tingaikin authored Oct 23, 2023
2 parents 5775a45 + e3b9217 commit ebda296
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 35 deletions.
22 changes: 16 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ func translationFromNode(e watch.Event) []mapipwriter.Event {

var node = e.Object.(*corev1.Node)

// map internal ip on itself, in case we don't have an external IP
for i := 0; i < len(node.Status.Addresses); i++ {
if node.Status.Addresses[i].Type == corev1.NodeInternalIP {
result = append(result, mapipwriter.Event{
Expand All @@ -308,19 +309,28 @@ func translationFromNode(e watch.Event) []mapipwriter.Event {
}
}

// if we have external IPs, instead map internal IP to external
for i := 0; i < len(node.Status.Addresses); i++ {
if node.Status.Addresses[i].Type == corev1.NodeExternalIP {
var l = len(result)
for j := 0; j < l; j++ {
for j := 0; j < len(result); j++ {
result[j].To = node.Status.Addresses[i].Address
result = append(result, mapipwriter.Event{
Type: e.Type,
Translation: result[j].Reverse(),
})
}
break
}
}

// map external IP to itself, in case we want to send data from external IP
for i := 0; i < len(node.Status.Addresses); i++ {
if node.Status.Addresses[i].Type == corev1.NodeExternalIP {
result = append(result, mapipwriter.Event{
Type: e.Type,
Translation: mapipwriter.Translation{
From: node.Status.Addresses[i].Address,
To: node.Status.Addresses[i].Address,
},
})
}
}

return result
}
68 changes: 39 additions & 29 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,39 +51,41 @@ func Test_NodeHasChanged(t *testing.T) {
watcher := watch.NewFake()
client.PrependWatchReactor("nodes", k8stest.DefaultWatchReactor(watcher, nil))

nodes := map[string]string{
"1.1.1.1": "2.1.1.1",
"1.1.1.2": "2.1.1.2",
"1.1.1.3": "2.1.1.3",
}

var appCh = mainpkg.Start(ctx, conf, client)
go func() {
defer watcher.Stop()
time.Sleep(time.Millisecond * 30)
watcher.Add(&v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-1",
},
Status: v1.NodeStatus{
Addresses: []v1.NodeAddress{
{
Type: v1.NodeExternalIP,
Address: "1.1.1.1",
},
{
Type: v1.NodeInternalIP,
Address: "2.2.2.2",
for k, v := range nodes {
watcher.Add(&v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-" + k,
},
Status: v1.NodeStatus{
Addresses: []v1.NodeAddress{
{
Type: v1.NodeInternalIP,
Address: k,
},
{
Type: v1.NodeExternalIP,
Address: v,
},
},
},
},
})
})
}
}()

require.Len(t, appCh, 0)

require.Eventually(t, func() bool {
return verifyIPmap(
conf.OutputPath,
map[string]string{
"1.1.1.1": "2.2.2.2",
"2.2.2.2": "1.1.1.1",
},
)
return verifyIPmap(conf.OutputPath, nodes, true)
}, time.Second*2, time.Second/10)
}

Expand All @@ -106,7 +108,7 @@ func Test_ConfigMapLoadedFromStart(t *testing.T) {
Namespace: "nsm",
},
Data: map[string]string{
"config.yaml": "1.1.1.1: 2.2.2.2\n2.2.2.2: 1.1.1.1",
"config.yaml": "1.1.1.1: 2.1.1.1\n1.1.1.2: 2.1.1.2",
},
}, metav1.CreateOptions{})
require.NoError(t, err)
Expand All @@ -119,9 +121,10 @@ func Test_ConfigMapLoadedFromStart(t *testing.T) {
return verifyIPmap(
conf.OutputPath,
map[string]string{
"1.1.1.1": "2.2.2.2",
"2.2.2.2": "1.1.1.1",
"1.1.1.1": "2.1.1.1",
"1.1.1.2": "2.1.1.2",
},
false,
)
}, time.Second*2, time.Second/10)
}
Expand Down Expand Up @@ -151,7 +154,7 @@ func Test_ConfigMapHasChanged(t *testing.T) {
Name: "test",
},
Data: map[string]string{
"config.yaml": "1.1.1.1: 2.2.2.2\n2.2.2.2: 1.1.1.1",
"config.yaml": "1.1.1.1: 2.1.1.1\n1.1.1.2: 2.1.1.2",
},
})
}()
Expand All @@ -162,14 +165,15 @@ func Test_ConfigMapHasChanged(t *testing.T) {
return verifyIPmap(
conf.OutputPath,
map[string]string{
"1.1.1.1": "2.2.2.2",
"2.2.2.2": "1.1.1.1",
"1.1.1.1": "2.1.1.1",
"1.1.1.2": "2.1.1.2",
},
false,
)
}, time.Second*2, time.Second/10)
}

func verifyIPmap(p string, expected map[string]string) bool {
func verifyIPmap(p string, expected map[string]string, checkTargetMapping bool) bool {
// #nosec
b, err := os.ReadFile(p)

Expand All @@ -189,6 +193,12 @@ func verifyIPmap(p string, expected map[string]string) bool {
if m[k] != v {
return false
}
if checkTargetMapping {
// check that target values are mapped on themselves
if m[v] != v {
return false
}
}
}

return true
Expand Down

0 comments on commit ebda296

Please sign in to comment.