Skip to content

Commit 4e7cc8d

Browse files
committed
feat: update the ip parsing for the nodes
updated the logic for the cluster info to use net package and added unit tests for dualstack behavior to select first ip address Signed-off-by: ehila <[email protected]>
1 parent 0e9d61f commit 4e7cc8d

File tree

2 files changed

+100
-7
lines changed

2 files changed

+100
-7
lines changed

pkg/tnf/pkg/config/cluster.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"context"
55
"fmt"
6+
"net"
67
"sort"
78

89
corev1 "k8s.io/api/core/v1"
@@ -53,12 +54,24 @@ func GetClusterConfig(ctx context.Context, kubeClient kubernetes.Interface) (Clu
5354
return clusterCfg, nil
5455
}
5556

57+
// getInternalIP returns the internal ip address of the node.
58+
// If no internal ip is found, returns the first ip address as a fallback.
59+
// If address list is empty, returns the empty string as a fallback.
5660
func getInternalIP(addresses []corev1.NodeAddress) string {
61+
62+
if len(addresses) == 0 {
63+
return ""
64+
}
65+
5766
for _, addr := range addresses {
58-
if addr.Type == corev1.NodeInternalIP {
59-
return addr.Address
67+
switch addr.Type {
68+
case corev1.NodeInternalIP:
69+
ip := net.ParseIP(addr.Address)
70+
if ip != nil {
71+
return ip.String()
72+
}
6073
}
6174
}
62-
// fallback...
75+
6376
return addresses[0].Address
6477
}

pkg/tnf/pkg/config/cluster_test.go

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestGetClusterConfig(t *testing.T) {
3838
Addresses: []corev1.NodeAddress{
3939
// ensure that internal IP is used
4040
{Type: corev1.NodeExternalIP, Address: "xxx"},
41-
{Type: corev1.NodeInternalIP, Address: "IP1"},
41+
{Type: corev1.NodeInternalIP, Address: "192.168.111.12"},
4242
},
4343
},
4444
},
@@ -52,16 +52,96 @@ func TestGetClusterConfig(t *testing.T) {
5252
Status: corev1.NodeStatus{
5353
Addresses: []corev1.NodeAddress{
5454
// fallback to 1st IP incase no internal IP is set
55-
{Type: corev1.NodeExternalIP, Address: "IP2"},
55+
{Type: corev1.NodeExternalIP, Address: "192.168.111.13"},
5656
},
5757
},
5858
},
5959
}),
6060
want: ClusterConfig{
6161
NodeName1: "test1",
6262
NodeName2: "test2",
63-
NodeIP1: "IP1",
64-
NodeIP2: "IP2",
63+
NodeIP1: "192.168.111.12",
64+
NodeIP2: "192.168.111.13",
65+
},
66+
wantErr: false,
67+
},
68+
{
69+
name: "default-ipv6",
70+
args: getArgs(t, []*corev1.Node{
71+
{
72+
ObjectMeta: metav1.ObjectMeta{
73+
Name: "test1",
74+
Labels: map[string]string{
75+
"node-role.kubernetes.io/master": "",
76+
},
77+
},
78+
Status: corev1.NodeStatus{
79+
Addresses: []corev1.NodeAddress{
80+
{Type: corev1.NodeExternalIP, Address: "xxx"},
81+
{Type: corev1.NodeInternalIP, Address: "fd2e:6f44:5dd8:c956::16"},
82+
},
83+
},
84+
},
85+
{
86+
ObjectMeta: metav1.ObjectMeta{
87+
Name: "test2",
88+
Labels: map[string]string{
89+
"node-role.kubernetes.io/master": "",
90+
},
91+
},
92+
Status: corev1.NodeStatus{
93+
Addresses: []corev1.NodeAddress{
94+
{Type: corev1.NodeInternalIP, Address: "fd2e:6f44:5dd8:c956::17"},
95+
},
96+
},
97+
},
98+
}),
99+
want: ClusterConfig{
100+
NodeName1: "test1",
101+
NodeName2: "test2",
102+
NodeIP1: "fd2e:6f44:5dd8:c956::16",
103+
NodeIP2: "fd2e:6f44:5dd8:c956::17",
104+
},
105+
wantErr: false,
106+
},
107+
{
108+
name: "default-dualstack-select-first-ip",
109+
args: getArgs(t, []*corev1.Node{
110+
{
111+
ObjectMeta: metav1.ObjectMeta{
112+
Name: "test1",
113+
Labels: map[string]string{
114+
"node-role.kubernetes.io/master": "",
115+
},
116+
},
117+
Status: corev1.NodeStatus{
118+
Addresses: []corev1.NodeAddress{
119+
{Type: corev1.NodeExternalIP, Address: "xxx"},
120+
{Type: corev1.NodeInternalIP, Address: "192.168.111.12"},
121+
{Type: corev1.NodeInternalIP, Address: "fd2e:6f44:5dd8:c956::16"},
122+
},
123+
},
124+
},
125+
{
126+
ObjectMeta: metav1.ObjectMeta{
127+
Name: "test2",
128+
Labels: map[string]string{
129+
"node-role.kubernetes.io/master": "",
130+
},
131+
},
132+
Status: corev1.NodeStatus{
133+
Addresses: []corev1.NodeAddress{
134+
{Type: corev1.NodeInternalIP, Address: "fd2e:6f44:5dd8:c956::17"},
135+
{Type: corev1.NodeInternalIP, Address: "192.168.111.13"},
136+
},
137+
},
138+
},
139+
}),
140+
want: ClusterConfig{
141+
NodeName1: "test1",
142+
NodeName2: "test2",
143+
NodeIP1: "192.168.111.12",
144+
NodeIP2: "fd2e:6f44:5dd8:c956::17",
65145
},
66146
wantErr: false,
67147
},

0 commit comments

Comments
 (0)