9
9
cfg "github.com/stacklok/toolhive/pkg/config"
10
10
"github.com/stacklok/toolhive/pkg/container"
11
11
"github.com/stacklok/toolhive/pkg/container/runtime"
12
+ "github.com/stacklok/toolhive/pkg/environment"
12
13
"github.com/stacklok/toolhive/pkg/ignore"
13
14
"github.com/stacklok/toolhive/pkg/networking"
14
15
"github.com/stacklok/toolhive/pkg/process"
@@ -184,16 +185,16 @@ func AddRunFlags(cmd *cobra.Command, config *RunFlags) {
184
185
// BuildRunnerConfig creates a runner.RunConfig from the configuration
185
186
func BuildRunnerConfig (
186
187
ctx context.Context ,
187
- runConfig * RunFlags ,
188
+ runFlags * RunFlags ,
188
189
serverOrImage string ,
189
190
cmdArgs []string ,
190
191
debugMode bool ,
191
192
cmd * cobra.Command ,
192
193
) (* runner.RunConfig , error ) {
193
194
// Validate the host flag
194
- validatedHost , err := ValidateAndNormaliseHostFlag (runConfig .Host )
195
+ validatedHost , err := ValidateAndNormaliseHostFlag (runFlags .Host )
195
196
if err != nil {
196
- return nil , fmt .Errorf ("invalid host: %s" , runConfig .Host )
197
+ return nil , fmt .Errorf ("invalid host: %s" , runFlags .Host )
197
198
}
198
199
199
200
// Get OIDC flags
@@ -212,15 +213,15 @@ func BuildRunnerConfig(
212
213
// Get OTEL flag values with config fallbacks
213
214
config := cfg .GetConfig ()
214
215
finalOtelEndpoint , finalOtelSamplingRate , finalOtelEnvironmentVariables := getTelemetryFromFlags (cmd , config ,
215
- runConfig .OtelEndpoint , runConfig .OtelSamplingRate , runConfig .OtelEnvironmentVariables )
216
+ runFlags .OtelEndpoint , runFlags .OtelSamplingRate , runFlags .OtelEnvironmentVariables )
216
217
217
218
// Create container runtime
218
219
rt , err := container .NewFactory ().Create (ctx )
219
220
if err != nil {
220
221
return nil , fmt .Errorf ("failed to create container runtime: %v" , err )
221
222
}
222
223
223
- // Select an env var validation strategy depending on how the CLI is run:
224
+ // Select an envVars var validation strategy depending on how the CLI is run:
224
225
// If we have called the CLI directly, we use the CLIEnvVarValidator.
225
226
// If we are running in detached mode, or the CLI is wrapped by the K8s operator,
226
227
// we use the DetachedEnvVarValidator.
@@ -241,52 +242,58 @@ func BuildRunnerConfig(
241
242
// Take the MCP server we were supplied and either fetch the image, or
242
243
// build it from a protocol scheme. If the server URI refers to an image
243
244
// in our trusted registry, we will also fetch the image metadata.
244
- imageURL , imageMetadata , err = retriever .GetMCPServer (ctx , serverOrImage , runConfig .CACertPath , runConfig .VerifyImage )
245
+ imageURL , imageMetadata , err = retriever .GetMCPServer (ctx , serverOrImage , runFlags .CACertPath , runFlags .VerifyImage )
245
246
if err != nil {
246
247
return nil , fmt .Errorf ("failed to find or create the MCP server %s: %v" , serverOrImage , err )
247
248
}
248
249
}
249
250
250
251
// Validate proxy mode early
251
- if ! types .IsValidProxyMode (runConfig .ProxyMode ) {
252
- if runConfig .ProxyMode == "" {
253
- runConfig .ProxyMode = types .ProxyModeSSE .String () // default to SSE for backward compatibility
252
+ if ! types .IsValidProxyMode (runFlags .ProxyMode ) {
253
+ if runFlags .ProxyMode == "" {
254
+ runFlags .ProxyMode = types .ProxyModeSSE .String () // default to SSE for backward compatibility
254
255
} else {
255
- return nil , fmt .Errorf ("invalid value for --proxy-mode: %s" , runConfig .ProxyMode )
256
+ return nil , fmt .Errorf ("invalid value for --proxy-mode: %s" , runFlags .ProxyMode )
256
257
}
257
258
}
258
259
260
+ // Parse the environment variables from a list of strings to a map.
261
+ envVars , err := environment .ParseEnvironmentVariables (runFlags .Env )
262
+ if err != nil {
263
+ return nil , fmt .Errorf ("failed to parse environment variables: %v" , err )
264
+ }
265
+
259
266
// Initialize a new RunConfig with values from command-line flags
260
267
return runner .NewRunConfigBuilder ().
261
268
WithRuntime (rt ).
262
269
WithCmdArgs (cmdArgs ).
263
- WithName (runConfig .Name ).
270
+ WithName (runFlags .Name ).
264
271
WithImage (imageURL ).
265
272
WithHost (validatedHost ).
266
- WithTargetHost (runConfig .TargetHost ).
273
+ WithTargetHost (runFlags .TargetHost ).
267
274
WithDebug (debugMode ).
268
- WithVolumes (runConfig .Volumes ).
269
- WithSecrets (runConfig .Secrets ).
270
- WithAuthzConfigPath (runConfig .AuthzConfig ).
271
- WithAuditConfigPath (runConfig .AuditConfig ).
272
- WithPermissionProfileNameOrPath (runConfig .PermissionProfile ).
273
- WithNetworkIsolation (runConfig .IsolateNetwork ).
274
- WithK8sPodPatch (runConfig .K8sPodPatch ).
275
- WithProxyMode (types .ProxyMode (runConfig .ProxyMode )).
276
- WithTransportAndPorts (runConfig .Transport , runConfig .ProxyPort , runConfig .TargetPort ).
277
- WithAuditEnabled (runConfig .EnableAudit , runConfig .AuditConfig ).
278
- WithLabels (runConfig .Labels ).
279
- WithGroup (runConfig .Group ).
275
+ WithVolumes (runFlags .Volumes ).
276
+ WithSecrets (runFlags .Secrets ).
277
+ WithAuthzConfigPath (runFlags .AuthzConfig ).
278
+ WithAuditConfigPath (runFlags .AuditConfig ).
279
+ WithPermissionProfileNameOrPath (runFlags .PermissionProfile ).
280
+ WithNetworkIsolation (runFlags .IsolateNetwork ).
281
+ WithK8sPodPatch (runFlags .K8sPodPatch ).
282
+ WithProxyMode (types .ProxyMode (runFlags .ProxyMode )).
283
+ WithTransportAndPorts (runFlags .Transport , runFlags .ProxyPort , runFlags .TargetPort ).
284
+ WithAuditEnabled (runFlags .EnableAudit , runFlags .AuditConfig ).
285
+ WithLabels (runFlags .Labels ).
286
+ WithGroup (runFlags .Group ).
280
287
WithOIDCConfig (oidcIssuer , oidcAudience , oidcJwksURL , oidcIntrospectionURL , oidcClientID , oidcClientSecret ,
281
- runConfig .ThvCABundle , runConfig .JWKSAuthTokenFile , runConfig .ResourceURL , runConfig .JWKSAllowPrivateIP ).
282
- WithTelemetryConfig (finalOtelEndpoint , runConfig .OtelEnablePrometheusMetricsPath , runConfig .OtelServiceName ,
283
- finalOtelSamplingRate , runConfig .OtelHeaders , runConfig .OtelInsecure , finalOtelEnvironmentVariables ).
284
- WithToolsFilter (runConfig .ToolsFilter ).
288
+ runFlags .ThvCABundle , runFlags .JWKSAuthTokenFile , runFlags .ResourceURL , runFlags .JWKSAllowPrivateIP ).
289
+ WithTelemetryConfig (finalOtelEndpoint , runFlags .OtelEnablePrometheusMetricsPath , runFlags .OtelServiceName ,
290
+ finalOtelSamplingRate , runFlags .OtelHeaders , runFlags .OtelInsecure , finalOtelEnvironmentVariables ).
291
+ WithToolsFilter (runFlags .ToolsFilter ).
285
292
WithIgnoreConfig (& ignore.Config {
286
- LoadGlobal : runConfig .IgnoreGlobally ,
287
- PrintOverlays : runConfig .PrintOverlays ,
293
+ LoadGlobal : runFlags .IgnoreGlobally ,
294
+ PrintOverlays : runFlags .PrintOverlays ,
288
295
}).
289
- Build (ctx , imageMetadata , runConfig . Env , envVarValidator )
296
+ Build (ctx , imageMetadata , envVars , envVarValidator )
290
297
}
291
298
292
299
// getOidcFromFlags extracts OIDC configuration from command flags
0 commit comments