Skip to content

Commit 6917c48

Browse files
agusdallalbaGacko
andauthored
Status: Add support for multiple Node IP addresses. (#14049)
Co-authored-by: Marco Ebert <[email protected]>
1 parent 652bfff commit 6917c48

File tree

3 files changed

+136
-39
lines changed

3 files changed

+136
-39
lines changed

internal/ingress/status/status.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,11 @@ func (s *statusSync) runningAddresses() ([]v1.IngressLoadBalancerIngress, error)
225225
continue
226226
}
227227

228-
name := k8s.GetNodeIPOrName(s.Client, pod.Spec.NodeName, s.UseNodeInternalIP)
229-
if !stringInIngresses(name, addrs) {
230-
addrs = append(addrs, nameOrIPToLoadBalancerIngress(name))
228+
theseAddresses := k8s.GetNodeIPs(s.Client, pod.Spec.NodeName, s.UseNodeInternalIP)
229+
for _, thisAddress := range theseAddresses {
230+
if !stringInIngresses(thisAddress, addrs) {
231+
addrs = append(addrs, nameOrIPToLoadBalancerIngress(thisAddress))
232+
}
231233
}
232234
}
233235

internal/k8s/main.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,37 +42,31 @@ func ParseNameNS(input string) (ns, name string, err error) {
4242
return nsName[0], nsName[1], nil
4343
}
4444

