Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: continue using and drain serving endpointslices during termination #4946

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/gatewayapi/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ func destinationSettingFromHostAndPort(host string, port uint32) []*ir.Destinati
{
Weight: ptr.To[uint32](1),
Protocol: ir.GRPC,
Endpoints: []*ir.DestinationEndpoint{ir.NewDestEndpoint(host, port)},
Endpoints: []*ir.DestinationEndpoint{ir.NewDestEndpoint(host, port, false)},
},
}
}
Expand Down
34 changes: 20 additions & 14 deletions internal/gatewayapi/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -1320,9 +1320,7 @@
} else {
backendIps := resources.GetServiceImport(backendNamespace, string(backendRef.Name)).Spec.IPs
for _, ip := range backendIps {
ep := ir.NewDestEndpoint(
ip,
uint32(*backendRef.Port))
ep := ir.NewDestEndpoint(ip, uint32(*backendRef.Port), false)

Check warning on line 1323 in internal/gatewayapi/route.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/route.go#L1323

Added line #L1323 was not covered by tests
endpoints = append(endpoints, ep)
}
}
Expand Down Expand Up @@ -1452,9 +1450,7 @@
endpoints, addrType = getIREndpointsFromEndpointSlices(endpointSlices, servicePort.Name, servicePort.Protocol)
} else {
// Fall back to Service ClusterIP routing
ep := ir.NewDestEndpoint(
service.Spec.ClusterIP,
uint32(*backendRef.Port))
ep := ir.NewDestEndpoint(service.Spec.ClusterIP, uint32(*backendRef.Port), false)
endpoints = append(endpoints, ep)
}

