Skip to content

Commit

Permalink
api: adds cel validation on requiring at least one targetRefs (#114)
Browse files Browse the repository at this point in the history
Previously targetRefs can be empty. However, in reality,
such AIGatewayRoute is useless while making it necessary
for controller to handle the edge case. This sets the CEL
validation rule to require at least one TargeRefs to exist.

---------

Signed-off-by: Takeshi Yoneda <[email protected]>
  • Loading branch information
mathetake authored Jan 17, 2025
1 parent 58e1973 commit a1d118b
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions api/v1alpha1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ type AIGatewayRouteList struct {
type AIGatewayRouteSpec struct {
// TargetRefs are the names of the Gateway resources this AIGatewayRoute is being attached to.
//
// +optional
// +kubebuilder:validation:MinItems=1
// +kubebuilder:validation:MaxItems=128
TargetRefs []gwapiv1a2.LocalPolicyTargetReferenceWithSectionName `json:"targetRefs,omitempty"`
TargetRefs []gwapiv1a2.LocalPolicyTargetReferenceWithSectionName `json:"targetRefs"`
// APISchema specifies the API schema of the input that the target Gateway(s) will receive.
// Based on this schema, the ai-gateway will perform the necessary transformation to the
// output schema specified in the selected AIServiceBackend during the routing process.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,12 @@ spec:
- name
type: object
maxItems: 128
minItems: 1
type: array
required:
- inputSchema
- rules
- targetRefs
type: object
type: object
served: true
Expand Down
4 changes: 4 additions & 0 deletions tests/cel-validation/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func TestAIGatewayRoutes(t *testing.T) {
name: "unsupported_match.yaml",
expErr: "spec.rules[0].matches[0].headers: Invalid value: \"array\": currently only exact match is supported",
},
{
name: "no_target_refs.yaml",
expErr: `spec.targetRefs: Invalid value: 0: spec.targetRefs in body should have at least 1 items`,
},
} {
t.Run(tc.name, func(t *testing.T) {
data, err := testdata.ReadFile(path.Join("testdata/aigatewayroutes", tc.name))
Expand Down
4 changes: 4 additions & 0 deletions tests/cel-validation/testdata/aigatewayroutes/basic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ metadata:
name: apple
namespace: default
spec:
targetRefs:
- name: some-gateway
kind: Gateway
group: gateway.networking.k8s.io
inputSchema:
schema: OpenAI
rules:
Expand Down
20 changes: 20 additions & 0 deletions tests/cel-validation/testdata/aigatewayroutes/no_target_refs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: aigateway.envoyproxy.io/v1alpha1
kind: AIGatewayRoute
metadata:
name: apple
namespace: default
spec:
targetRefs: []
inputSchema:
schema: OpenAI
rules:
- matches:
- headers:
- type: Exact
name: x-envoy-ai-gateway-model
value: llama3-70b
backendRefs:
- name: kserve
weight: 20
- name: aws-bedrock
weight: 80
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ metadata:
name: apple
namespace: default
spec:
targetRefs:
- name: some-gateway
kind: Gateway
group: gateway.networking.k8s.io
inputSchema:
# Input must be OpenAI schema at the moment, so this is invalid.
schema: AWSBedrock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ metadata:
name: apple
namespace: default
spec:
targetRefs:
- name: some-gateway
kind: Gateway
group: gateway.networking.k8s.io
inputSchema:
# Schema must be OpenAI schema at the moment, so this is invalid.
schema: SomeRandomVendor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ metadata:
name: apple
namespace: default
spec:
targetRefs:
- name: some-gateway
kind: Gateway
group: gateway.networking.k8s.io
inputSchema:
schema: OpenAI
rules:
Expand Down

0 comments on commit a1d118b

Please sign in to comment.