Skip to content

Commit 6512de7

Browse files
committed
Make EndpointSlice mirroring controller always canonicalize the IPs it writes out
(Also rearrange some code to avoid parsing the IP twice.)
1 parent 29be52b commit 6512de7

File tree

4 files changed

+19
-21
lines changed

4 files changed

+19
-21
lines changed

pkg/controller/endpointslicemirroring/reconciler_helpers.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ func (d *desiredCalc) initPorts(subsetPorts []v1.EndpointPort) multiAddrTypePort
8787
// addAddress adds an EndpointAddress to the desired state if it is valid. It
8888
// returns false if the address was invalid.
8989
func (d *desiredCalc) addAddress(address v1.EndpointAddress, multiKey multiAddrTypePortMapKey, ready bool) bool {
90-
endpoint := addressToEndpoint(address, ready)
91-
addrType := getAddressType(address.IP)
90+
endpoint, addrType := addressToEndpoint(address, ready)
9291
if addrType == nil {
9392
return false
9493
}

pkg/controller/endpointslicemirroring/reconciler_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,7 @@ func expectMatchingAddresses(t *testing.T, epSubset corev1.EndpointSubset, esEnd
12451245
expectedEndpoints := map[string]addressInfo{}
12461246

12471247
for _, address := range epSubset.Addresses {
1248-
at := getAddressType(address.IP)
1248+
_, at := addressToEndpoint(address, true)
12491249
if at != nil && *at == addrType && len(expectedEndpoints) < maxEndpointsPerSubset {
12501250
expectedEndpoints[address.IP] = addressInfo{
12511251
ready: true,
@@ -1255,7 +1255,7 @@ func expectMatchingAddresses(t *testing.T, epSubset corev1.EndpointSubset, esEnd
12551255
}
12561256

12571257
for _, address := range epSubset.NotReadyAddresses {
1258-
at := getAddressType(address.IP)
1258+
_, at := addressToEndpoint(address, true)
12591259
if at != nil && *at == addrType && len(expectedEndpoints) < maxEndpointsPerSubset {
12601260
expectedEndpoints[address.IP] = addressInfo{
12611261
ready: false,

pkg/controller/endpointslicemirroring/utils.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,6 @@ func (pk addrTypePortMapKey) addressType() discovery.AddressType {
4949
return discovery.AddressTypeIPv4
5050
}
5151

52-
func getAddressType(address string) *discovery.AddressType {
53-
ip := netutils.ParseIPSloppy(address)
54-
if ip == nil {
55-
return nil
56-
}
57-
addressType := discovery.AddressTypeIPv4
58-
if ip.To4() == nil {
59-
addressType = discovery.AddressTypeIPv6
60-
}
61-
return &addressType
62-
}
63-
6452
// newEndpointSlice returns an EndpointSlice generated from an Endpoints
6553
// resource, ports, and address type.
6654
func newEndpointSlice(endpoints *corev1.Endpoints, ports []discovery.EndpointPort, addrType discovery.AddressType, sliceName string) *discovery.EndpointSlice {
@@ -115,10 +103,20 @@ func getEndpointSlicePrefix(serviceName string) string {
115103
}
116104

117105
// addressToEndpoint converts an address from an Endpoints resource to an
118-
// EndpointSlice endpoint.
119-
func addressToEndpoint(address corev1.EndpointAddress, ready bool) *discovery.Endpoint {
106+
// EndpointSlice endpoint and AddressType.
107+
func addressToEndpoint(address corev1.EndpointAddress, ready bool) (*discovery.Endpoint, *discovery.AddressType) {
108+
ip := netutils.ParseIPSloppy(address.IP)
109+
if ip == nil {
110+
return nil, nil
111+
}
112+
addressType := discovery.AddressTypeIPv4
113+
if ip.To4() == nil {
114+
addressType = discovery.AddressTypeIPv6
115+
}
116+
120117
endpoint := &discovery.Endpoint{
121-
Addresses: []string{address.IP},
118+
// We parse and restringify the Endpoints IP in case it is in an irregular format.
119+
Addresses: []string{ip.String()},
122120
Conditions: discovery.EndpointConditions{
123121
Ready: &ready,
124122
},
@@ -132,7 +130,7 @@ func addressToEndpoint(address corev1.EndpointAddress, ready bool) *discovery.En
132130
endpoint.Hostname = &address.Hostname
133131
}
134132

135-
return endpoint
133+
return endpoint, &addressType
136134
}
137135

138136
// epPortsToEpsPorts converts ports from an Endpoints resource to ports for an

pkg/controller/endpointslicemirroring/utils_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,9 @@ func TestAddressToEndpoint(t *testing.T) {
217217
NodeName: pointer.String("node-abc"),
218218
}
219219

220-
ep := addressToEndpoint(epAddress, ready)
220+
ep, addrType := addressToEndpoint(epAddress, ready)
221221
assert.EqualValues(t, expectedEndpoint, *ep)
222+
assert.EqualValues(t, discovery.AddressTypeIPv4, *addrType)
222223
}
223224

224225
// Test helpers

0 commit comments

Comments
 (0)