Skip to content

Commit 83dd747

Browse files
authored
cli: fixes race during config writes on pre-startup (#1080)
1 parent 2607e82 commit 83dd747

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

cmd/aigw/run.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ func run(ctx context.Context, c cmdRun, stdout, stderr io.Writer) error {
100100
return fmt.Errorf("failed to execute certgen: %w: %s", err, certGenOut.String())
101101
}
102102

103-
tmpdir := os.TempDir()
103+
tmpdir := filepath.Join(os.TempDir(), "aigw-run")
104+
if err := recreateDir(tmpdir); err != nil {
105+
return fmt.Errorf("failed to create temporary directory %s: %w", tmpdir, err)
106+
}
104107
egConfigPath := filepath.Join(tmpdir, "envoy-gateway-config.yaml") // 1. The path to the Envoy Gateway config.
105108
resourcesTmpdir := filepath.Join(tmpdir, "/envoy-ai-gateway-resources") // 2. The path to the resources.
106109
if err := recreateDir(resourcesTmpdir); err != nil {
@@ -119,17 +122,12 @@ func run(ctx context.Context, c cmdRun, stdout, stderr io.Writer) error {
119122
// Write the Envoy Gateway resources into a file under resourcesTmpdir.
120123
resourceYamlPath := filepath.Join(resourcesTmpdir, "config.yaml")
121124
stderrLogger.Info("Creating Envoy Gateway resource file", "path", resourceYamlPath)
122-
f, err := os.Create(resourceYamlPath)
123-
defer func() {
124-
_ = f.Close()
125-
}()
126-
if err != nil {
127-
return fmt.Errorf("failed to create file %s: %w", resourceYamlPath, err)
128-
}
129125
udsPath := filepath.Join(tmpdir, "uds.sock")
130126
_ = os.Remove(udsPath)
127+
131128
// Do the translation of the given AI Gateway resources Yaml into Envoy Gateway resources and write them to the file.
132-
runCtx := &runCmdContext{envoyGatewayResourcesOut: f, stderrLogger: stderrLogger, udsPath: udsPath, tmpdir: tmpdir, isDebug: c.Debug}
129+
resourcesBuf := &bytes.Buffer{}
130+
runCtx := &runCmdContext{envoyGatewayResourcesOut: resourcesBuf, stderrLogger: stderrLogger, udsPath: udsPath, tmpdir: tmpdir, isDebug: c.Debug}
133131
// Use the default configuration if the path is not given.
134132
aiGatewayResourcesYaml := aiGatewayDefaultResources
135133
if c.Path != "" {
@@ -140,17 +138,21 @@ func run(ctx context.Context, c cmdRun, stdout, stderr io.Writer) error {
140138
}
141139
aiGatewayResourcesYaml = string(yamlBytes)
142140
}
143-
fakeCleint, err := runCtx.writeEnvoyResourcesAndRunExtProc(ctx, aiGatewayResourcesYaml)
141+
fakeClient, err := runCtx.writeEnvoyResourcesAndRunExtProc(ctx, aiGatewayResourcesYaml)
144142
if err != nil {
145-
return err
143+
return fmt.Errorf("failed to write envoy resources and run extproc: %w", err)
144+
}
145+
err = os.WriteFile(resourceYamlPath, resourcesBuf.Bytes(), 0o600)
146+
if err != nil {
147+
return fmt.Errorf("failed to write file %s: %w", resourceYamlPath, err)
146148
}
147149

148150
lis, err := net.Listen("tcp", "localhost:1061")
149151
if err != nil {
150152
return fmt.Errorf("failed to listen: %w", err)
151153
}
152154
s := grpc.NewServer()
153-
extSrv := extensionserver.New(fakeCleint, ctrl.Log, udsPath, true)
155+
extSrv := extensionserver.New(fakeClient, ctrl.Log, udsPath, true)
154156
egextension.RegisterEnvoyGatewayExtensionServer(s, extSrv)
155157
grpc_health_v1.RegisterHealthServer(s, extSrv)
156158
go func() {

0 commit comments

Comments
 (0)