Expand Down Expand Up @@ -1665,15 +1661,25 @@
for _, endpoint := range endpointSlice.Endpoints {
for _, endpointPort := range endpointSlice.Ports {
// Check if the endpoint port matches the service port
// and if endpoint is Ready
if *endpointPort.Name == portName &&
*endpointPort.Protocol == portProtocol &&
// Unknown state (nil) should be interpreted as Ready, see https://pkg.go.dev/k8s.io/api/discovery/v1#EndpointConditions
(endpoint.Conditions.Ready == nil || *endpoint.Conditions.Ready) {
if *endpointPort.Name != portName || *endpointPort.Protocol != portProtocol {
continue
}
conditions := endpoint.Conditions
// Unknown Serving/Terminating (nil) should fall-back to Ready, see https://pkg.go.dev/k8s.io/api/discovery/v1#EndpointConditions
if conditions.Serving != nil && conditions.Terminating != nil {
// Check if the endpoint is serving
if !*conditions.Serving {
continue
}
// Drain the endpoint if it is being terminated
draining := *conditions.Terminating
for _, address := range endpoint.Addresses {
ep := ir.NewDestEndpoint(address, uint32(*endpointPort.Port), draining)
endpoints = append(endpoints, ep)
}
} else if conditions.Ready == nil || *conditions.Ready {
for _, address := range endpoint.Addresses {
ep := ir.NewDestEndpoint(
address,
uint32(*endpointPort.Port))
ep := ir.NewDestEndpoint(address, uint32(*endpointPort.Port), false)
endpoints = append(endpoints, ep)
}
}
Expand Down
83 changes: 66 additions & 17 deletions internal/gatewayapi/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
discoveryv1 "k8s.io/api/discovery/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -23,7 +24,7 @@ func TestGetIREndpointsFromEndpointSlices(t *testing.T) {
endpointSlices []*discoveryv1.EndpointSlice
portName string
portProtocol corev1.Protocol
expectedEndpoints int
expectedEndpoints []*ir.DestinationEndpoint
expectedAddrType ir.DestinationAddressType
}{
{
Expand Down Expand Up @@ -51,10 +52,14 @@ func TestGetIREndpointsFromEndpointSlices(t *testing.T) {
},
},
},
portName: "http",
portProtocol: corev1.ProtocolTCP,
expectedEndpoints: 3,
expectedAddrType: ir.IP,
portName: "http",
portProtocol: corev1.ProtocolTCP,
expectedEndpoints: []*ir.DestinationEndpoint{
{Host: "192.0.2.1", Port: 80, Draining: false},
{Host: "192.0.2.2", Port: 80, Draining: false},
{Host: "2001:db8::1", Port: 80, Draining: false},
},
expectedAddrType: ir.IP,
},
{
name: "Mixed IP and FQDN endpoints",
Expand All @@ -80,10 +85,13 @@ func TestGetIREndpointsFromEndpointSlices(t *testing.T) {
},
},
},
portName: "http",
portProtocol: corev1.ProtocolTCP,
expectedEndpoints: 2,
expectedAddrType: ir.MIXED,
portName: "http",
portProtocol: corev1.ProtocolTCP,
expectedEndpoints: []*ir.DestinationEndpoint{
{Host: "192.0.2.1", Port: 80, Draining: false},
{Host: "example.com", Port: 80, Draining: false},
},
expectedAddrType: ir.MIXED,
},
{
name: "Dual-stack IP endpoints",
Expand Down Expand Up @@ -111,10 +119,15 @@ func TestGetIREndpointsFromEndpointSlices(t *testing.T) {
},
},
},
portName: "http",
portProtocol: corev1.ProtocolTCP,
expectedEndpoints: 4,
expectedAddrType: ir.IP,
portName: "http",
portProtocol: corev1.ProtocolTCP,
expectedEndpoints: []*ir.DestinationEndpoint{
{Host: "192.0.2.1", Port: 80, Draining: false},
{Host: "192.0.2.2", Port: 80, Draining: false},
{Host: "2001:db8::1", Port: 80, Draining: false},
{Host: "2001:db8::2", Port: 80, Draining: false},
},
expectedAddrType: ir.IP,
},
{
name: "Dual-stack with FQDN",
Expand Down Expand Up @@ -150,10 +163,43 @@ func TestGetIREndpointsFromEndpointSlices(t *testing.T) {
},
},
},
portName: "http",
portProtocol: corev1.ProtocolTCP,
expectedEndpoints: 3,
expectedAddrType: ir.MIXED,
portName: "http",
portProtocol: corev1.ProtocolTCP,
expectedEndpoints: []*ir.DestinationEndpoint{
{Host: "192.0.2.1", Port: 80, Draining: false},
{Host: "2001:db8::1", Port: 80, Draining: false},
{Host: "example.com", Port: 80, Draining: false},
},
expectedAddrType: ir.MIXED,
},
{
name: "Keep serving and terminating as draining",
endpointSlices: []*discoveryv1.EndpointSlice{
{
ObjectMeta: metav1.ObjectMeta{Name: "slice1"},
AddressType: discoveryv1.AddressTypeIPv4,
Endpoints: []discoveryv1.Endpoint{
{Addresses: []string{"192.0.2.1"}, Conditions: discoveryv1.EndpointConditions{
Ready: ptr.To(false), Serving: ptr.To(true), Terminating: ptr.To(true),
}},
{Addresses: []string{"192.0.2.2"}, Conditions: discoveryv1.EndpointConditions{
Ready: ptr.To(false), Serving: ptr.To(false), Terminating: ptr.To(true),
}},
{Addresses: []string{"192.0.2.3"}, Conditions: discoveryv1.EndpointConditions{
Ready: ptr.To(false),
}},
},
Ports: []discoveryv1.EndpointPort{
{Name: ptr.To("http"), Port: ptr.To(int32(80)), Protocol: ptr.To(corev1.ProtocolTCP)},
},
},
},
portName: "http",
portProtocol: corev1.ProtocolTCP,
expectedEndpoints: []*ir.DestinationEndpoint{
{Host: "192.0.2.1", Port: 80, Draining: true},
},
expectedAddrType: ir.IP,
},
}

Expand All @@ -170,10 +216,13 @@ func TestGetIREndpointsFromEndpointSlices(t *testing.T) {
fmt.Printf(" Endpoint %d:\n", i+1)
fmt.Printf(" Address: %s\n", endpoint.Host)
fmt.Printf(" Port: %d\n", endpoint.Port)
fmt.Printf(" Draining: %t\n", endpoint.Draining)

}

fmt.Println()
require.Equal(t, tt.expectedEndpoints, endpoints)
require.Equal(t, tt.expectedAddrType, *addrType)
})
}
}
9 changes: 6 additions & 3 deletions internal/ir/xds.go
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,8 @@
Port uint32 `json:"port" yaml:"port"`
// Path refers to the Unix Domain Socket
Path *string `json:"path,omitempty" yaml:"path,omitempty"`
// Draining is true if this endpoint should be drained
Draining bool `json:"draining,omitempty" yaml:"draining,omitempty"`
}

// Validate the fields within the DestinationEndpoint structure
Expand Down Expand Up @@ -1503,10 +1505,11 @@
}