45-
// GetNodeIPOrName returns the IP address or the name of a node in the cluster
46-
func GetNodeIPOrName(kubeClient clientset.Interface, name string, useInternalIP bool) string {
45+
// GetNodeIPs returns the IP addresses of a node in the cluster
46+
func GetNodeIPs(kubeClient clientset.Interface, name string, useInternalIP bool) []string {
4747
node, err := kubeClient.CoreV1().Nodes().Get(context.TODO(), name, metav1.GetOptions{})
4848
if err != nil {
4949
klog.ErrorS(err, "Error getting node", "name", name)
50-
return ""
50+
return []string{}
5151
}
5252

53-
defaultOrInternalIP := ""
53+
externalIPs := []string{}
54+
internalIPs := []string{}
55+
5456
for _, address := range node.Status.Addresses {
55-
if address.Type == apiv1.NodeInternalIP {
56-
if address.Address != "" {
57-
defaultOrInternalIP = address.Address
58-
break
59-
}
57+
if address.Type == apiv1.NodeInternalIP && address.Address != "" {
58+
internalIPs = append(internalIPs, address.Address)
59+
}
60+
if address.Type == apiv1.NodeExternalIP && address.Address != "" {
61+
externalIPs = append(externalIPs, address.Address)
6062
}
6163
}
6264

63-
if useInternalIP {
64-
return defaultOrInternalIP
65-
}
66-
67-
for _, address := range node.Status.Addresses {
68-
if address.Type == apiv1.NodeExternalIP {
69-
if address.Address != "" {
70-
return address.Address
71-
}
72-
}
65+
if useInternalIP || len(externalIPs) == 0 {
66+
return internalIPs
7367
}
7468

75-
return defaultOrInternalIP
69+
return externalIPs
7670
}
7771

7872
var (

internal/k8s/main_test.go

Lines changed: 117 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package k8s
1818

1919
import (
20+
"slices"
2021
"testing"
2122

2223
apiv1 "k8s.io/api/core/v1"
@@ -60,13 +61,15 @@ func TestGetNodeIP(t *testing.T) {
6061
name string
6162
cs *testclient.Clientset
6263
nodeName string
63-
ea string
64+
ea []string
6465
useInternalIP bool
6566
}{
6667
{
6768
"empty node list",
6869
testclient.NewSimpleClientset(),
69-
"demo", "", true,
70+
"demo",
71+
[]string{},
72+
true,
7073
},
7174
{
7275
"node does not exist",
@@ -82,10 +85,12 @@ func TestGetNodeIP(t *testing.T) {
8285
},
8386
},
8487
},
85-
}}}), "notexistnode", "", true,
88+
}}}), "notexistnode",
89+
[]string{},
90+
true,
8691
},
8792
{
88-
"node exist and only has an internal IP address (useInternalIP=false)",
93+
"node exists and only has an internal IP address (useInternalIP=false)",
8994
testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
9095
ObjectMeta: metav1.ObjectMeta{
9196
Name: "demo",
@@ -98,10 +103,56 @@ func TestGetNodeIP(t *testing.T) {
98103
},
99104
},
100105
},
101-
}}}), "demo", "10.0.0.1", false,
106+
}}}), "demo",
107+
[]string{"10.0.0.1"},
108+
false,
102109
},
103110
{
104-
"node exist and only has an internal IP address",
111+
"node exists has an internal IP address and an empty external IP address (useInternalIP=false)",
112+
testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
113+
ObjectMeta: metav1.ObjectMeta{
114+
Name: "demo",
115+
},
116+
Status: apiv1.NodeStatus{
117+
Addresses: []apiv1.NodeAddress{
118+
{
119+
Type: apiv1.NodeExternalIP,
120+
Address: "",
121+
},
122+
{
123+
Type: apiv1.NodeInternalIP,
124+
Address: "10.0.0.1",
125+
},
126+
},
127+
},
128+
}}}), "demo",
129+
[]string{"10.0.0.1"},
130+
false,
131+
},
132+
{
133+
"node exists and has two internal IP address (useInternalIP=false)",
134+
testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
135+
ObjectMeta: metav1.ObjectMeta{
136+
Name: "demo",
137+
},
138+
Status: apiv1.NodeStatus{
139+
Addresses: []apiv1.NodeAddress{
140+
{
141+
Type: apiv1.NodeInternalIP,
142+
Address: "10.0.0.1",
143+
},
144+
{
145+
Type: apiv1.NodeInternalIP,
146+
Address: "fd00::1",
147+
},
148+
},
149+
},
150+
}}}), "demo",
151+
[]string{"10.0.0.1", "fd00::1"},
152+
false,
153+
},
154+
{
155+
"node exists and only has an internal IP address",
105156
testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
106157
ObjectMeta: metav1.ObjectMeta{
107158
Name: "demo",
@@ -114,7 +165,31 @@ func TestGetNodeIP(t *testing.T) {
114165
},
115166
},
116167
},
117-
}}}), "demo", "10.0.0.1", true,
168+
}}}), "demo",
169+
[]string{"10.0.0.1"},
170+
true,
171+
},
172+
{
173+
"node exists and has two internal IP address",
174+
testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
175+
ObjectMeta: metav1.ObjectMeta{
176+
Name: "demo",
177+
},
178+
Status: apiv1.NodeStatus{
179+
Addresses: []apiv1.NodeAddress{
180+
{
181+
Type: apiv1.NodeInternalIP,
182+
Address: "10.0.0.1",
183+
},
184+
{
185+
Type: apiv1.NodeInternalIP,
186+
Address: "fd00::1",
187+
},
188+
},
189+
},
190+
}}}), "demo",
191+
[]string{"10.0.0.1", "fd00::1"},
192+
true,
118193
},
119194
{
120195
"node exist and only has an external IP address",
@@ -130,7 +205,27 @@ func TestGetNodeIP(t *testing.T) {
130205
},
131206
},
132207
},
133-
}}}), "demo", "10.0.0.1", false,
208+
}}}), "demo",
209+
[]string{"10.0.0.1"},
210+
false,
211+
},
212+
{
213+
"node exist and only has an external IP address (useInternalIP=true)",
214+
testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
215+
ObjectMeta: metav1.ObjectMeta{
216+
Name: "demo",
217+
},
218+
Status: apiv1.NodeStatus{
219+
Addresses: []apiv1.NodeAddress{
220+
{
221+
Type: apiv1.NodeExternalIP,
222+
Address: "10.0.0.1",
223+
},
224+
},
225+
},
226+
}}}), "demo",
227+
[]string{},
228+
true,
134229
},
135230
{
136231
"multiple nodes - choose the right one",
@@ -162,10 +257,12 @@ func TestGetNodeIP(t *testing.T) {
162257
},
163258
},
164259
}}),
165-
"demo2", "10.0.0.2", true,
260+
"demo2",
261+
[]string{"10.0.0.2"},
262+
true,
166263
},
167264
{
168-
"node with both IP internal and external IP address - returns external IP",
265+
"node with both internal and external IP address - returns external IP",
169266
testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
170267
ObjectMeta: metav1.ObjectMeta{
171268
Name: "demo",
@@ -182,10 +279,12 @@ func TestGetNodeIP(t *testing.T) {
182279
},
183280
},
184281
}}}),
185-
"demo", "10.0.0.2", false,
282+
"demo",
283+
[]string{"10.0.0.2"},
284+
false,
186285
},
187286
{
188-
"node with both IP internal and external IP address - returns internal IP",
287+
"node with both internal and external IP address - returns internal IP",
189288
testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
190289
ObjectMeta: metav1.ObjectMeta{
191290
Name: "demo",
@@ -202,14 +301,16 @@ func TestGetNodeIP(t *testing.T) {
202301
},
203302
},
204303
}}}),
205-
"demo", "10.0.0.2", true,
304+
"demo",
305+
[]string{"10.0.0.2"},
306+
true,
206307
},
207308
}
208309

209310
for _, fk := range fKNodes {
210-
address := GetNodeIPOrName(fk.cs, fk.nodeName, fk.useInternalIP)
211-
if address != fk.ea {
212-
t.Errorf("%v - expected %s, but returned %s", fk.name, fk.ea, address)
311+
addresses := GetNodeIPs(fk.cs, fk.nodeName, fk.useInternalIP)
312+
if !slices.Equal(addresses, fk.ea) {
313+
t.Errorf("%v - expected %v, but returned %v", fk.name, fk.ea, addresses)
213314
}
214315
}
215316
}

0 commit comments

Comments
 (0)