-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.go
129 lines (111 loc) · 3.29 KB
/
build.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package gockerfile
import (
"context"
"encoding/json"
"fmt"
"github.com/containerd/containerd/platforms"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/exporter/containerimage/exptypes"
"github.com/moby/buildkit/frontend/gateway/client"
"github.com/pkg/errors"
"github.com/po3rin/gockerfile/config"
)
const (
LocalNameContext = "context"
LocalNameDockerfile = "dockerfile"
LocalNameGockerfile = "gockerfile"
keyTarget = "target"
keyFilename = "filename"
keyCacheFrom = "cache-from"
defaultGockerfileName = "Gockerfile.yaml"
dockerignoreFilename = ".dockerignore"
buildArgPrefix = "build-arg:"
labelPrefix = "label:"
keyNoCache = "no-cache"
keyTargetPlatform = "platform"
keyMultiPlatform = "multi-platform"
keyImageResolveMode = "image-resolve-mode"
keyGlobalAddHosts = "add-hosts"
keyForceNetwork = "force-network-mode"
keyOverrideCopyImage = "override-copy-image" // remove after CopyOp implemented
)
// Build builds Docker Image. Internaly, get config from Gockerfile.yml & convert LLB & solve.
func Build(ctx context.Context, c client.Client) (*client.Result, error) {
cfg, err := GetGockerfileConfig(ctx, c)
if err != nil {
return nil, errors.Wrap(err, "got error getting gockerfile")
}
st, img, err := Gocker2LLB(cfg)
if err != nil {
return nil, errors.Wrapf(err, "failed to convert gocker to llb")
}
def, err := st.Marshal()
if err != nil {
return nil, errors.Wrapf(err, "failed to marshal local source")
}
res, err := c.Solve(ctx, client.SolveRequest{
Definition: def.ToPB(),
})
if err != nil {
return nil, errors.Wrapf(err, "failed to resolve dockerfile")
}
ref, err := res.SingleRef()
if err != nil {
return nil, err
}
config, err := json.Marshal(img)
if err != nil {
return nil, errors.Wrapf(err, "failed to marshal image config")
}
k := platforms.Format(platforms.DefaultSpec())
res.AddMeta(fmt.Sprintf("%s/%s", exptypes.ExporterImageConfigKey, k), config)
res.SetRef(ref)
return res, nil
}
func GetGockerfileConfig(ctx context.Context, c client.Client) (*config.Config, error) {
opts := c.BuildOpts().Opts
var localName string
filename := opts[keyFilename]
if filename == "" {
filename = defaultGockerfileName
localName = LocalNameGockerfile
} else {
localName = LocalNameDockerfile
}
name := "load Gockerfile"
if filename != "Gockerfile" {
name += " from " + filename
}
src := llb.Local(localName,
llb.IncludePatterns([]string{filename}),
llb.SessionID(c.BuildOpts().SessionID),
llb.SharedKeyHint(defaultGockerfileName),
llb.WithCustomName("[internal] "+name),
)
def, err := src.Marshal()
if err != nil {
return nil, errors.Wrapf(err, "failed to marshal local source")
}
var dtGockerfile []byte
res, err := c.Solve(ctx, client.SolveRequest{
Definition: def.ToPB(),
})
if err != nil {
return nil, errors.Wrapf(err, "failed to resolve Gockerfile")
}
ref, err := res.SingleRef()
if err != nil {
return nil, err
}
dtGockerfile, err = ref.ReadFile(ctx, client.ReadRequest{
Filename: filename,
})
if err != nil {
return nil, errors.Wrapf(err, "failed to read Gockerfile")
}
cfg, err := config.NewConfigFromBytes(dtGockerfile)
if err != nil {
return nil, errors.Wrap(err, "getting config")
}
return cfg, nil
}