1
0
Fork 0

Fix condition used for serving and fenced endpoints

Co-authored-by: Kevin Pollet <pollet.kevin@gmail.com>
This commit is contained in:
LBF38 2026-01-13 11:38:05 +01:00 committed by GitHub
parent 5a9f3e6999
commit 2e6dfbae57
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 15 additions and 94 deletions

View file

@ -14,7 +14,6 @@ import (
"github.com/traefik/traefik/v3/pkg/observability/logs" "github.com/traefik/traefik/v3/pkg/observability/logs"
"github.com/traefik/traefik/v3/pkg/provider" "github.com/traefik/traefik/v3/pkg/provider"
traefikv1alpha1 "github.com/traefik/traefik/v3/pkg/provider/kubernetes/crd/traefikio/v1alpha1" traefikv1alpha1 "github.com/traefik/traefik/v3/pkg/provider/kubernetes/crd/traefikio/v1alpha1"
"github.com/traefik/traefik/v3/pkg/provider/kubernetes/k8s"
"github.com/traefik/traefik/v3/pkg/tls" "github.com/traefik/traefik/v3/pkg/tls"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/intstr"
@ -642,7 +641,10 @@ func (c configBuilder) loadServers(parentNamespace string, svc traefikv1alpha1.L
} }
for _, endpoint := range endpointSlice.Endpoints { for _, endpoint := range endpointSlice.Endpoints {
if !k8s.EndpointServing(endpoint) { // The Serving condition allows to track if the Pod can receive traffic.
// It is set to true when the Pod is Ready or Terminating.
// From the go documentation, a nil value should be interpreted as "true".
if !ptr.Deref(endpoint.Conditions.Serving, true) {
continue continue
} }
@ -654,7 +656,7 @@ func (c configBuilder) loadServers(parentNamespace string, svc traefikv1alpha1.L
addresses[address] = struct{}{} addresses[address] = struct{}{}
servers = append(servers, dynamic.Server{ servers = append(servers, dynamic.Server{
URL: fmt.Sprintf("%s://%s", protocol, net.JoinHostPort(address, strconv.Itoa(int(port)))), URL: fmt.Sprintf("%s://%s", protocol, net.JoinHostPort(address, strconv.Itoa(int(port)))),
Fenced: ptr.Deref(endpoint.Conditions.Terminating, false) && ptr.Deref(endpoint.Conditions.Serving, false), Fenced: ptr.Deref(endpoint.Conditions.Terminating, false),
}) })
} }
} }

View file

@ -22,7 +22,6 @@ import (
"github.com/traefik/traefik/v3/pkg/job" "github.com/traefik/traefik/v3/pkg/job"
"github.com/traefik/traefik/v3/pkg/observability/logs" "github.com/traefik/traefik/v3/pkg/observability/logs"
"github.com/traefik/traefik/v3/pkg/provider" "github.com/traefik/traefik/v3/pkg/provider"
"github.com/traefik/traefik/v3/pkg/provider/kubernetes/k8s"
"github.com/traefik/traefik/v3/pkg/safe" "github.com/traefik/traefik/v3/pkg/safe"
"github.com/traefik/traefik/v3/pkg/tls" "github.com/traefik/traefik/v3/pkg/tls"
"github.com/traefik/traefik/v3/pkg/types" "github.com/traefik/traefik/v3/pkg/types"
@ -646,7 +645,10 @@ func (p *Provider) getBackendAddresses(namespace string, backend netv1.IngressBa
} }
for _, endpoint := range endpointSlice.Endpoints { for _, endpoint := range endpointSlice.Endpoints {
if !k8s.EndpointServing(endpoint) { // The Serving condition allows to track if the Pod can receive traffic.
// It is set to true when the Pod is Ready or Terminating.
// From the go documentation, a nil value should be interpreted as "true".
if !ptr.Deref(endpoint.Conditions.Serving, true) {
continue continue
} }
@ -658,7 +660,7 @@ func (p *Provider) getBackendAddresses(namespace string, backend netv1.IngressBa
uniqAddresses[address] = struct{}{} uniqAddresses[address] = struct{}{}
addresses = append(addresses, backendAddress{ addresses = append(addresses, backendAddress{
Address: net.JoinHostPort(address, strconv.Itoa(int(port))), Address: net.JoinHostPort(address, strconv.Itoa(int(port))),
Fenced: ptr.Deref(endpoint.Conditions.Terminating, false) && ptr.Deref(endpoint.Conditions.Serving, false), Fenced: ptr.Deref(endpoint.Conditions.Terminating, false),
}) })
} }
} }

