mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-09-15 18:57:01 +00:00
chore(refactor): split Options into ConfigOptions & JobOptions
They are both command line options to be parsed as if provided to docker-run, but they are not to be trusted in the same way.
This commit is contained in:
parent
b03a6b9004
commit
fc518884f9
4 changed files with 22 additions and 13 deletions
|
@ -26,11 +26,13 @@ type NewContainerInput struct {
|
|||
Privileged bool
|
||||
UsernsMode string
|
||||
Platform string
|
||||
Options string
|
||||
NetworkAliases []string
|
||||
ExposedPorts nat.PortSet
|
||||
PortBindings nat.PortMap
|
||||
|
||||
ConfigOptions string
|
||||
JobOptions string
|
||||
|
||||
// Gitea specific
|
||||
AutoRemove bool
|
||||
|
||||
|
|
|
@ -350,22 +350,24 @@ func (cr *containerReference) mergeContainerConfigs(ctx context.Context, config
|
|||
logger := common.Logger(ctx)
|
||||
input := cr.input
|
||||
|
||||
if input.Options == "" {
|
||||
if input.ConfigOptions == "" && input.JobOptions == "" {
|
||||
return config, hostConfig, nil
|
||||
}
|
||||
|
||||
options := input.ConfigOptions + " " + input.JobOptions
|
||||
|
||||
// parse configuration from CLI container.options
|
||||
flags := pflag.NewFlagSet("container_flags", pflag.ContinueOnError)
|
||||
copts := addFlags(flags)
|
||||
|
||||
optionsArgs, err := shellquote.Split(input.Options)
|
||||
optionsArgs, err := shellquote.Split(options)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("Cannot split container options: '%s': '%w'", input.Options, err)
|
||||
return nil, nil, fmt.Errorf("Cannot split container options: '%s': '%w'", options, err)
|
||||
}
|
||||
|
||||
err = flags.Parse(optionsArgs)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("Cannot parse container options: '%s': '%w'", input.Options, err)
|
||||
return nil, nil, fmt.Errorf("Cannot parse container options: '%s': '%w'", options, err)
|
||||
}
|
||||
|
||||
// If a service container's network is set to `host`, the container will not be able to
|
||||
|
@ -386,14 +388,14 @@ func (cr *containerReference) mergeContainerConfigs(ctx context.Context, config
|
|||
|
||||
containerConfig, err := parse(flags, copts, runtime.GOOS)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("Cannot process container options: '%s': '%w'", input.Options, err)
|
||||
return nil, nil, fmt.Errorf("Cannot process container options: '%s': '%w'", options, err)
|
||||
}
|
||||
|
||||
logger.Debugf("Custom container.Config from options ==> %+v", containerConfig.Config)
|
||||
|
||||
err = mergo.Merge(config, containerConfig.Config, mergo.WithOverride, mergo.WithAppendSlice)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("Cannot merge container.Config options: '%s': '%w'", input.Options, err)
|
||||
return nil, nil, fmt.Errorf("Cannot merge container.Config options: '%s': '%w'", options, err)
|
||||
}
|
||||
logger.Debugf("Merged container.Config ==> %+v", config)
|
||||
|
||||
|
@ -406,7 +408,7 @@ func (cr *containerReference) mergeContainerConfigs(ctx context.Context, config
|
|||
networkMode := hostConfig.NetworkMode
|
||||
err = mergo.Merge(hostConfig, containerConfig.HostConfig, mergo.WithOverride)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("Cannot merge container.HostConfig options: '%s': '%w'", input.Options, err)
|
||||
return nil, nil, fmt.Errorf("Cannot merge container.HostConfig options: '%s': '%w'", options, err)
|
||||
}
|
||||
hostConfig.Binds = binds
|
||||
hostConfig.Mounts = mounts
|
||||
|
|
|
@ -429,9 +429,10 @@ func newStepContainer(ctx context.Context, step step, image string, cmd []string
|
|||
Privileged: rc.Config.Privileged,
|
||||
UsernsMode: rc.Config.UsernsMode,
|
||||
Platform: rc.Config.ContainerArchitecture,
|
||||
Options: rc.Config.ContainerOptions,
|
||||
AutoRemove: rc.Config.AutoRemove,
|
||||
ValidVolumes: rc.Config.ValidVolumes,
|
||||
|
||||
ConfigOptions: rc.Config.ContainerOptions,
|
||||
})
|
||||
return stepContainer
|
||||
}
|
||||
|
|
|
@ -489,12 +489,14 @@ func (rc *RunContext) startJobContainer() common.Executor {
|
|||
UsernsMode: rc.Config.UsernsMode,
|
||||
Platform: rc.Config.ContainerArchitecture,
|
||||
AutoRemove: rc.Config.AutoRemove,
|
||||
Options: rc.ExprEval.Interpolate(ctx, spec.Options),
|
||||
NetworkMode: networkName,
|
||||
NetworkAliases: []string{serviceID},
|
||||
ExposedPorts: exposedPorts,
|
||||
PortBindings: portBindings,
|
||||
ValidVolumes: rc.Config.ValidVolumes,
|
||||
|
||||
JobOptions: rc.ExprEval.Interpolate(ctx, spec.Options),
|
||||
ConfigOptions: rc.Config.ContainerOptions,
|
||||
})
|
||||
rc.ServiceContainers = append(rc.ServiceContainers, c)
|
||||
}
|
||||
|
@ -549,9 +551,11 @@ func (rc *RunContext) startJobContainer() common.Executor {
|
|||
Privileged: rc.Config.Privileged,
|
||||
UsernsMode: rc.Config.UsernsMode,
|
||||
Platform: rc.Config.ContainerArchitecture,
|
||||
Options: rc.options(ctx),
|
||||
AutoRemove: rc.Config.AutoRemove,
|
||||
ValidVolumes: rc.Config.ValidVolumes,
|
||||
|
||||
JobOptions: rc.options(ctx),
|
||||
ConfigOptions: "",
|
||||
})
|
||||
if rc.JobContainer == nil {
|
||||
return errors.New("Failed to create job container")
|
||||
|
@ -889,10 +893,10 @@ func (rc *RunContext) options(ctx context.Context) string {
|
|||
job := rc.Run.Job()
|
||||
c := job.Container()
|
||||
if c != nil {
|
||||
return rc.Config.ContainerOptions + " " + rc.ExprEval.Interpolate(ctx, c.Options)
|
||||
return rc.ExprEval.Interpolate(ctx, c.Options)
|
||||
}
|
||||
|
||||
return rc.Config.ContainerOptions
|
||||
return ""
|
||||
}
|
||||
|
||||
func (rc *RunContext) isEnabled(ctx context.Context) (bool, error) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue