Skip to content

Commit 1281a0a

Browse files
authored
Replace runconfig from flags with builder pattern (#1267)
1 parent 77e3225 commit 1281a0a

File tree

3 files changed

+185
-325
lines changed

3 files changed

+185
-325
lines changed

cmd/thv/app/run_flags.go

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
cfg "github.com/stacklok/toolhive/pkg/config"
1010
"github.com/stacklok/toolhive/pkg/container"
1111
"github.com/stacklok/toolhive/pkg/container/runtime"
12+
"github.com/stacklok/toolhive/pkg/ignore"
1213
"github.com/stacklok/toolhive/pkg/process"
1314
"github.com/stacklok/toolhive/pkg/registry"
1415
"github.com/stacklok/toolhive/pkg/runner"
@@ -241,45 +242,36 @@ func BuildRunnerConfig(
241242
}
242243

243244
// Initialize a new RunConfig with values from command-line flags
244-
// TODO: As noted elsewhere, we should use the builder pattern here to make it more readable.
245-
return runner.NewRunConfigFromFlags(
246-
ctx, rt, cmdArgs,
247-
runConfig.Name,
248-
imageURL, imageMetadata,
249-
validatedHost,
250-
debugMode,
251-
runConfig.Volumes,
252-
runConfig.Secrets,
253-
runConfig.AuthzConfig,
254-
runConfig.AuditConfig,
255-
runConfig.EnableAudit,
256-
runConfig.PermissionProfile,
257-
runConfig.TargetHost,
258-
runConfig.Transport,
259-
runConfig.ProxyPort,
260-
runConfig.TargetPort,
261-
runConfig.Env,
262-
runConfig.Labels,
263-
oidcIssuer, oidcAudience, oidcJwksURL, oidcClientID, oidcAllowOpaqueTokens,
264-
finalOtelEndpoint,
265-
runConfig.OtelServiceName,
266-
finalOtelSamplingRate,
267-
runConfig.OtelHeaders,
268-
runConfig.OtelInsecure,
269-
runConfig.OtelEnablePrometheusMetricsPath,
270-
finalOtelEnvironmentVariables,
271-
runConfig.IsolateNetwork,
272-
runConfig.K8sPodPatch,
273-
runConfig.ThvCABundle,
274-
runConfig.JWKSAuthTokenFile,
275-
runConfig.JWKSAllowPrivateIP,
276-
envVarValidator,
277-
types.ProxyMode(runConfig.ProxyMode),
278-
runConfig.Group,
279-
runConfig.ToolsFilter,
280-
runConfig.IgnoreGlobally,
281-
runConfig.PrintOverlays,
282-
)
245+
return runner.NewRunConfigBuilder().
246+
WithRuntime(rt).
247+
WithCmdArgs(cmdArgs).
248+
WithName(runConfig.Name).
249+
WithImage(imageURL).
250+
WithHost(validatedHost).
251+
WithTargetHost(runConfig.TargetHost).
252+
WithDebug(debugMode).
253+
WithVolumes(runConfig.Volumes).
254+
WithSecrets(runConfig.Secrets).
255+
WithAuthzConfigPath(runConfig.AuthzConfig).
256+
WithAuditConfigPath(runConfig.AuditConfig).
257+
WithPermissionProfileNameOrPath(runConfig.PermissionProfile).
258+
WithNetworkIsolation(runConfig.IsolateNetwork).
259+
WithK8sPodPatch(runConfig.K8sPodPatch).
260+
WithProxyMode(types.ProxyMode(runConfig.ProxyMode)).
261+
WithTransportAndPorts(runConfig.Transport, runConfig.ProxyPort, runConfig.TargetPort).
262+
WithAuditEnabled(runConfig.EnableAudit, runConfig.AuditConfig).
263+
WithLabels(runConfig.Labels).
264+
WithGroup(runConfig.Group).
265+
WithOIDCConfig(oidcIssuer, oidcAudience, oidcJwksURL, oidcClientID, oidcAllowOpaqueTokens,
266+
runConfig.ThvCABundle, runConfig.JWKSAuthTokenFile, runConfig.JWKSAllowPrivateIP).
267+
WithTelemetryConfig(finalOtelEndpoint, runConfig.OtelEnablePrometheusMetricsPath, runConfig.OtelServiceName,
268+
finalOtelSamplingRate, runConfig.OtelHeaders, runConfig.OtelInsecure, finalOtelEnvironmentVariables).
269+
WithToolsFilter(runConfig.ToolsFilter).
270+
WithIgnoreConfig(&ignore.Config{
271+
LoadGlobal: runConfig.IgnoreGlobally,
272+
PrintOverlays: runConfig.PrintOverlays,
273+
}).
274+
Build(ctx, imageMetadata, runConfig.Env, envVarValidator)
283275
}
284276