View file

@ -662,7 +662,10 @@ func (p *Provider) loadService(client Client, namespace string, backend netv1.In
protocol := getProtocol(portSpec, portName, svcConfig) protocol := getProtocol(portSpec, portName, svcConfig)
for _, endpoint := range endpointSlice.Endpoints { for _, endpoint := range endpointSlice.Endpoints {
if !k8s.EndpointServing(endpoint) { // The Serving condition allows to track if the Pod can receive traffic.
// It is set to true when the Pod is Ready or Terminating.
// From the go documentation, a nil value should be interpreted as "true".
if !ptr.Deref(endpoint.Conditions.Serving, true) {
continue continue
} }
@ -674,7 +677,7 @@ func (p *Provider) loadService(client Client, namespace string, backend netv1.In
addresses[address] = struct{}{} addresses[address] = struct{}{}
svc.LoadBalancer.Servers = append(svc.LoadBalancer.Servers, dynamic.Server{ svc.LoadBalancer.Servers = append(svc.LoadBalancer.Servers, dynamic.Server{
URL: fmt.Sprintf("%s://%s", protocol, net.JoinHostPort(address, strconv.Itoa(int(port)))), URL: fmt.Sprintf("%s://%s", protocol, net.JoinHostPort(address, strconv.Itoa(int(port)))),
Fenced: ptr.Deref(endpoint.Conditions.Terminating, false) && ptr.Deref(endpoint.Conditions.Serving, false), Fenced: ptr.Deref(endpoint.Conditions.Terminating, false),
}) })
} }
} }

View file

@ -1,11 +0,0 @@
package k8s
import (
v1 "k8s.io/api/discovery/v1"
"k8s.io/utils/ptr"
)
// EndpointServing returns true if the endpoint is still serving the service.
func EndpointServing(endpoint v1.Endpoint) bool {
return ptr.Deref(endpoint.Conditions.Ready, false) || ptr.Deref(endpoint.Conditions.Serving, false)
}

View file

@ -1,75 +0,0 @@
package k8s
import (
"testing"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/discovery/v1"
)
func TestEndpointServing(t *testing.T) {
tests := []struct {
name string
endpoint v1.Endpoint
want bool
}{
{
name: "no status",
endpoint: v1.Endpoint{
Conditions: v1.EndpointConditions{
Ready: nil,
Serving: nil,
},
},
want: false,
},
{
name: "ready",
endpoint: v1.Endpoint{
Conditions: v1.EndpointConditions{
Ready: pointer(true),
Serving: nil,
},
},
want: true,
},
{
name: "not ready",
endpoint: v1.Endpoint{
Conditions: v1.EndpointConditions{
Ready: pointer(false),
Serving: nil,
},
},
want: false,
},
{
name: "not ready and serving",
endpoint: v1.Endpoint{
Conditions: v1.EndpointConditions{
Ready: pointer(false),
Serving: pointer(true),
},
},
want: true,
},
{
name: "not ready and not serving",
endpoint: v1.Endpoint{
Conditions: v1.EndpointConditions{
Ready: pointer(false),
Serving: pointer(false),
},
},
want: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := EndpointServing(test.endpoint)
assert.Equal(t, test.want, got)
})
}
}
func pointer[T any](v T) *T { return &v }