|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package test |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + |
| 23 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 24 | + |
| 25 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend" |
| 26 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/metadata" |
| 27 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins" |
| 28 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/requestcontrol" |
| 29 | + schedulingtypes "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types" |
| 30 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging" |
| 31 | +) |
| 32 | + |
| 33 | +const ( |
| 34 | + // ConformanceTestResultHeader is the header key for the conformance test result. |
| 35 | + ConformanceTestResultHeader = "x-conformance-test-served-endpoint" |
| 36 | + // ExpectedServedEndpointHeader is the header key for the expected served endpoint. |
| 37 | + ExpectedServedEndpointHeader = "x-test-expected-served-endpoint" |
| 38 | + // DestinationEndpointServedVerifierType is the ResponseReceived type that is used in plugins registry. |
| 39 | + DestinationEndpointServedVerifierType = "destination-endpoint-server-verifier" |
| 40 | +) |
| 41 | + |
| 42 | +var _ requestcontrol.ResponseReceived = &DestinationEndpointServedVerifier{} |
| 43 | + |
| 44 | +// DestinationEndpointServedVerifier is a test-only plugin for conformance tests. |
| 45 | +// It verifies that the request was served by the expected endpoint. |
| 46 | +// It works by reading Envoy's dynamic metadata, which is passed in the |
| 47 | +// Response.ReqMetadata field. This metadata should contain the |
| 48 | +// address of the backend endpoint that served the request. The verifier then |
| 49 | +// writes this address to the "x-conformance-test-served-endpoint" response header. |
| 50 | +// The conformance test client can then validate this header to ensure the request |
| 51 | +// was routed correctly. |
| 52 | +type DestinationEndpointServedVerifier struct { |
| 53 | + typedName plugins.TypedName |
| 54 | +} |
| 55 | + |
| 56 | +// TypedName returns the type and name tuple of this plugin instance. |
| 57 | +func (f *DestinationEndpointServedVerifier) TypedName() plugins.TypedName { |
| 58 | + return f.typedName |
| 59 | +} |
| 60 | + |
| 61 | +// WithName sets the name of the filter. |
| 62 | +func (f *DestinationEndpointServedVerifier) WithName(name string) *DestinationEndpointServedVerifier { |
| 63 | + f.typedName.Name = name |
| 64 | + return f |
| 65 | +} |
| 66 | + |
| 67 | +// DestinationEndpointServedVerifierFactory defines the factory function for DestinationEndpointServedVerifier. |
| 68 | +func DestinationEndpointServedVerifierFactory(name string, _ json.RawMessage, _ plugins.Handle) (plugins.Plugin, error) { |
| 69 | + return NewDestinationEndpointServedVerifier().WithName(name), nil |
| 70 | +} |
| 71 | + |
| 72 | +func NewDestinationEndpointServedVerifier() *DestinationEndpointServedVerifier { |
| 73 | + return &DestinationEndpointServedVerifier{} |
| 74 | +} |
| 75 | + |
| 76 | +// ResponseReceived is the handler for the ResponseReceived extension point. |
| 77 | +func (p *DestinationEndpointServedVerifier) ResponseReceived(ctx context.Context, request *schedulingtypes.LLMRequest, response *requestcontrol.Response, _ *backend.Pod) { |
| 78 | + logger := log.FromContext(ctx).WithName(p.TypedName().String()) |
| 79 | + logger.V(logging.DEBUG).Info("Verifying destination endpoint served") |
| 80 | + |
| 81 | + reqMetadata := response.ReqMetadata |
| 82 | + lbMetadata, ok := reqMetadata[metadata.DestinationEndpointNamespace].(map[string]any) |
| 83 | + if !ok { |
| 84 | + logger.V(logging.DEBUG).Info("Response does not contain envoy lb metadata, skipping verification") |
| 85 | + response.Headers[ConformanceTestResultHeader] = "fail: missing envoy lb metadata" |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + actualEndpoint, ok := lbMetadata[metadata.DestinationEndpointServedKey].(string) |
| 90 | + if !ok { |
| 91 | + logger.V(logging.DEBUG).Info("Response does not contain destination endpoint served metadata, skipping verification") |
| 92 | + response.Headers[ConformanceTestResultHeader] = "fail: missing destination endpoint served metadata" |
| 93 | + return |
| 94 | + } |
| 95 | + response.Headers[ConformanceTestResultHeader] = actualEndpoint |
| 96 | +} |
0 commit comments