mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 05:03:09 +00:00
EndpointSlice and Endpoints should treat terminating pods the same
Signed-off-by: Andrew Sy Kim <kim.andrewsy@gmail.com>
This commit is contained in:
parent
7989ca4324
commit
366dd4af44
@ -86,7 +86,10 @@ func (r *reconciler) reconcile(service *corev1.Service, pods []*corev1.Pod, exis
|
|||||||
numDesiredEndpoints := 0
|
numDesiredEndpoints := 0
|
||||||
|
|
||||||
for _, pod := range pods {
|
for _, pod := range pods {
|
||||||
if endpointutil.ShouldPodBeInEndpoints(pod) {
|
if !endpointutil.ShouldPodBeInEndpoints(pod, service.Spec.PublishNotReadyAddresses) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
endpointPorts := getEndpointPorts(service, pod)
|
endpointPorts := getEndpointPorts(service, pod)
|
||||||
epHash := endpointutil.NewPortMapKey(endpointPorts)
|
epHash := endpointutil.NewPortMapKey(endpointPorts)
|
||||||
if _, ok := desiredEndpointsByPortMap[epHash]; !ok {
|
if _, ok := desiredEndpointsByPortMap[epHash]; !ok {
|
||||||
@ -110,7 +113,6 @@ func (r *reconciler) reconcile(service *corev1.Service, pods []*corev1.Pod, exis
|
|||||||
numDesiredEndpoints++
|
numDesiredEndpoints++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
spMetrics := metrics.NewServicePortCache()
|
spMetrics := metrics.NewServicePortCache()
|
||||||
totalAdded := 0
|
totalAdded := 0
|
||||||
|
@ -126,12 +126,16 @@ func DeepHashObjectToString(objectToWrite interface{}) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ShouldPodBeInEndpoints returns true if a specified pod should be in an
|
// ShouldPodBeInEndpoints returns true if a specified pod should be in an
|
||||||
// endpoints object.
|
// endpoints object. Terminating pods are only included if publishNotReady is true.
|
||||||
func ShouldPodBeInEndpoints(pod *v1.Pod) bool {
|
func ShouldPodBeInEndpoints(pod *v1.Pod, publishNotReady bool) bool {
|
||||||
if len(pod.Status.PodIP) == 0 && len(pod.Status.PodIPs) == 0 {
|
if len(pod.Status.PodIP) == 0 && len(pod.Status.PodIPs) == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !publishNotReady && pod.DeletionTimestamp != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
if pod.Spec.RestartPolicy == v1.RestartPolicyNever {
|
if pod.Spec.RestartPolicy == v1.RestartPolicyNever {
|
||||||
return pod.Status.Phase != v1.PodFailed && pod.Status.Phase != v1.PodSucceeded
|
return pod.Status.Phase != v1.PodFailed && pod.Status.Phase != v1.PodSucceeded
|
||||||
}
|
}
|
||||||
|
@ -104,6 +104,7 @@ func TestShouldPodBeInEndpoints(t *testing.T) {
|
|||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
pod *v1.Pod
|
pod *v1.Pod
|
||||||
|
publishNotReady bool
|
||||||
expected bool
|
expected bool
|
||||||
}{
|
}{
|
||||||
// Pod should not be in endpoints:
|
// Pod should not be in endpoints:
|
||||||
@ -160,6 +161,23 @@ func TestShouldPodBeInEndpoints(t *testing.T) {
|
|||||||
},
|
},
|
||||||
expected: false,
|
expected: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Terminating Pod",
|
||||||
|
pod: &v1.Pod{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
DeletionTimestamp: &metav1.Time{
|
||||||
|
Time: time.Now(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Spec: v1.PodSpec{},
|
||||||
|
Status: v1.PodStatus{
|
||||||
|
Phase: v1.PodRunning,
|
||||||
|
PodIP: "1.2.3.4",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
publishNotReady: false,
|
||||||
|
expected: false,
|
||||||
|
},
|
||||||
// Pod should be in endpoints:
|
// Pod should be in endpoints:
|
||||||
{
|
{
|
||||||
name: "Failed pod with Always RestartPolicy",
|
name: "Failed pod with Always RestartPolicy",
|
||||||
@ -226,11 +244,28 @@ func TestShouldPodBeInEndpoints(t *testing.T) {
|
|||||||
},
|
},
|
||||||
expected: true,
|
expected: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Terminating Pod with publish not ready",
|
||||||
|
pod: &v1.Pod{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
DeletionTimestamp: &metav1.Time{
|
||||||
|
Time: time.Now(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Spec: v1.PodSpec{},
|
||||||
|
Status: v1.PodStatus{
|
||||||
|
Phase: v1.PodRunning,
|
||||||
|
PodIP: "1.2.3.4",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
publishNotReady: true,
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
result := ShouldPodBeInEndpoints(test.pod)
|
result := ShouldPodBeInEndpoints(test.pod, test.publishNotReady)
|
||||||
if result != test.expected {
|
if result != test.expected {
|
||||||
t.Errorf("expected: %t, got: %t", test.expected, result)
|
t.Errorf("expected: %t, got: %t", test.expected, result)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user