Skip to content

Commit 7259742

Browse files
committed
Make Endpoints controller always canonicalize the IPs it writes out
Also, fix the unit tests to not sometimes generate alleged pod IPs like "1.2.3.999".
1 parent 1bd3d34 commit 7259742

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

pkg/controller/endpoint/endpoints_controller.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,12 @@ func podToEndpointAddressForService(svc *v1.Service, pod *v1.Pod) (*v1.EndpointA
217217

218218
wantIPv6 := svc.Spec.IPFamilies[0] == v1.IPv6Protocol
219219

220-
// find an ip that matches the family
220+
// Find an IP that matches the family. We parse and restringify the IP in case the
221+
// value on the Pod is in an irregular format.
221222
for _, podIP := range pod.Status.PodIPs {
222-
if wantIPv6 == utilnet.IsIPv6String(podIP.IP) {
223-
endpointIP = podIP.IP
223+
ip := utilnet.ParseIPSloppy(podIP.IP)
224+
if wantIPv6 == utilnet.IsIPv6(ip) {
225+
endpointIP = ip.String()
224226
break
225227
}
226228
}

pkg/controller/endpoint/endpoints_controller_test.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net/http/httptest"
2424
"reflect"
2525
"strconv"
26+
"strings"
2627
"testing"
2728
"time"
2829

@@ -92,9 +93,12 @@ func testPod(namespace string, id int, nPorts int, isReady bool, ipFamilies []v1
9293
for _, family := range ipFamilies {
9394
var ip string
9495
if family == v1.IPv4Protocol {
95-
ip = fmt.Sprintf("1.2.3.%d", 4+id)
96+
// if id=1, ip=1.2.3.4
97+
// if id=2, ip=1.2.3.5
98+
// if id=999, ip=1.2.6.235
99+
ip = fmt.Sprintf("1.2.%d.%d", 3+((4+id)/256), (4+id)%256)
96100
} else {
97-
ip = fmt.Sprintf("2000::%d", 4+id)
101+
ip = fmt.Sprintf("2000::%x", 4+id)
98102
}
99103
p.Status.PodIPs = append(p.Status.PodIPs, v1.PodIP{IP: ip})
100104
}
@@ -111,6 +115,13 @@ func addPods(store cache.Store, namespace string, nPods int, nPorts int, nNotRea
111115
}
112116
}
113117

118+
func addBadIPPod(store cache.Store, namespace string, ipFamilies []v1.IPFamily) {
119+
pod := testPod(namespace, 0, 1, true, ipFamilies)
120+
pod.Status.PodIPs[0].IP = "0" + pod.Status.PodIPs[0].IP
121+
pod.Status.PodIP = pod.Status.PodIPs[0].IP
122+
store.Add(pod)
123+
}
124+
114125
func addNotReadyPodsWithSpecifiedRestartPolicyAndPhase(store cache.Store, namespace string, nPods int, nPorts int, restartPolicy v1.RestartPolicy, podPhase v1.PodPhase) {
115126
for i := 0; i < nPods; i++ {
116127
p := &v1.Pod{
@@ -1476,7 +1487,9 @@ func TestPodToEndpointAddressForService(t *testing.T) {
14761487
t.Run(tc.name, func(t *testing.T) {
14771488
podStore := cache.NewStore(cache.DeletionHandlingMetaNamespaceKeyFunc)
14781489
ns := "test"
1479-
addPods(podStore, ns, 1, 1, 0, tc.ipFamilies)
1490+
// We use addBadIPPod to test that podToEndpointAddressForService
1491+
// fixes up the bad IP.
1492+
addBadIPPod(podStore, ns, tc.ipFamilies)
14801493
pods := podStore.List()
14811494
if len(pods) != 1 {
14821495
t.Fatalf("podStore size: expected: %d, got: %d", 1, len(pods))
@@ -1499,6 +1512,9 @@ func TestPodToEndpointAddressForService(t *testing.T) {
14991512
if utilnet.IsIPv6String(epa.IP) != (tc.expectedEndpointFamily == ipv6) {
15001513
t.Fatalf("IP: expected %s, got: %s", tc.expectedEndpointFamily, epa.IP)
15011514
}
1515+
if strings.HasPrefix(epa.IP, "0") {
1516+
t.Fatalf("IP: expected valid, got: %s", epa.IP)
1517+
}
15021518
if *(epa.NodeName) != pod.Spec.NodeName {
15031519
t.Fatalf("NodeName: expected: %s, got: %s", pod.Spec.NodeName, *(epa.NodeName))
15041520
}

0 commit comments

Comments
 (0)