From 7c1801b3027ba5ded8f0b396a83ceb3f4b6d325a Mon Sep 17 00:00:00 2001 From: Jason Song Date: Tue, 4 Apr 2023 14:32:01 +0800 Subject: [PATCH] Config for container network (#96) Fix #66 Reviewed-on: https://gitea.com/gitea/act_runner/pulls/96 Reviewed-by: Lunny Xiao --- cmd/daemon.go | 1 + config/config.example.yaml | 4 ++++ config/config.go | 6 ++++++ runtime/runtime.go | 3 ++- runtime/task.go | 4 ++-- 5 files changed, 15 insertions(+), 3 deletions(-) diff --git a/cmd/daemon.go b/cmd/daemon.go index b71815f..188491d 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -71,6 +71,7 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, ForgeInstance: reg.Address, Environ: cfg.Runner.Envs, Labels: reg.Labels, + Network: cfg.Container.Network, Version: version, } diff --git a/config/config.example.yaml b/config/config.example.yaml index 10f3e47..1f05f68 100644 --- a/config/config.example.yaml +++ b/config/config.example.yaml @@ -36,3 +36,7 @@ cache: # The port of the cache server. # 0 means to use a random available port. port: 0 + +container: + # Which network to use for the job containers. + network: bridge diff --git a/config/config.go b/config/config.go index f7cc864..40fef9a 100644 --- a/config/config.go +++ b/config/config.go @@ -28,6 +28,9 @@ type Config struct { Host string `yaml:"host"` Port uint16 `yaml:"port"` } `yaml:"cache"` + Container struct { + Network string `yaml:"network"` + } } // LoadDefault returns the default configuration. @@ -81,6 +84,9 @@ func LoadDefault(file string) (*Config, error) { cfg.Cache.Dir = filepath.Join(home, ".cache", "actcache") } } + if cfg.Container.Network == "" { + cfg.Container.Network = "bridge" + } return cfg, nil } diff --git a/runtime/runtime.go b/runtime/runtime.go index 9d24caf..068dd4f 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -18,6 +18,7 @@ type Runner struct { Environ map[string]string Client client.Client Labels []string + Network string CacheHandler *artifactcache.Handler } @@ -30,7 +31,7 @@ func (s *Runner) Run(ctx context.Context, task *runnerv1.Task) error { if s.CacheHandler != nil { env["ACTIONS_CACHE_URL"] = s.CacheHandler.ExternalURL() + "/" } - return NewTask(s.ForgeInstance, task.Id, s.Client, env, s.platformPicker).Run(ctx, task, s.Machine, s.Version) + return NewTask(task.Id, s.Client, env, s.Network, s.platformPicker).Run(ctx, task, s.Machine, s.Version) } func (s *Runner) platformPicker(labels []string) string { diff --git a/runtime/task.go b/runtime/task.go index bffe098..ece4ad0 100644 --- a/runtime/task.go +++ b/runtime/task.go @@ -71,11 +71,11 @@ type Task struct { } // NewTask creates a new task -func NewTask(forgeInstance string, buildID int64, client client.Client, runnerEnvs map[string]string, picker func([]string) string) *Task { +func NewTask(buildID int64, client client.Client, runnerEnvs map[string]string, network string, picker func([]string) string) *Task { task := &Task{ Input: &TaskInput{ envs: runnerEnvs, - containerNetworkMode: "bridge", // TODO should be configurable + containerNetworkMode: network, }, BuildID: buildID,