Skip to content

Commit 5a529aa

Browse files
authored
Merge pull request kubernetes#91399 from danwinship/endpoint-ipfamily
multiple IPv6/dual-stack endpoint fixes
2 parents 5feab0a + e46572e commit 5a529aa

File tree

10 files changed

+423
-375
lines changed

10 files changed

+423
-375
lines changed

pkg/controller/endpoint/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ go_test(
4848
"//pkg/api/v1/endpoints:go_default_library",
4949
"//pkg/apis/core:go_default_library",
5050
"//pkg/controller:go_default_library",
51-
"//pkg/controller/util/endpoint:go_default_library",
5251
"//pkg/features:go_default_library",
5352
"//staging/src/k8s.io/api/core/v1:go_default_library",
5453
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
@@ -65,6 +64,7 @@ go_test(
6564
"//staging/src/k8s.io/client-go/tools/cache:go_default_library",
6665
"//staging/src/k8s.io/client-go/util/testing:go_default_library",
6766
"//staging/src/k8s.io/component-base/featuregate/testing:go_default_library",
67+
"//vendor/k8s.io/utils/net:go_default_library",
6868
"//vendor/k8s.io/utils/pointer:go_default_library",
6969
],
7070
)

pkg/controller/endpoint/endpoints_controller.go

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package endpoint
1919
import (
2020
"context"
2121
"fmt"
22-
"reflect"
2322
"strconv"
2423
"time"
2524

@@ -213,64 +212,43 @@ func (e *EndpointController) addPod(obj interface{}) {
213212
}
214213

215214
func podToEndpointAddressForService(svc *v1.Service, pod *v1.Pod) (*v1.EndpointAddress, error) {
215+
var endpointIP string
216+
216217
if !utilfeature.DefaultFeatureGate.Enabled(features.IPv6DualStack) {
217-
return podToEndpointAddress(pod), nil
218-
}
219-
220-
// api-server service controller ensured that the service got the correct IP Family
221-
// according to user setup, here we only need to match EndPoint IPs' family to service
222-
// actual IP family. as in, we don't need to check service.IPFamily
223-
224-
ipv6ClusterIP := utilnet.IsIPv6String(svc.Spec.ClusterIP)
225-
for _, podIP := range pod.Status.PodIPs {
226-
ipv6PodIP := utilnet.IsIPv6String(podIP.IP)
227-
// same family?
228-
// TODO (khenidak) when we remove the max of 2 PodIP limit from pods
229-
// we will have to return multiple endpoint addresses
230-
if ipv6ClusterIP == ipv6PodIP {
231-
return &v1.EndpointAddress{
232-
IP: podIP.IP,
233-
NodeName: &pod.Spec.NodeName,
234-
TargetRef: &v1.ObjectReference{
235-
Kind: "Pod",
236-
Namespace: pod.ObjectMeta.Namespace,
237-
Name: pod.ObjectMeta.Name,
238-
UID: pod.ObjectMeta.UID,
239-
ResourceVersion: pod.ObjectMeta.ResourceVersion,
240-
}}, nil
218+
// In a legacy cluster, the pod IP is guaranteed to be usable
219+
endpointIP = pod.Status.PodIP
220+
} else {
221+
ipv6Service := endpointutil.IsIPv6Service(svc)
222+
for _, podIP := range pod.Status.PodIPs {
223+
ipv6PodIP := utilnet.IsIPv6String(podIP.IP)
224+
if ipv6Service == ipv6PodIP {
225+
endpointIP = podIP.IP
226+
break
227+
}
228+
}
229+
if endpointIP == "" {
230+
return nil, fmt.Errorf("failed to find a matching endpoint for service %v", svc.Name)
241231
}
242232
}
243-
return nil, fmt.Errorf("failed to find a matching endpoint for service %v", svc.Name)
244-
}
245233

246-
func podToEndpointAddress(pod *v1.Pod) *v1.EndpointAddress {
247234
return &v1.EndpointAddress{
248-
IP: pod.Status.PodIP,
235+
IP: endpointIP,
249236
NodeName: &pod.Spec.NodeName,
250237
TargetRef: &v1.ObjectReference{
251238
Kind: "Pod",
252239
Namespace: pod.ObjectMeta.Namespace,
253240
Name: pod.ObjectMeta.Name,
254241
UID: pod.ObjectMeta.UID,
255242
ResourceVersion: pod.ObjectMeta.ResourceVersion,
256-
}}
257-
}
258-
259-
func endpointChanged(pod1, pod2 *v1.Pod) bool {
260-
endpointAddress1 := podToEndpointAddress(pod1)
261-
endpointAddress2 := podToEndpointAddress(pod2)
262-
263-
endpointAddress1.TargetRef.ResourceVersion = ""
264-
endpointAddress2.TargetRef.ResourceVersion = ""
265-
266-
return !reflect.DeepEqual(endpointAddress1, endpointAddress2)
243+
},
244+
}, nil
267245
}
268246

269247
// When a pod is updated, figure out what services it used to be a member of
270248
// and what services it will be a member of, and enqueue the union of these.
271249
// old and cur must be *v1.Pod types.
272250
func (e *EndpointController) updatePod(old, cur interface{}) {
273-
services := endpointutil.GetServicesToUpdateOnPodChange(e.serviceLister, e.serviceSelectorCache, old, cur, endpointChanged)
251+
services := endpointutil.GetServicesToUpdateOnPodChange(e.serviceLister, e.serviceSelectorCache, old, cur)
274252
for key := range services {
275253
e.queue.AddAfter(key, e.endpointUpdatesBatchPeriod)
276254
}

0 commit comments

Comments
 (0)