Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Ignasi Barrera <ignasi@tetrate.io>
  • Loading branch information
nacx committed Feb 19, 2025
1 parent 3d0b367 commit 30f086f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
10 changes: 10 additions & 0 deletions filterapi/filterconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,13 @@ func UnmarshalConfigYaml(path string) (*Config, []byte, error) {
}
return &cfg, raw, nil
}

// MustLoadDefaultConfig loads the default configuration.
// This panics if the configuration fails to be loaded.
func MustLoadDefaultConfig() (*Config, []byte) {
var cfg Config
if err := yaml.Unmarshal([]byte(DefaultConfig), &cfg); err != nil {
panic(err)
}
return &cfg, []byte(DefaultConfig)
}
13 changes: 8 additions & 5 deletions filterapi/filterconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"testing"

"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/util/yaml"

"github.com/envoyproxy/ai-gateway/filterapi"
"github.com/envoyproxy/ai-gateway/internal/extproc"
Expand All @@ -23,11 +22,15 @@ func TestDefaultConfig(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, server)

var cfg filterapi.Config
err = yaml.Unmarshal([]byte(filterapi.DefaultConfig), &cfg)
require.NoError(t, err)
cfg, raw := filterapi.MustLoadDefaultConfig()
require.Equal(t, []byte(filterapi.DefaultConfig), raw)
require.Equal(t, &filterapi.Config{
Schema: filterapi.VersionedAPISchema{Name: filterapi.APISchemaOpenAI},
SelectedBackendHeaderKey: "x-ai-eg-selected-backend",
ModelNameHeaderKey: "x-ai-eg-model",
}, cfg)

err = server.LoadConfig(t.Context(), &cfg)
err = server.LoadConfig(t.Context(), cfg)
require.NoError(t, err)
}

Expand Down
13 changes: 1 addition & 12 deletions internal/extproc/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"strings"
"time"

"k8s.io/apimachinery/pkg/util/yaml"

"github.com/envoyproxy/ai-gateway/filterapi"
)

Expand Down Expand Up @@ -76,7 +74,7 @@ func (cw *configWatcher) loadConfig(ctx context.Context) error {
if err != nil && os.IsNotExist(err) {
// If the file does not exist, do not fail (which could lead to the extproc process to terminate)
// Instead, load the default configuration and keep running unconfigured
cfg, raw, err = loadDefaultConfig()
cfg, raw = filterapi.MustLoadDefaultConfig()
}
// If reading the file fails, or loading the default config fails, abort
if err != nil {
Expand Down Expand Up @@ -131,12 +129,3 @@ func (cw *configWatcher) diff(oldConfig, newConfig string) {
}
}
}

// loadDefaultConfig loads the default configuration when the given config file path is not found.
func loadDefaultConfig() (*filterapi.Config, []byte, error) {
var cfg filterapi.Config
if err := yaml.Unmarshal([]byte(filterapi.DefaultConfig), &cfg); err != nil {
return nil, nil, err
}
return &cfg, []byte(filterapi.DefaultConfig), nil
}
6 changes: 2 additions & 4 deletions internal/extproc/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,11 @@ func TestStartConfigWatcher(t *testing.T) {
path := tmpdir + "/config.yaml"
rcv := &mockReceiver{}

ctx, cancel := context.WithCancel(t.Context())
defer cancel()
logger, buf := newTestLoggerWithBuffer()
err := StartConfigWatcher(ctx, path, rcv, logger, time.Millisecond*100)
err := StartConfigWatcher(t.Context(), path, rcv, logger, time.Millisecond*100)
require.NoError(t, err)

defaultCfg, _, err := loadDefaultConfig()
defaultCfg, _ := filterapi.MustLoadDefaultConfig()
require.NoError(t, err)

// Verify the default config has been loaded.
Expand Down

0 comments on commit 30f086f

Please sign in to comment.