Skip to content

Commit 6e1bc38

Browse files
authored
feat: support networks (#41)
Introduces the ability to specify networks for containers, ensuring that tasks can be run within specific network environments.
1 parent d6a7e4b commit 6e1bc38

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

internal/engine/clients/runner/docker/runner.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/docker/docker/api/types/filters"
1313
"github.com/docker/docker/api/types/image"
1414
"github.com/docker/docker/api/types/mount"
15+
"github.com/docker/docker/api/types/network"
1516
"github.com/docker/docker/api/types/volume"
1617
"github.com/docker/docker/client"
1718
"github.com/docker/docker/pkg/stdcopy"
@@ -65,7 +66,15 @@ func (r *dockerRunner) Run(ctx context.Context, opts ...runner.RunOption) (strin
6566
Mounts: mounts,
6667
}
6768

68-
rsp, err := r.client.ContainerCreate(ctx, &cc, &hc, nil, nil, "")
69+
nc := network.NetworkingConfig{
70+
EndpointsConfig: map[string]*network.EndpointSettings{},
71+
}
72+
73+
for _, nw := range options.Networks {
74+
nc.EndpointsConfig[nw] = &network.EndpointSettings{NetworkID: nw}
75+
}
76+
77+
rsp, err := r.client.ContainerCreate(ctx, &cc, &hc, &nc, nil, "")
6978
if err != nil {
7079
// span
7180
slog.ErrorContext(ctx, "failed to create container", "image", options.Image, "error", err)

internal/engine/clients/runner/options.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ func NewOptions(opts ...Option) Options {
3030
type RunOption func(o *RunOptions)
3131

3232
type RunOptions struct {
33-
ID string
34-
Image string
35-
Cmd []string
36-
Env []string
37-
Volumes []string
38-
Context context.Context
33+
ID string
34+
Image string
35+
Cmd []string
36+
Env []string
37+
Volumes []string
38+
Networks []string
39+
Context context.Context
3940
}
4041

4142
func RunWithID(id string) RunOption {
@@ -68,6 +69,12 @@ func RunWithVolumes(volumes []string) RunOption {
6869
}
6970
}
7071

72+
func RunWithNetworks(networks []string) RunOption {
73+
return func(o *RunOptions) {
74+
o.Networks = networks
75+
}
76+
}
77+
7178
func NewRunOptions(opts ...RunOption) RunOptions {
7279
options := RunOptions{
7380
Context: context.Background(),

internal/engine/services/worker/service.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ func (s *Service) runTask(ctx context.Context, t *task.Task) error {
124124
for _, pre := range t.Pre {
125125
pre.ID = strings.ReplaceAll(uuid.NewString(), "-", "")
126126
pre.Volumes = t.Volumes
127+
pre.Networks = t.Networks
127128
result, err := s.run(ctx, pre)
128129
finished := time.Now()
129130
if err != nil {
@@ -171,6 +172,7 @@ func (s *Service) runTask(ctx context.Context, t *task.Task) error {
171172
for _, post := range t.Post {
172173
post.ID = strings.ReplaceAll(uuid.NewString(), "-", "")
173174
post.Volumes = t.Volumes
175+
post.Networks = t.Networks
174176
result, err := s.run(ctx, post)
175177
finished := time.Now()
176178
if err != nil {
@@ -224,6 +226,7 @@ func (s *Service) run(ctx context.Context, t *task.Task) (string, error) {
224226
runner.RunWithCmd(t.Cmd),
225227
runner.RunWithEnv(t.Env),
226228
runner.RunWithVolumes(t.Volumes),
229+
runner.RunWithNetworks(t.Networks),
227230
}
228231

229232
return s.runner.Run(ctx, runOpts...)

internal/task/domain.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Task struct {
3131
Pre []*Task `json:"pre,omitempty"`
3232
Post []*Task `json:"post,omitempty"`
3333
Volumes []string `json:"volumes,omitempty"`
34+
Networks []string `json:"networks,omitempty"`
3435
Retry *Retry `json:"retry,omitempty"`
3536
Timeout string `json:"timeout,omitempty"`
3637
}

0 commit comments

Comments
 (0)