-
Notifications
You must be signed in to change notification settings - Fork 18
/
phony.go
80 lines (66 loc) · 2.02 KB
/
phony.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package fixtures
import (
"context"
"encoding/json"
"github.com/Azure/dalec"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/exporter/containerimage/exptypes"
"github.com/moby/buildkit/frontend/dockerui"
gwclient "github.com/moby/buildkit/frontend/gateway/client"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
)
var (
goModCache = llb.AddMount("/go/pkg/mod", llb.Scratch(), llb.AsPersistentCacheDir("dalec-go-mod-cache", llb.CacheMountShared))
goBuildCache = llb.AddMount("/root/.cache/go-build", llb.Scratch(), llb.AsPersistentCacheDir("dalec-go-build-cache", llb.CacheMountShared))
)
func PhonyFrontend(ctx context.Context, gwc gwclient.Client) (*gwclient.Result, error) {
dc, err := dockerui.NewClient(gwc)
if err != nil {
return nil, err
}
bctx, err := dc.MainContext(ctx)
if err != nil {
return nil, err
}
st := llb.Image("golang:1.21", llb.WithMetaResolver(gwc)).
Run(
llb.Args([]string{"go", "build", "-o=/build/out", "./test/fixtures/phony"}),
llb.AddEnv("CGO_ENABLED", "0"),
goModCache,
goBuildCache,
llb.Dir("/build/src"),
llb.AddMount("/build/src", *bctx, llb.Readonly),
).
AddMount("/build/out", llb.Scratch())
cfg := dalec.DockerImageSpec{
Config: dalec.DockerImageConfig{
ImageConfig: ocispecs.ImageConfig{
Entrypoint: []string{"/phony"},
},
},
}
injectFrontendLabels(&cfg)
dt, err := json.Marshal(cfg)
if err != nil {
return nil, err
}
def, err := st.Marshal(ctx)
if err != nil {
return nil, err
}
res, err := gwc.Solve(ctx, gwclient.SolveRequest{
Definition: def.ToPB(),
})
if err != nil {
return nil, err
}
res.AddMeta(exptypes.ExporterImageConfigKey, dt)
return res, nil
}
func injectFrontendLabels(cfg *dalec.DockerImageSpec) {
if cfg.Config.Labels == nil {
cfg.Config.Labels = map[string]string{}
}
cfg.Config.Labels["moby.buildkit.frontend.network.none"] = "true"
cfg.Config.Labels["moby.buildkit.frontend.caps"] = "moby.buildkit.frontend.inputs,moby.buildkit.frontend.subrequests,moby.buildkit.frontend.contexts"
}