Skip to content

Commit 4c2524a

Browse files
feat: --container-options (#1462)
* feat: `--container-options` This deprecates the following options - `--privileged` - `--container-cap-add` - `--container-cap-drop` - `--container-architecture` - `--userns` * Merge binds/mounts, add desc * avoid linter error * fix: apply options to step env / deprecate warning Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent d9fe63e commit 4c2524a

File tree

6 files changed

+28
-1
lines changed

6 files changed

+28
-1
lines changed

cmd/input.go

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Input struct {
3030
usernsMode string
3131
containerArchitecture string
3232
containerDaemonSocket string
33+
containerOptions string
3334
noWorkflowRecurse bool
3435
useGitIgnore bool
3536
githubInstance string

cmd/root.go

+18
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func Execute(ctx context.Context, version string) {
7676
rootCmd.PersistentFlags().StringVarP(&input.envfile, "env-file", "", ".env", "environment file to read and use as env in the containers")
7777
rootCmd.PersistentFlags().StringVarP(&input.containerArchitecture, "container-architecture", "", "", "Architecture which should be used to run containers, e.g.: linux/amd64. If not specified, will use host default architecture. Requires Docker server API Version 1.41+. Ignored on earlier Docker server platforms.")
7878
rootCmd.PersistentFlags().StringVarP(&input.containerDaemonSocket, "container-daemon-socket", "", "/var/run/docker.sock", "Path to Docker daemon socket which will be mounted to containers")
79+
rootCmd.PersistentFlags().StringVarP(&input.containerOptions, "container-options", "", "", "Custom docker container options for the job container without an options property in the job definition")
7980
rootCmd.PersistentFlags().StringVarP(&input.githubInstance, "github-instance", "", "github.com", "GitHub instance to use. Don't use this if you are not using GitHub Enterprise Server.")
8081
rootCmd.PersistentFlags().StringVarP(&input.artifactServerPath, "artifact-server-path", "", "", "Defines the path where the artifact server stores uploads and retrieves downloads from. If not specified the artifact server will not start.")
8182
rootCmd.PersistentFlags().StringVarP(&input.artifactServerPort, "artifact-server-port", "", "34567", "Defines the port where the artifact server listens (will only bind to localhost).")
@@ -414,6 +415,22 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
414415
input.platforms = readArgsFile(cfgLocations[0], true)
415416
}
416417
}
418+
deprecationWarning := "--%s is deprecated and will be removed soon, please switch to cli: `--container-options \"%[2]s\"` or `.actrc`: `--container-options %[2]s`."
419+
if input.privileged {
420+
log.Warnf(deprecationWarning, "privileged", "--privileged")
421+
}
422+
if len(input.usernsMode) > 0 {
423+
log.Warnf(deprecationWarning, "userns", fmt.Sprintf("--userns=%s", input.usernsMode))
424+
}
425+
if len(input.containerArchitecture) > 0 {
426+
log.Warnf(deprecationWarning, "container-architecture", fmt.Sprintf("--platform=%s", input.containerArchitecture))
427+
}
428+
if len(input.containerCapAdd) > 0 {
429+
log.Warnf(deprecationWarning, "container-cap-add", fmt.Sprintf("--cap-add=%s", input.containerCapAdd))
430+
}
431+
if len(input.containerCapDrop) > 0 {
432+
log.Warnf(deprecationWarning, "container-cap-drop", fmt.Sprintf("--cap-drop=%s", input.containerCapDrop))
433+
}
417434

418435
// run the plan
419436
config := &runner.Config{
@@ -437,6 +454,7 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
437454
UsernsMode: input.usernsMode,
438455
ContainerArchitecture: input.containerArchitecture,
439456
ContainerDaemonSocket: input.containerDaemonSocket,
457+
ContainerOptions: input.containerOptions,
440458
UseGitIgnore: input.useGitIgnore,
441459
GitHubInstance: input.githubInstance,
442460
ContainerCapAdd: input.containerCapAdd,

pkg/container/docker_run.go

+6
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,16 @@ func (cr *containerReference) mergeContainerConfigs(ctx context.Context, config
411411

412412
logger.Debugf("Custom container.HostConfig from options ==> %+v", containerConfig.HostConfig)
413413

414+
hostConfig.Binds = append(hostConfig.Binds, containerConfig.HostConfig.Binds...)
415+
hostConfig.Mounts = append(hostConfig.Mounts, containerConfig.HostConfig.Mounts...)
416+
binds := hostConfig.Binds
417+
mounts := hostConfig.Mounts
414418
err = mergo.Merge(hostConfig, containerConfig.HostConfig, mergo.WithOverride)
415419
if err != nil {
416420
return nil, nil, fmt.Errorf("Cannot merge container.HostConfig options: '%s': '%w'", input.Options, err)
417421
}
422+
hostConfig.Binds = binds
423+
hostConfig.Mounts = mounts
418424
logger.Debugf("Merged container.HostConfig ==> %+v", hostConfig)
419425

420426
return config, hostConfig, nil

pkg/runner/action.go

+1
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ func newStepContainer(ctx context.Context, step step, image string, cmd []string
366366
Privileged: rc.Config.Privileged,
367367
UsernsMode: rc.Config.UsernsMode,
368368
Platform: rc.Config.ContainerArchitecture,
369+
Options: rc.Config.ContainerOptions,
369370
})
370371
return stepContainer
371372
}

pkg/runner/run_context.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ func (rc *RunContext) options(ctx context.Context) string {
410410
job := rc.Run.Job()
411411
c := job.Container()
412412
if c == nil {
413-
return ""
413+
return rc.Config.ContainerOptions
414414
}
415415

416416
return c.Options

pkg/runner/runner.go

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type Config struct {
3939
UsernsMode string // user namespace to use
4040
ContainerArchitecture string // Desired OS/architecture platform for running containers
4141
ContainerDaemonSocket string // Path to Docker daemon socket
42+
ContainerOptions string // Options for the job container
4243
UseGitIgnore bool // controls if paths in .gitignore should not be copied into container, default true
4344
GitHubInstance string // GitHub instance to use, default "github.com"
4445
ContainerCapAdd []string // list of kernel capabilities to add to the containers

0 commit comments

Comments
 (0)