Skip to content

Commit

Permalink
change nested schema to name
Browse files Browse the repository at this point in the history
Signed-off-by: Alexa Griffith <[email protected]>

change nested schema to name in structs

Signed-off-by: Alexa Griffith <[email protected]>

fix factory logic

Signed-off-by: Alexa Griffith <[email protected]>

rebase# This is a combination of 20 commits.

fix some other schema names to be consistent

Signed-off-by: Alexa Griffith <[email protected]>

gitignore idea

Signed-off-by: Alexa Griffith <[email protected]>

fix missing name change

Signed-off-by: Alexa Griffith <[email protected]>

fix filterconfig

Signed-off-by: Alexa Griffith <[email protected]>

fix test

Signed-off-by: Alexa Griffith <[email protected]>

renaming

Signed-off-by: Alexa Griffith <[email protected]>

fix

Signed-off-by: Alexa Griffith <[email protected]>

fix sink test naming

Signed-off-by: Alexa Griffith <[email protected]>

fix controller test

Signed-off-by: Alexa Griffith <[email protected]>

add name rule

Signed-off-by: Alexa Griffith <[email protected]>

fix rule

Signed-off-by: Alexa Griffith <[email protected]>

rename klog pkg

Signed-off-by: Alexa Griffith <[email protected]>

fix cel tests

Signed-off-by: Alexa Griffith <[email protected]>

re-add openai pkg

Signed-off-by: Alexa Griffith <[email protected]>

fix llm backend yaml

Signed-off-by: Alexa Griffith <[email protected]>

fix backend

Signed-off-by: Alexa Griffith <[email protected]>

fix the code style

Signed-off-by: Alexa Griffith <[email protected]>

fix order

Signed-off-by: Alexa Griffith <[email protected]>

fix

Signed-off-by: Alexa Griffith <[email protected]>

lint

Signed-off-by: Alexa Griffith <[email protected]>
  • Loading branch information
alexagriffith committed Jan 15, 2025
1 parent 7d96b48 commit 2e997d4
Show file tree
Hide file tree
Showing 25 changed files with 91 additions and 88 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ out/
.DS_Store
.terraform
*for_tests.yaml
.idea/

# Files and directories to ignore in the site directory
# dependencies
Expand Down
14 changes: 7 additions & 7 deletions api/v1alpha1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type LLMRouteSpec struct {
// Currently, the only supported schema is OpenAI as the input schema.
//
// +kubebuilder:validation:Required
// +kubebuilder:validation:XValidation:rule="self.schema == 'OpenAI'"
// +kubebuilder:validation:XValidation:rule="self.name == 'OpenAI'"
APISchema LLMAPISchema `json:"schema"`
// Rules is the list of LLMRouteRule that this LLMRoute will match the traffic to.
// Each rule is a subset of the HTTPRoute in the Gateway API (https://gateway-api.sigs.k8s.io/api-types/httproute/).
Expand Down Expand Up @@ -183,27 +183,27 @@ type LLMBackendSpec struct {
// Note that this is vendor specific, and the stability of the API schema is not guaranteed by
// the ai-gateway, but by the vendor via proper versioning.
type LLMAPISchema struct {
// Schema is the API schema of the LLMRoute or LLMBackend.
// Name is the name of the API schema of the LLMRoute or LLMBackend.
//
// +kubebuilder:validation:Enum=OpenAI;AWSBedrock
Schema APISchema `json:"schema"`
Name APISchemaName `json:"name"`

// Version is the version of the API schema.
Version string `json:"version,omitempty"`
}

// APISchema defines the API schema.
type APISchema string
// APISchemaName defines the name of the API schema.
type APISchemaName string

const (
// APISchemaOpenAI is the OpenAI schema.
//
// https://github.com/openai/openai-openapi
APISchemaOpenAI APISchema = "OpenAI"
APISchemaOpenAI APISchemaName = "OpenAI"
// APISchemaAWSBedrock is the AWS Bedrock schema.
//
// https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Amazon_Bedrock_Runtime.html
APISchemaAWSBedrock APISchema = "AWSBedrock"
APISchemaAWSBedrock APISchemaName = "AWSBedrock"
)

const (
Expand Down
22 changes: 11 additions & 11 deletions filterconfig/filterconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// fallback when the configuration is not explicitly provided.
const DefaultConfig = `
schema:
schema: OpenAI
name: OpenAI
selectedBackendHeaderKey: x-envoy-ai-gateway-selected-backend
modelNameHeaderKey: x-envoy-ai-gateway-model
`
Expand All @@ -30,7 +30,7 @@ modelNameHeaderKey: x-envoy-ai-gateway-model
// # Example configuration:
//
// schema:
// schema: OpenAI
// name: OpenAI
// selectedBackendHeaderKey: x-envoy-ai-gateway-selected-backend
// modelNameHeaderKey: x-envoy-ai-gateway-model
// tokenUsageMetadata:
Expand All @@ -41,18 +41,18 @@ modelNameHeaderKey: x-envoy-ai-gateway-model
// - name: kserve
// weight: 1
// schema:
// schema: OpenAI
// name: OpenAI
// - name: awsbedrock
// weight: 10
// schema:
// schema: AWSBedrock
// name: AWSBedrock
// headers:
// - name: x-envoy-ai-gateway-model
// value: llama3.3333
// - backends:
// - name: openai
// schema:
// schema: OpenAI
// name: OpenAI
// headers:
// - name: x-envoy-ai-gateway-model
// value: gpt4.4444
Expand Down Expand Up @@ -96,18 +96,18 @@ type TokenUsageMetadata struct {

// VersionedAPISchema corresponds to LLMAPISchema in api/v1alpha1/api.go.
type VersionedAPISchema struct {
// Schema is the API schema.
Schema APISchema `yaml:"schema"`
// Name is the name of the API schema.
Name APISchemaName `yaml:"name"`
// Version is the version of the API schema. Optional.
Version string `yaml:"version,omitempty"`
}

// APISchema corresponds to APISchema in api/v1alpha1/api.go.
type APISchema string
// APISchemaName corresponds to APISchemaName in api/v1alpha1/api.go.
type APISchemaName string

const (
APISchemaOpenAI APISchema = "OpenAI"
APISchemaAWSBedrock APISchema = "AWSBedrock"
APISchemaOpenAI APISchemaName = "OpenAI"
APISchemaAWSBedrock APISchemaName = "AWSBedrock"
)

// HeaderMatch is an alias for HTTPHeaderMatch of the Gateway API.
Expand Down
14 changes: 7 additions & 7 deletions filterconfig/filterconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestUnmarshalConfigYaml(t *testing.T) {
configPath := path.Join(t.TempDir(), "config.yaml")
const config = `
schema:
schema: OpenAI
name: OpenAI
selectedBackendHeaderKey: x-envoy-ai-gateway-selected-backend
modelNameHeaderKey: x-envoy-ai-gateway-model
tokenUsageMetadata:
Expand All @@ -41,18 +41,18 @@ rules:
- name: kserve
weight: 1
schema:
schema: OpenAI
name: OpenAI
- name: awsbedrock
weight: 10
schema:
schema: AWSBedrock
name: AWSBedrock
headers:
- name: x-envoy-ai-gateway-model
value: llama3.3333
- backends:
- name: openai
schema:
schema: OpenAI
name: OpenAI
headers:
- name: x-envoy-ai-gateway-model
value: gpt4.4444
Expand All @@ -62,15 +62,15 @@ rules:
require.NoError(t, err)
require.Equal(t, "ai_gateway_llm_ns", cfg.TokenUsageMetadata.Namespace)
require.Equal(t, "token_usage_key", cfg.TokenUsageMetadata.Key)
require.Equal(t, "OpenAI", string(cfg.Schema.Schema))
require.Equal(t, "OpenAI", string(cfg.Schema.Name))
require.Equal(t, "x-envoy-ai-gateway-selected-backend", cfg.SelectedBackendHeaderKey)
require.Equal(t, "x-envoy-ai-gateway-model", cfg.ModelNameHeaderKey)
require.Len(t, cfg.Rules, 2)
require.Equal(t, "llama3.3333", cfg.Rules[0].Headers[0].Value)
require.Equal(t, "gpt4.4444", cfg.Rules[1].Headers[0].Value)
require.Equal(t, "kserve", cfg.Rules[0].Backends[0].Name)
require.Equal(t, 10, cfg.Rules[0].Backends[1].Weight)
require.Equal(t, "AWSBedrock", string(cfg.Rules[0].Backends[1].Schema.Schema))
require.Equal(t, "AWSBedrock", string(cfg.Rules[0].Backends[1].Schema.Name))
require.Equal(t, "openai", cfg.Rules[1].Backends[0].Name)
require.Equal(t, "OpenAI", string(cfg.Rules[1].Backends[0].Schema.Schema))
require.Equal(t, "OpenAI", string(cfg.Rules[1].Backends[0].Schema.Name))
}
4 changes: 2 additions & 2 deletions internal/controller/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (c *configSink) updateExtProcConfigMap(llmRoute *aigv1a1.LLMRoute) error {
ec := &filterconfig.Config{}
spec := &llmRoute.Spec

ec.Schema.Schema = filterconfig.APISchema(spec.APISchema.Schema)
ec.Schema.Name = filterconfig.APISchemaName(spec.APISchema.Name)
ec.Schema.Version = spec.APISchema.Version
ec.ModelNameHeaderKey = aigv1a1.LLMModelHeaderKey
ec.SelectedBackendHeaderKey = selectedBackendHeaderKey
Expand All @@ -242,7 +242,7 @@ func (c *configSink) updateExtProcConfigMap(llmRoute *aigv1a1.LLMRoute) error {
err = fmt.Errorf("backend %s not found", key)
return err
} else {
ec.Rules[i].Backends[j].Schema.Schema = filterconfig.APISchema(backendObj.Spec.APISchema.Schema)
ec.Rules[i].Backends[j].Schema.Name = filterconfig.APISchemaName(backendObj.Spec.APISchema.Name)
ec.Rules[i].Backends[j].Schema.Version = backendObj.Spec.APISchema.Version
}
}
Expand Down
18 changes: 9 additions & 9 deletions internal/controller/sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestConfigSink_init(t *testing.T) {
BackendRef: egv1a1.BackendRef{
BackendObjectReference: gwapiv1.BackendObjectReference{Name: "some-backend1", Namespace: ptr.To[gwapiv1.Namespace]("ns1")},
},
APISchema: aigv1a1.LLMAPISchema{Schema: aigv1a1.APISchemaOpenAI},
APISchema: aigv1a1.LLMAPISchema{Name: aigv1a1.APISchemaOpenAI},
},
},
{
Expand All @@ -87,7 +87,7 @@ func TestConfigSink_init(t *testing.T) {
BackendRef: egv1a1.BackendRef{
BackendObjectReference: gwapiv1.BackendObjectReference{Name: "some-backend2", Namespace: ptr.To[gwapiv1.Namespace]("ns1")},
},
APISchema: aigv1a1.LLMAPISchema{Schema: aigv1a1.APISchemaAWSBedrock},
APISchema: aigv1a1.LLMAPISchema{Name: aigv1a1.APISchemaAWSBedrock},
},
},
{
Expand All @@ -96,7 +96,7 @@ func TestConfigSink_init(t *testing.T) {
BackendRef: egv1a1.BackendRef{
BackendObjectReference: gwapiv1.BackendObjectReference{Name: "some-backend3", Namespace: ptr.To[gwapiv1.Namespace]("ns1")},
},
APISchema: aigv1a1.LLMAPISchema{Schema: aigv1a1.APISchemaOpenAI},
APISchema: aigv1a1.LLMAPISchema{Name: aigv1a1.APISchemaOpenAI},
},
},
{
Expand All @@ -105,7 +105,7 @@ func TestConfigSink_init(t *testing.T) {
BackendRef: egv1a1.BackendRef{
BackendObjectReference: gwapiv1.BackendObjectReference{Name: "some-backend4", Namespace: ptr.To[gwapiv1.Namespace]("ns1")},
},
APISchema: aigv1a1.LLMAPISchema{Schema: aigv1a1.APISchemaOpenAI},
APISchema: aigv1a1.LLMAPISchema{Name: aigv1a1.APISchemaOpenAI},
},
},
} {
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestConfigSink_syncLLMRoute(t *testing.T) {
BackendRefs: []aigv1a1.LLMRouteRuleBackendRef{{Name: "apple", Weight: 1}, {Name: "orange", Weight: 1}},
},
},
APISchema: aigv1a1.LLMAPISchema{Schema: aigv1a1.APISchemaOpenAI, Version: "v123"},
APISchema: aigv1a1.LLMAPISchema{Name: aigv1a1.APISchemaOpenAI, Version: "v123"},
},
}
err := fakeClient.Create(context.Background(), route, &client.CreateOptions{})
Expand Down Expand Up @@ -389,7 +389,7 @@ func Test_updateExtProcConfigMap(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{Name: "apple", Namespace: "ns"},
Spec: aigv1a1.LLMBackendSpec{
APISchema: aigv1a1.LLMAPISchema{
Schema: aigv1a1.APISchemaAWSBedrock,
Name: aigv1a1.APISchemaAWSBedrock,
},
BackendRef: egv1a1.BackendRef{
BackendObjectReference: gwapiv1.BackendObjectReference{Name: "some-backend1", Namespace: ptr.To[gwapiv1.Namespace]("ns")},
Expand Down Expand Up @@ -425,7 +425,7 @@ func Test_updateExtProcConfigMap(t *testing.T) {
route: &aigv1a1.LLMRoute{
ObjectMeta: metav1.ObjectMeta{Name: "myroute", Namespace: "ns"},
Spec: aigv1a1.LLMRouteSpec{
APISchema: aigv1a1.LLMAPISchema{Schema: aigv1a1.APISchemaOpenAI, Version: "v123"},
APISchema: aigv1a1.LLMAPISchema{Name: aigv1a1.APISchemaOpenAI, Version: "v123"},
Rules: []aigv1a1.LLMRouteRule{
{
BackendRefs: []aigv1a1.LLMRouteRuleBackendRef{
Expand All @@ -446,13 +446,13 @@ func Test_updateExtProcConfigMap(t *testing.T) {
},
},
exp: &filterconfig.Config{
Schema: filterconfig.VersionedAPISchema{Schema: filterconfig.APISchemaOpenAI, Version: "v123"},
Schema: filterconfig.VersionedAPISchema{Name: filterconfig.APISchemaOpenAI, Version: "v123"},
ModelNameHeaderKey: aigv1a1.LLMModelHeaderKey,
SelectedBackendHeaderKey: selectedBackendHeaderKey,
Rules: []filterconfig.RouteRule{
{
Backends: []filterconfig.Backend{
{Name: "apple.ns", Weight: 1, Schema: filterconfig.VersionedAPISchema{Schema: filterconfig.APISchemaAWSBedrock}}, {Name: "pineapple.ns", Weight: 2},
{Name: "apple.ns", Weight: 1, Schema: filterconfig.VersionedAPISchema{Name: filterconfig.APISchemaAWSBedrock}}, {Name: "pineapple.ns", Weight: 2},
},
Headers: []filterconfig.HeaderMatch{{Name: aigv1a1.LLMModelHeaderKey, Value: "some-ai"}},
},
Expand Down
14 changes: 7 additions & 7 deletions internal/extproc/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestProcessor_ProcessRequestBody(t *testing.T) {
rbp := mockRequestBodyParser{t: t, retModelName: "some-model", expPath: "/foo"}
rt := mockRouter{
t: t, expHeaders: headers, retBackendName: "some-backend",
retVersionedAPISchema: filterconfig.VersionedAPISchema{Schema: "some-schema", Version: "v10.0"},
retVersionedAPISchema: filterconfig.VersionedAPISchema{Name: "some-schema", Version: "v10.0"},
}
p := &Processor{config: &processorConfig{
bodyParser: rbp.impl, router: rt,
Expand All @@ -109,13 +109,13 @@ func TestProcessor_ProcessRequestBody(t *testing.T) {
rbp := mockRequestBodyParser{t: t, retModelName: "some-model", expPath: "/foo"}
rt := mockRouter{
t: t, expHeaders: headers, retBackendName: "some-backend",
retVersionedAPISchema: filterconfig.VersionedAPISchema{Schema: "some-schema", Version: "v10.0"},
retVersionedAPISchema: filterconfig.VersionedAPISchema{Name: "some-schema", Version: "v10.0"},
}
factory := mockTranslatorFactory{t: t, retErr: errors.New("test error"), expPath: "/foo"}
p := &Processor{config: &processorConfig{
bodyParser: rbp.impl, router: rt,
factories: map[filterconfig.VersionedAPISchema]translator.Factory{
{Schema: "some-schema", Version: "v10.0"}: factory.impl,
{Name: "some-schema", Version: "v10.0"}: factory.impl,
},
}, requestHeaders: headers}
_, err := p.ProcessRequestBody(context.Background(), &extprocv3.HttpBody{})
Expand All @@ -126,13 +126,13 @@ func TestProcessor_ProcessRequestBody(t *testing.T) {
rbp := mockRequestBodyParser{t: t, retModelName: "some-model", expPath: "/foo"}
rt := mockRouter{
t: t, expHeaders: headers, retBackendName: "some-backend",
retVersionedAPISchema: filterconfig.VersionedAPISchema{Schema: "some-schema", Version: "v10.0"},
retVersionedAPISchema: filterconfig.VersionedAPISchema{Name: "some-schema", Version: "v10.0"},
}
factory := mockTranslatorFactory{t: t, retTranslator: mockTranslator{t: t, retErr: errors.New("test error")}, expPath: "/foo"}
p := &Processor{config: &processorConfig{
bodyParser: rbp.impl, router: rt,
factories: map[filterconfig.VersionedAPISchema]translator.Factory{
{Schema: "some-schema", Version: "v10.0"}: factory.impl,
{Name: "some-schema", Version: "v10.0"}: factory.impl,
},
}, requestHeaders: headers}
_, err := p.ProcessRequestBody(context.Background(), &extprocv3.HttpBody{})
Expand All @@ -144,7 +144,7 @@ func TestProcessor_ProcessRequestBody(t *testing.T) {
rbp := mockRequestBodyParser{t: t, retModelName: "some-model", expPath: "/foo", retRb: someBody}
rt := mockRouter{
t: t, expHeaders: headers, retBackendName: "some-backend",
retVersionedAPISchema: filterconfig.VersionedAPISchema{Schema: "some-schema", Version: "v10.0"},
retVersionedAPISchema: filterconfig.VersionedAPISchema{Name: "some-schema", Version: "v10.0"},
}
headerMut := &extprocv3.HeaderMutation{}
bodyMut := &extprocv3.BodyMutation{}
Expand All @@ -153,7 +153,7 @@ func TestProcessor_ProcessRequestBody(t *testing.T) {
p := &Processor{config: &processorConfig{
bodyParser: rbp.impl, router: rt,
factories: map[filterconfig.VersionedAPISchema]translator.Factory{
{Schema: "some-schema", Version: "v10.0"}: factory.impl,
{Name: "some-schema", Version: "v10.0"}: factory.impl,
},
selectedBackendHeaderKey: "x-ai-gateway-backend-key",
ModelNameHeaderKey: "x-ai-gateway-model-key",
Expand Down
2 changes: 1 addition & 1 deletion internal/extproc/router/request_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type RequestBodyParser func(path string, body *extprocv3.HttpBody) (modelName st

// NewRequestBodyParser creates a new RequestBodyParser based on the schema.
func NewRequestBodyParser(schema filterconfig.VersionedAPISchema) (RequestBodyParser, error) {
if schema.Schema == filterconfig.APISchemaOpenAI {
if schema.Name == filterconfig.APISchemaOpenAI {
return openAIParseBody, nil
}
return nil, fmt.Errorf("unsupported API schema: %s", schema)
Expand Down
4 changes: 2 additions & 2 deletions internal/extproc/router/request_body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (

func TestNewRequestBodyParser(t *testing.T) {
t.Run("ok", func(t *testing.T) {
res, err := NewRequestBodyParser(filterconfig.VersionedAPISchema{Schema: filterconfig.APISchemaOpenAI})
res, err := NewRequestBodyParser(filterconfig.VersionedAPISchema{Name: filterconfig.APISchemaOpenAI})
require.NotNil(t, res)
require.NoError(t, err)
})
t.Run("error", func(t *testing.T) {
res, err := NewRequestBodyParser(filterconfig.VersionedAPISchema{Schema: "foo"})
res, err := NewRequestBodyParser(filterconfig.VersionedAPISchema{Name: "foo"})
require.Nil(t, res)
require.Error(t, err)
})
Expand Down
4 changes: 2 additions & 2 deletions internal/extproc/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestRouter_Calculate(t *testing.T) {
outSchema := filterconfig.VersionedAPISchema{Schema: filterconfig.APISchemaOpenAI}
outSchema := filterconfig.VersionedAPISchema{Name: filterconfig.APISchemaOpenAI}
_r, err := NewRouter(&filterconfig.Config{
Rules: []filterconfig.RouteRule{
{
Expand Down Expand Up @@ -68,7 +68,7 @@ func TestRouter_selectBackendFromRule(t *testing.T) {
r, ok := _r.(*router)
require.True(t, ok)

outSchema := filterconfig.VersionedAPISchema{Schema: filterconfig.APISchemaOpenAI}
outSchema := filterconfig.VersionedAPISchema{Name: filterconfig.APISchemaOpenAI}

rule := &filterconfig.RouteRule{
Backends: []filterconfig.Backend{
Expand Down
Loading

0 comments on commit 2e997d4

Please sign in to comment.