Skip to content

Commit 366dd4a

Browse files
committed
EndpointSlice and Endpoints should treat terminating pods the same
Signed-off-by: Andrew Sy Kim <[email protected]>
1 parent 7989ca4 commit 366dd4a

File tree

3 files changed

+67
-26
lines changed

3 files changed

+67
-26
lines changed

pkg/controller/endpointslice/reconciler.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,30 +86,32 @@ func (r *reconciler) reconcile(service *corev1.Service, pods []*corev1.Pod, exis
8686
numDesiredEndpoints := 0
8787

8888
for _, pod := range pods {
89-
if endpointutil.ShouldPodBeInEndpoints(pod) {
90-
endpointPorts := getEndpointPorts(service, pod)
91-
epHash := endpointutil.NewPortMapKey(endpointPorts)
92-
if _, ok := desiredEndpointsByPortMap[epHash]; !ok {
93-
desiredEndpointsByPortMap[epHash] = endpointSet{}
94-
}
89+
if !endpointutil.ShouldPodBeInEndpoints(pod, service.Spec.PublishNotReadyAddresses) {
90+
continue
91+
}
9592

96-
if _, ok := desiredMetaByPortMap[epHash]; !ok {
97-
desiredMetaByPortMap[epHash] = &endpointMeta{
98-
AddressType: addressType,
99-
Ports: endpointPorts,
100-
}
101-
}
93+
endpointPorts := getEndpointPorts(service, pod)
94+
epHash := endpointutil.NewPortMapKey(endpointPorts)
95+
if _, ok := desiredEndpointsByPortMap[epHash]; !ok {
96+
desiredEndpointsByPortMap[epHash] = endpointSet{}
97+
}
10298

103-
node, err := r.nodeLister.Get(pod.Spec.NodeName)
104-
if err != nil {
105-
return err
106-
}
107-
endpoint := podToEndpoint(pod, node, service)
108-
if len(endpoint.Addresses) > 0 {
109-
desiredEndpointsByPortMap[epHash].Insert(&endpoint)
110-
numDesiredEndpoints++
99+
if _, ok := desiredMetaByPortMap[epHash]; !ok {
100+
desiredMetaByPortMap[epHash] = &endpointMeta{
101+
AddressType: addressType,
102+
Ports: endpointPorts,
111103
}
112104
}
105+
106+
node, err := r.nodeLister.Get(pod.Spec.NodeName)
107+
if err != nil {
108+
return err
109+
}
110+
endpoint := podToEndpoint(pod, node, service)
111+
if len(endpoint.Addresses) > 0 {
112+
desiredEndpointsByPortMap[epHash].Insert(&endpoint)
113+
numDesiredEndpoints++
114+
}
113115
}
114116

115117
spMetrics := metrics.NewServicePortCache()

pkg/controller/util/endpoint/controller_utils.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,16 @@ func DeepHashObjectToString(objectToWrite interface{}) string {
126126
}
127127

128128
// ShouldPodBeInEndpoints returns true if a specified pod should be in an
129-
// endpoints object.
130-
func ShouldPodBeInEndpoints(pod *v1.Pod) bool {
129+
// endpoints object. Terminating pods are only included if publishNotReady is true.
130+
func ShouldPodBeInEndpoints(pod *v1.Pod, publishNotReady bool) bool {
131131
if len(pod.Status.PodIP) == 0 && len(pod.Status.PodIPs) == 0 {
132132
return false
133133
}
134134

135+
if !publishNotReady && pod.DeletionTimestamp != nil {
136+
return false
137+
}
138+
135139
if pod.Spec.RestartPolicy == v1.RestartPolicyNever {
136140
return pod.Status.Phase != v1.PodFailed && pod.Status.Phase != v1.PodSucceeded
137141
}

pkg/controller/util/endpoint/controller_utils_test.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,10 @@ func TestDetermineNeededServiceUpdates(t *testing.T) {
102102
// 12 true cases.
103103
func TestShouldPodBeInEndpoints(t *testing.T) {
104104
testCases := []struct {
105-
name string
106-
pod *v1.Pod
107-
expected bool
105+
name string
106+
pod *v1.Pod
107+
publishNotReady bool
108+
expected bool
108109
}{
109110
// Pod should not be in endpoints:
110111
{
@@ -160,6 +161,23 @@ func TestShouldPodBeInEndpoints(t *testing.T) {
160161
},
161162
expected: false,
162163
},
164+
{
165+
name: "Terminating Pod",
166+
pod: &v1.Pod{
167+
ObjectMeta: metav1.ObjectMeta{
168+
DeletionTimestamp: &metav1.Time{
169+
Time: time.Now(),
170+
},
171+
},
172+
Spec: v1.PodSpec{},
173+
Status: v1.PodStatus{
174+
Phase: v1.PodRunning,
175+
PodIP: "1.2.3.4",
176+
},
177+
},
178+
publishNotReady: false,
179+
expected: false,
180+
},
163181
// Pod should be in endpoints:
164182
{
165183
name: "Failed pod with Always RestartPolicy",
@@ -226,11 +244,28 @@ func TestShouldPodBeInEndpoints(t *testing.T) {
226244
},
227245
expected: true,
228246
},
247+
{
248+
name: "Terminating Pod with publish not ready",
249+
pod: &v1.Pod{
250+
ObjectMeta: metav1.ObjectMeta{
251+
DeletionTimestamp: &metav1.Time{
252+
Time: time.Now(),
253+
},
254+
},
255+
Spec: v1.PodSpec{},
256+
Status: v1.PodStatus{
257+
Phase: v1.PodRunning,
258+
PodIP: "1.2.3.4",
259+
},
260+
},
261+
publishNotReady: true,
262+
expected: true,
263+
},
229264
}
230265

231266
for _, test := range testCases {
232267
t.Run(test.name, func(t *testing.T) {
233-
result := ShouldPodBeInEndpoints(test.pod)
268+
result := ShouldPodBeInEndpoints(test.pod, test.publishNotReady)
234269
if result != test.expected {
235270
t.Errorf("expected: %t, got: %t", test.expected, result)
236271
}

0 commit comments

Comments
 (0)