285277
// getOidcFromFlags extracts OIDC configuration from command flags

pkg/runner/config.go

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/stacklok/toolhive/pkg/logger"
1919
"github.com/stacklok/toolhive/pkg/networking"
2020
"github.com/stacklok/toolhive/pkg/permissions"
21-
"github.com/stacklok/toolhive/pkg/registry"
2221
"github.com/stacklok/toolhive/pkg/secrets"
2322
"github.com/stacklok/toolhive/pkg/telemetry"
2423
"github.com/stacklok/toolhive/pkg/transport/types"
@@ -155,84 +154,6 @@ func NewRunConfig() *RunConfig {
155154
}
156155
}
157156

158-
// NewRunConfigFromFlags creates a new RunConfig with values from command-line flags
159-
func NewRunConfigFromFlags(
160-
ctx context.Context,
161-
runtime rt.Deployer,
162-
cmdArgs []string,
163-
name string,
164-
imageURL string,
165-
imageMetadata *registry.ImageMetadata,
166-
host string,
167-
debug bool,
168-
volumes []string,
169-
secretsList []string,
170-
authzConfigPath string,
171-
auditConfigPath string,
172-
enableAudit bool,
173-
permissionProfile string,
174-
targetHost string,
175-
mcpTransport string,
176-
port int,
177-
targetPort int,
178-
envVars []string,
179-
runLabels []string,
180-
oidcIssuer string,
181-
oidcAudience string,
182-
oidcJwksURL string,
183-
oidcClientID string,
184-
oidcAllowOpaqueTokens bool,
185-
otelEndpoint string,
186-
otelServiceName string,
187-
otelSamplingRate float64,
188-
otelHeaders []string,
189-
otelInsecure bool,
190-
otelEnablePrometheusMetricsPath bool,
191-
otelEnvironmentVariables []string,
192-
isolateNetwork bool,
193-
k8sPodPatch string,
194-
thvCABundle string,
195-
jwksAuthTokenFile string,
196-
jwksAllowPrivateIP bool,
197-
envVarValidator EnvVarValidator,
198-
proxyMode types.ProxyMode,
199-
groupName string,
200-
toolsFilter []string,
201-
ignoreGlobally bool,
202-
printOverlays bool,
203-
) (*RunConfig, error) {
204-
return NewRunConfigBuilder().
205-
WithRuntime(runtime).
206-
WithCmdArgs(cmdArgs).
207-
WithName(name).
208-
WithImage(imageURL).
209-
WithHost(host).
210-
WithTargetHost(targetHost).
211-
WithDebug(debug).
212-
WithVolumes(volumes).
213-
WithSecrets(secretsList).
214-
WithAuthzConfigPath(authzConfigPath).
215-
WithAuditConfigPath(auditConfigPath).
216-
WithPermissionProfileNameOrPath(permissionProfile).
217-
WithNetworkIsolation(isolateNetwork).
218-
WithK8sPodPatch(k8sPodPatch).
219-
WithProxyMode(proxyMode).
220-
WithTransportAndPorts(mcpTransport, port, targetPort).
221-
WithAuditEnabled(enableAudit, auditConfigPath).
222-
WithLabels(runLabels).
223-
WithGroup(groupName).
224-
WithOIDCConfig(oidcIssuer, oidcAudience, oidcJwksURL, oidcClientID, oidcAllowOpaqueTokens,
225-
thvCABundle, jwksAuthTokenFile, jwksAllowPrivateIP).
226-
WithTelemetryConfig(otelEndpoint, otelEnablePrometheusMetricsPath, otelServiceName,
227-
otelSamplingRate, otelHeaders, otelInsecure, otelEnvironmentVariables).
228-
WithToolsFilter(toolsFilter).
229-
WithIgnoreConfig(&ignore.Config{
230-
LoadGlobal: ignoreGlobally,
231-
PrintOverlays: printOverlays,
232-
}).
233-
Build(ctx, imageMetadata, envVars, envVarValidator)
234-
}
235-
236157
// WithAuthz adds authorization configuration to the RunConfig
237158
func (c *RunConfig) WithAuthz(config *authz.Config) *RunConfig {
238159
c.AuthzConfig = config

0 commit comments

Comments
 (0)