Skip to content

Commit 5822bb5

Browse files
authored
Merge pull request kubernetes#130101 from danwinship/controller-ip-canonicalization
Canonicalize IPs written out by controllers
2 parents 9bf60d0 + e31a398 commit 5822bb5

File tree

11 files changed

+170
-179
lines changed

11 files changed

+170
-179
lines changed

pkg/controller/endpoint/endpoints_controller.go

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -214,39 +214,15 @@ func (e *Controller) addPod(obj interface{}) {
214214

215215
func podToEndpointAddressForService(svc *v1.Service, pod *v1.Pod) (*v1.EndpointAddress, error) {
216216
var endpointIP string
217-
ipFamily := v1.IPv4Protocol
218217

219-
if len(svc.Spec.IPFamilies) > 0 {
220-
// controller is connected to an api-server that correctly sets IPFamilies
221-
ipFamily = svc.Spec.IPFamilies[0] // this works for headful and headless
222-
} else {
223-
// controller is connected to an api server that does not correctly
224-
// set IPFamilies (e.g. old api-server during an upgrade)
225-
// TODO (khenidak): remove by when the possibility of upgrading
226-
// from a cluster that does not support dual stack is nil
227-
if len(svc.Spec.ClusterIP) > 0 && svc.Spec.ClusterIP != v1.ClusterIPNone {
228-
// headful service. detect via service clusterIP
229-
if utilnet.IsIPv6String(svc.Spec.ClusterIP) {
230-
ipFamily = v1.IPv6Protocol
231-
}
232-
} else {
233-
// Since this is a headless service we use podIP to identify the family.
234-
// This assumes that status.PodIP is assigned correctly (follows pod cidr and
235-
// pod cidr list order is same as service cidr list order). The expectation is
236-
// this is *most probably* the case.
237-
238-
// if the family was incorrectly identified then this will be corrected once the
239-
// upgrade is completed (controller connects to api-server that correctly defaults services)
240-
if utilnet.IsIPv6String(pod.Status.PodIP) {
241-
ipFamily = v1.IPv6Protocol
242-
}
243-
}
244-
}
218+
wantIPv6 := svc.Spec.IPFamilies[0] == v1.IPv6Protocol
245219

246-
// 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.
247222
for _, podIP := range pod.Status.PodIPs {
248-
if (ipFamily == v1.IPv6Protocol) == utilnet.IsIPv6String(podIP.IP) {
249-
endpointIP = podIP.IP
223+
ip := utilnet.ParseIPSloppy(podIP.IP)
224+
if wantIPv6 == utilnet.IsIPv6(ip) {
225+
endpointIP = ip.String()
250226
break
251227
}
252228
}

0 commit comments

Comments
 (0)