// NewDestEndpoint creates a new DestinationEndpoint.
func NewDestEndpoint(host string, port uint32) *DestinationEndpoint {
func NewDestEndpoint(host string, port uint32, draining bool) *DestinationEndpoint {

Check warning on line 1508 in internal/ir/xds.go

View check run for this annotation

Codecov / codecov/patch

internal/ir/xds.go#L1508

Added line #L1508 was not covered by tests
return &DestinationEndpoint{
Host: host,
Port: port,
Host: host,
Port: port,
Draining: draining,

Check warning on line 1512 in internal/ir/xds.go

View check run for this annotation

Codecov / codecov/patch

internal/ir/xds.go#L1510-L1512

Added lines #L1510 - L1512 were not covered by tests
}
}

Expand Down
5 changes: 5 additions & 0 deletions internal/xds/translator/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,13 +435,18 @@ func buildXdsClusterLoadAssignment(clusterName string, destSettings []*ir.Destin
}

for _, irEp := range ds.Endpoints {
healthStatus := corev3.HealthStatus_UNKNOWN
if irEp.Draining {
healthStatus = corev3.HealthStatus_DRAINING
}
lbEndpoint := &endpointv3.LbEndpoint{
Metadata: metadata,
HostIdentifier: &endpointv3.LbEndpoint_Endpoint{
Endpoint: &endpointv3.Endpoint{
Address: buildAddress(irEp),
},
},
HealthStatus: healthStatus,
}
// Set default weight of 1 for all endpoints.
lbEndpoint.LoadBalancingWeight = &wrapperspb.UInt32Value{Value: 1}
Expand Down
4 changes: 1 addition & 3 deletions internal/xds/translator/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,7 @@ func createOAuth2TokenEndpointCluster(tCtx *types.ResourceVersionTable,
ds = &ir.DestinationSetting{
Weight: ptr.To[uint32](1),
Endpoints: []*ir.DestinationEndpoint{
ir.NewDestEndpoint(
cluster.hostname,
cluster.port),
ir.NewDestEndpoint(cluster.hostname, cluster.port, false),
},
}

Expand Down
2 changes: 1 addition & 1 deletion internal/xds/translator/ratelimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ func (t *Translator) createRateLimitServiceCluster(tCtx *types.ResourceVersionTa
ds := &ir.DestinationSetting{
Weight: ptr.To[uint32](1),
Protocol: ir.GRPC,
Endpoints: []*ir.DestinationEndpoint{ir.NewDestEndpoint(host, port)},
Endpoints: []*ir.DestinationEndpoint{ir.NewDestEndpoint(host, port, false)},
}

tSocket, err := buildRateLimitTLSocket()
Expand Down
3 changes: 3 additions & 0 deletions internal/xds/translator/testdata/in/xds-ir/http-route.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ http:
- endpoints:
- host: "1.2.3.4"
port: 50000
- host: "1.2.3.5"
port: 50000
draining: true
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
address: 1.2.3.4
portValue: 50000
loadBalancingWeight: 1
- endpoint:
address:
socketAddress:
address: 1.2.3.5
portValue: 50000
healthStatus: DRAINING
loadBalancingWeight: 1
loadBalancingWeight: 1
locality:
region: first-route-dest/backend/0
2 changes: 1 addition & 1 deletion internal/xds/translator/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func addClusterFromURL(url string, tCtx *types.ResourceVersionTable) error {

ds = &ir.DestinationSetting{
Weight: ptr.To[uint32](1),
Endpoints: []*ir.DestinationEndpoint{ir.NewDestEndpoint(uc.hostname, uc.port)},
Endpoints: []*ir.DestinationEndpoint{ir.NewDestEndpoint(uc.hostname, uc.port, false)},
}

clusterArgs := &xdsClusterArgs{
Expand Down
1 change: 1 addition & 0 deletions release-notes/current.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ new features: |
Added support for cost specifier in the rate limit API.
Added support for specifying dynamic metadata namespaces that External Processing services can access read from and write to in EnvoyExtensionPolicy API
Added support for API Key Authentication in the SecurityPolicy API
Continue using and drain endpoints during their graceful termination, as indicated by their respective EndpointConditions
Added support for GEP-1731 (HTTPRoute Retries)
Added support for routing to Backend resources in the GRPCRoute, TCPRoute and UDPRoute APIs

Expand Down
Loading