From df3cb60978d6d2bc928f0085a504b7c7e9bd26e7 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 b0cf3e7..e826970 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -74,6 +74,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 2679b03..34f181c 100644 --- a/config/config.go +++ b/config/config.go @@ -31,6 +31,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. @@ -84,6 +87,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 37cc15b..8e8bf01 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -22,6 +22,7 @@ type Runner struct { Environ map[string]string Client client.Client Labels []string + Network string CacheHandler *artifactcache.Handler } @@ -34,7 +35,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 86cde60..e11f65d 100644 --- a/runtime/task.go +++ b/runtime/task.go @@ -74,11 +74,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,