Skip to content

Commit cc9267b

Browse files
feat(aigw): point to example config instead of bundling one (envoyproxy#1235)
**Description** We can dramatically clarify `aigw run` docs now that you can auto-generate a configuration with ENV variables. This removes the bundled example config, and points to the examples directory instead, which allows people to discover more features and have a simpler doc experience. **Related Issues/PRs (if applicable)** envoyproxy#1233 (comment) --------- Signed-off-by: Adrian Cole <[email protected]> Co-authored-by: Takeshi Yoneda <[email protected]>
1 parent b674da7 commit cc9267b

File tree

9 files changed

+191
-756
lines changed

9 files changed

+191
-756
lines changed

cmd/aigw/ai-gateway-default-resources.yaml

Lines changed: 0 additions & 471 deletions
This file was deleted.

cmd/aigw/config_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,10 @@ func TestReadConfig(t *testing.T) {
7272
})
7373
}
7474

75-
t.Run("Default config uses 0.0.0.0 IP for Ollama when no env vars", func(t *testing.T) {
76-
t.Setenv("OPENAI_API_KEY", "")
77-
t.Setenv("OPENAI_BASE_URL", "")
78-
config, err := readConfig("")
79-
require.NoError(t, err)
80-
require.Contains(t, config, "address: 0.0.0.0")
81-
require.Contains(t, config, "port: 11434")
75+
t.Run("error when file and no OPENAI_API_KEY", func(t *testing.T) {
76+
_, err := readConfig("")
77+
require.Error(t, err)
78+
require.EqualError(t, err, "you must supply at least OPENAI_API_KEY or a config file path")
8279
})
8380

8481
t.Run("error when file does not exist", func(t *testing.T) {

cmd/aigw/main.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,19 @@ type (
3636
}
3737
// cmdRun corresponds to `aigw run` command.
3838
cmdRun struct {
39-
Debug bool `help:"Enable debug logging emitted to stderr."`
40-
Path string `arg:"" name:"path" optional:"" help:"Path to the AI Gateway configuration yaml file. Optional. When this is not given, aigw runs the default configuration. Use --show-default to check the default configuration's behavior" type:"path"`
41-
ShowDefault bool `help:"Show the default configuration, and exit."`
39+
Debug bool `help:"Enable debug logging emitted to stderr."`
40+
Path string `arg:"" name:"path" optional:"" help:"Path to the AI Gateway configuration yaml file. Optional when at least OPENAI_API_KEY is set." type:"path"`
4241
}
4342
)
4443

44+
// Validate is called by Kong after parsing to validate the cmdRun arguments.
45+
func (c *cmdRun) Validate() error {
46+
if c.Path == "" && os.Getenv("OPENAI_API_KEY") == "" {
47+
return fmt.Errorf("you must supply at least OPENAI_API_KEY or a config file path")
48+
}
49+
return nil
50+
}
51+
4552
type (
4653
subCmdFn[T any] func(context.Context, T, io.Writer, io.Writer) error
4754
translateFn subCmdFn[cmdTranslate]

cmd/aigw/main_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func Test_doMain(t *testing.T) {
2121
tests := []struct {
2222
name string
2323
args []string
24+
env map[string]string
2425
tf translateFn
2526
rf runFn
2627
expOut string
@@ -105,8 +106,15 @@ Flags:
105106
expPanicCode: ptr.To(0),
106107
},
107108
{
108-
name: "run no arg",
109+
name: "run no arg",
110+
args: []string{"run"},
111+
rf: func(context.Context, cmdRun, runOpts, io.Writer, io.Writer) error { return nil },
112+
expPanicCode: ptr.To(80),
113+
},
114+
{
115+
name: "run with OpenAI env",
109116
args: []string{"run"},
117+
env: map[string]string{"OPENAI_API_KEY": "dummy-key"},
110118
rf: func(context.Context, cmdRun, runOpts, io.Writer, io.Writer) error { return nil },
111119
},
112120
{
@@ -118,40 +126,32 @@ Flags:
118126
Run the AI Gateway locally for given configuration.
119127
120128
Arguments:
121-
[<path>] Path to the AI Gateway configuration yaml file. Optional.
122-
When this is not given, aigw runs the default configuration.
123-
Use --show-default to check the default configuration's behavior
129+
[<path>] Path to the AI Gateway configuration yaml file. Optional when at
130+
least OPENAI_API_KEY is set.
124131
125132
Flags:
126-
-h, --help Show context-sensitive help.
133+
-h, --help Show context-sensitive help.
127134
128-
--debug Enable debug logging emitted to stderr.
129-
--show-default Show the default configuration, and exit.
135+
--debug Enable debug logging emitted to stderr.
130136
`,
131137
expPanicCode: ptr.To(0),
132138
},
133-
{
134-
name: "run show default",
135-
args: []string{"run", "--show-default"},
136-
rf: func(_ context.Context, c cmdRun, _ runOpts, _, _ io.Writer) error {
137-
require.True(t, c.ShowDefault)
138-
return nil
139-
},
140-
},
141139
{
142140
name: "run with path",
143141
args: []string{"run", "./path"},
144142
rf: func(_ context.Context, c cmdRun, _ runOpts, _, _ io.Writer) error {
145143
abs, err := filepath.Abs("./path")
146144
require.NoError(t, err)
147145
require.Equal(t, abs, c.Path)
148-
require.False(t, c.ShowDefault)
149146
return nil
150147
},
151148
},
152149
}
153150
for _, tt := range tests {
154151
t.Run(tt.name, func(t *testing.T) {
152+
for k, v := range tt.env {
153+
t.Setenv(k, v)
154+
}
155155
out := &bytes.Buffer{}
156156
if tt.expPanicCode != nil {
157157
require.PanicsWithValue(t, *tt.expPanicCode, func() {

cmd/aigw/run.go

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"bytes"
1010
"context"
1111
_ "embed"
12+
"errors"
1213
"fmt"
1314
"io"
1415
"log/slog"
@@ -39,18 +40,11 @@ import (
3940
"github.com/envoyproxy/ai-gateway/internal/filterapi"
4041
)
4142

42-
var (
43-
// This is the default configuration for the AI Gateway when <path> parameter is not given.
44-
//
45-
//go:embed ai-gateway-default-resources.yaml
46-
aiGatewayDefaultResources string
47-
48-
// This is the template for the Envoy Gateway configuration where PLACEHOLDER_TMPDIR will be replaced with the temporary
49-
// directory where the resources are written to.
50-
//
51-
//go:embed envoy-gateway-config.yaml
52-
envoyGatewayConfigTemplate string
53-
)
43+
// This is the template for the Envoy Gateway configuration where PLACEHOLDER_TMPDIR will be replaced with the temporary
44+
// directory where the resources are written to.
45+
//
46+
//go:embed envoy-gateway-config.yaml
47+
var envoyGatewayConfigTemplate string
5448

5549
const (
5650
substitutionEnvAnnotationPrefix = "substitution.aigw.run/env/"
@@ -96,13 +90,6 @@ func run(ctx context.Context, c cmdRun, o runOpts, stdout, stderr io.Writer) err
9690
stderr = io.Discard
9791
}
9892
stderrLogger := slog.New(slog.NewTextHandler(stderr, &slog.HandlerOptions{}))
99-
if c.ShowDefault {
100-
_, err := stdout.Write([]byte(aiGatewayDefaultResources))
101-
if err != nil {
102-
panic(fmt.Sprintf("BUG: failed to write default resources: %v", err))
103-
}
104-
return nil
105-
}
10693

10794
// First, we need to create the self-signed certificates used for communication between the EG and Envoy.
10895
// Certificates will be placed at /tmp/envoy-gateway/certs, which is currently is not configurable:
@@ -248,21 +235,18 @@ func pollEnvoyReadiness(ctx context.Context, l *slog.Logger, addr string, interv
248235
}
249236
}
250237

251-
// readConfig reads and returns the configuration as a string from the specified path,
252-
// substituting environment variables similar to envsubst. If the path is empty and
253-
// OPENAI_API_KEY is set, it generates the configuration from OpenAI environment variables.
254-
// If the path is empty and no OPENAI_API_KEY is set, it returns the default configuration.
255-
// An error is returned if the file cannot be read or if config generation fails.
238+
// readConfig returns the configuration as a string from the given path,
239+
// substituting environment variables. If OPENAI_API_KEY is set, it generates
240+
// the config from OpenAI environment variables. Otherwise, it returns an error.
256241
func readConfig(path string) (string, error) {
257-
if path == "" {
258-
if os.Getenv("OPENAI_API_KEY") != "" {
259-
config, err := envgen.GenerateOpenAIConfig()
260-
if err != nil {
261-
return "", fmt.Errorf("error generating OpenAI config: %w", err)
262-
}
263-
return config, nil
242+
if os.Getenv("OPENAI_API_KEY") != "" {
243+
config, err := envgen.GenerateOpenAIConfig()
244+
if err != nil {
245+
return "", fmt.Errorf("error generating OpenAI config: %w", err)
264246
}
265-
return aiGatewayDefaultResources, nil
247+
return config, nil
248+
} else if path == "" {
249+
return "", errors.New("you must supply at least OPENAI_API_KEY or a config file path")
266250
}
267251
configBytes, err := envsubst.ReadFile(path)
268252
if err != nil {

0 commit comments

Comments
 (0)