From 0a41e2be4bab9b794f0c8b5164a901f87a585ae2 Mon Sep 17 00:00:00 2001 From: s3lph Date: Tue, 14 Nov 2023 22:02:37 +0000 Subject: [PATCH] [FORGEJO] feat(docker): Add flag to enable IPv6 in auto-created networks (#24) Implements one part of forgejo/runner#119. The other part is a corresponding PR in forgejo/runner: forgejo/runner#120. Reviewed-on: https://code.forgejo.org/forgejo/act/pulls/24 Reviewed-by: earl-warren Co-authored-by: s3lph Co-committed-by: s3lph --- pkg/container/docker_network.go | 7 ++----- pkg/runner/run_context.go | 14 ++++++++------ pkg/runner/runner.go | 3 ++- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/pkg/container/docker_network.go b/pkg/container/docker_network.go index 6c2676f..0bd062b 100644 --- a/pkg/container/docker_network.go +++ b/pkg/container/docker_network.go @@ -9,7 +9,7 @@ import ( "github.com/nektos/act/pkg/common" ) -func NewDockerNetworkCreateExecutor(name string) common.Executor { +func NewDockerNetworkCreateExecutor(name string, config *types.NetworkCreate) common.Executor { return func(ctx context.Context) error { cli, err := GetDockerClient(ctx) if err != nil { @@ -30,10 +30,7 @@ func NewDockerNetworkCreateExecutor(name string) common.Executor { } } - _, err = cli.NetworkCreate(ctx, name, types.NetworkCreate{ - Driver: "bridge", - Scope: "local", - }) + _, err = cli.NetworkCreate(ctx, name, *config) if err != nil { return err } diff --git a/pkg/runner/run_context.go b/pkg/runner/run_context.go index 1f0924f..b2b3627 100644 --- a/pkg/runner/run_context.go +++ b/pkg/runner/run_context.go @@ -20,6 +20,7 @@ import ( "strings" "text/template" + docker "github.com/docker/docker/api/types" "github.com/docker/go-connections/nat" "github.com/nektos/act/pkg/common" "github.com/nektos/act/pkg/container" @@ -97,14 +98,10 @@ func (rc *RunContext) jobContainerName() string { } // networkName return the name of the network which will be created by `act` automatically for job, -// only create network if using a service container func (rc *RunContext) networkName() (string, bool) { - if len(rc.Run.Job().Services) > 0 { + if len(rc.Run.Job().Services) > 0 || rc.Config.ContainerNetworkMode == "" { return fmt.Sprintf("%s-%s-network", rc.jobContainerName(), rc.Run.JobID), true } - if rc.Config.ContainerNetworkMode == "" { - return "host", false - } return string(rc.Config.ContainerNetworkMode), false } @@ -571,11 +568,16 @@ func (rc *RunContext) startJobContainer() common.Executor { return errors.New("Failed to create job container") } + networkConfig := docker.NetworkCreate{ + Driver: "bridge", + Scope: "local", + EnableIPv6: rc.Config.ContainerNetworkEnableIPv6, + } return common.NewPipelineExecutor( rc.pullServicesImages(rc.Config.ForcePull), rc.JobContainer.Pull(rc.Config.ForcePull), rc.stopJobContainer(), - container.NewDockerNetworkCreateExecutor(networkName).IfBool(!rc.IsHostEnv(ctx) && rc.Config.ContainerNetworkMode == ""), // if the value of `ContainerNetworkMode` is empty string, then will create a new network for containers. + container.NewDockerNetworkCreateExecutor(networkName, &networkConfig).IfBool(!rc.IsHostEnv(ctx) && rc.Config.ContainerNetworkMode == ""), // if the value of `ContainerNetworkMode` is empty string, then will create a new network for containers. rc.startServiceContainers(networkName), rc.JobContainer.Create(rc.Config.ContainerCapAdd, rc.Config.ContainerCapDrop), rc.JobContainer.Start(false), diff --git a/pkg/runner/runner.go b/pkg/runner/runner.go index b08e715..a4dd989 100644 --- a/pkg/runner/runner.go +++ b/pkg/runner/runner.go @@ -73,6 +73,8 @@ type Config struct { JobLoggerLevel *log.Level // the level of job logger ValidVolumes []string // only volumes (and bind mounts) in this slice can be mounted on the job container or service containers InsecureSkipTLS bool // whether to skip verifying TLS certificate of the Gitea instance + + ContainerNetworkEnableIPv6 bool // create the network with IPv6 support enabled } // GetToken: Adapt to Gitea @@ -209,7 +211,6 @@ func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor { stageExecutor = append(stageExecutor, func(ctx context.Context) error { jobName := fmt.Sprintf("%-*s", maxJobNameLen, rc.String()) executor, err := rc.Executor() - if err != nil { return err }