Skip to content

Commit 4409bdd

Browse files
authored
feat: support private docker registries (#45)
1 parent 71d0996 commit 4409bdd

File tree

5 files changed

+70
-4
lines changed

5 files changed

+70
-4
lines changed

cmd/engine.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ func initBroker() broker.Broker {
152152
func initRunner() runner.Runner {
153153
return docker.NewRunner(
154154
runner.WithHost(config.RunnerHost()),
155+
runner.WithRegistryUser(config.RunnerRegistryUser()),
156+
runner.WithRegistryPass(config.RunnerRegistryPass()),
155157
)
156158
}
157159

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package docker
22

33
import (
44
"context"
5+
"encoding/base64"
6+
"encoding/json"
57
"io"
68
"log/slog"
79
"maps"
@@ -14,6 +16,7 @@ import (
1416
"github.com/docker/docker/api/types/image"
1517
"github.com/docker/docker/api/types/mount"
1618
"github.com/docker/docker/api/types/network"
19+
"github.com/docker/docker/api/types/registry"
1720
"github.com/docker/docker/api/types/volume"
1821
"github.com/docker/docker/client"
1922
"github.com/docker/docker/pkg/stdcopy"
@@ -254,7 +257,15 @@ func (r *dockerRunner) pullImage(ctx context.Context, tag string) error {
254257
}
255258
}
256259

257-
reader, err := r.client.ImagePull(ctx, tag, image.PullOptions{})
260+
authConfig := registry.AuthConfig{
261+
Username: r.options.RegistryUser,
262+
Password: r.options.RegistryPass,
263+
}
264+
265+
encoded, _ := json.Marshal(authConfig)
266+
registryAuth := base64.URLEncoding.EncodeToString(encoded)
267+
268+
reader, err := r.client.ImagePull(ctx, tag, image.PullOptions{RegistryAuth: registryAuth})
258269
if err != nil {
259270
return err
260271
}

internal/engine/clients/runner/options.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import "context"
55
type Option func(o *Options)
66

77
type Options struct {
8-
Host string
9-
Context context.Context
8+
Host string
9+
RegistryUser string
10+
RegistryPass string
11+
Context context.Context
1012
}
1113

1214
func WithHost(host string) Option {
@@ -15,6 +17,18 @@ func WithHost(host string) Option {
1517
}
1618
}
1719

20+
func WithRegistryUser(user string) Option {
21+
return func(o *Options) {
22+
o.RegistryUser = user
23+
}
24+
}
25+
26+
func WithRegistryPass(pass string) Option {
27+
return func(o *Options) {
28+
o.RegistryPass = pass
29+
}
30+
}
31+
1832
func NewOptions(opts ...Option) Options {
1933
options := Options{
2034
Context: context.Background(),

internal/engine/config/config.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ type config struct {
2929
brokerLocation string
3030
runner string
3131
runnerHost string
32+
runnerRegistryUser string
33+
runnerRegistryPass string
3234
readwriter string
3335
readwriterLocation string
3436
}
@@ -47,6 +49,8 @@ func New() {
4749
brokerLocation: "",
4850
runner: "docker",
4951
runnerHost: "unix:///var/run/docker.sock",
52+
runnerRegistryUser: "",
53+
runnerRegistryPass: "",
5054
readwriter: "memory",
5155
readwriterLocation: "",
5256
}
@@ -82,7 +86,7 @@ func New() {
8286
}
8387

8488
qs := os.Getenv("QUEUES")
85-
if len(qs) > 0 {
89+
if len(qs) > 0 && instance.mode == "worker" {
8690
for _, q := range strings.Split(qs, ",") {
8791
q = strings.TrimSpace(q)
8892
if len(q) == 0 {
@@ -132,6 +136,16 @@ func New() {
132136
instance.runnerHost = runnerHost
133137
}
134138

139+
runnerRegistryUser := os.Getenv("RUNNER_REGISTRY_USER")
140+
if len(runnerRegistryUser) > 0 {
141+
instance.runnerRegistryUser = runnerRegistryUser
142+
}
143+
144+
runnerRegistryPass := os.Getenv("RUNNER_REGISTRY_PASS")
145+
if len(runnerRegistryPass) > 0 {
146+
instance.runnerRegistryPass = runnerRegistryPass
147+
}
148+
135149
rw := os.Getenv("READ_WRITER")
136150
if len(rw) > 0 {
137151
if _, ok := readwriter.ReadWriterTypes[rw]; ok {
@@ -240,6 +254,22 @@ func RunnerHost() string {
240254
return instance.runnerHost
241255
}
242256

257+
func RunnerRegistryUser() string {
258+
if instance == nil {
259+
panic("cfg is nil")
260+
}
261+
262+
return instance.runnerRegistryUser
263+
}
264+
265+
func RunnerRegistryPass() string {
266+
if instance == nil {
267+
panic("cfg is nil")
268+
}
269+
270+
return instance.runnerRegistryPass
271+
}
272+
243273
func ReadWriter() string {
244274
if instance == nil {
245275
panic("cfg is nil")

tests/testdata/hello.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "simple example",
3+
"image": "ubuntu:mantic",
4+
"cmd": [
5+
"echo",
6+
"-n",
7+
"hello world"
8+
]
9+
}

0 commit comments

Comments
 (0)