-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
218 lines (180 loc) · 5.28 KB
/
helpers.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package dalec
import (
"encoding/json"
"path"
"sort"
"sync/atomic"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/identity"
)
var disableDiffMerge atomic.Bool
// DisableDiffMerge allows disabling the use of [llb.Diff] and [llb.Merge] in favor of [llb.Copy].
// This is needed when the buildkit version does not support [llb.Diff] and [llb.Merge].
//
// Mainly this would be to allow dockerd with the (current)
// standard setup of dockerd which uses "graphdrivers" to work as these ops are not
// supported by the graphdriver backend.
// When this is false and the graphdriver backend is used, the build will fail when buildkit
// checks the capabilities of the backend.
func DisableDiffMerge(v bool) {
disableDiffMerge.Store(v)
}
type copyOptionFunc func(*llb.CopyInfo)
func (f copyOptionFunc) SetCopyOption(i *llb.CopyInfo) {
f(i)
}
func WithIncludes(patterns []string) llb.CopyOption {
return copyOptionFunc(func(i *llb.CopyInfo) {
i.IncludePatterns = patterns
})
}
func WithExcludes(patterns []string) llb.CopyOption {
return copyOptionFunc(func(i *llb.CopyInfo) {
i.ExcludePatterns = patterns
})
}
func WithDirContentsOnly() llb.CopyOption {
return copyOptionFunc(func(i *llb.CopyInfo) {
i.CopyDirContentsOnly = true
})
}
type constraintsOptFunc func(*llb.Constraints)
func (f constraintsOptFunc) SetConstraintsOption(c *llb.Constraints) {
f(c)
}
func (f constraintsOptFunc) SetRunOption(ei *llb.ExecInfo) {
f(&ei.Constraints)
}
func (f constraintsOptFunc) SetLocalOption(li *llb.LocalInfo) {
f(&li.Constraints)
}
func (f constraintsOptFunc) SetOCILayoutOption(oi *llb.OCILayoutInfo) {
f(&oi.Constraints)
}
func (f constraintsOptFunc) SetHTTPOption(hi *llb.HTTPInfo) {
f(&hi.Constraints)
}
func (f constraintsOptFunc) SetImageOption(ii *llb.ImageInfo) {
f(&ii.Constraints)
}
func (f constraintsOptFunc) SetGitOption(gi *llb.GitInfo) {
f(&gi.Constraints)
}
func WithConstraints(ls ...llb.ConstraintsOpt) llb.ConstraintsOpt {
return constraintsOptFunc(func(c *llb.Constraints) {
for _, opt := range ls {
opt.SetConstraintsOption(c)
}
})
}
func withConstraints(opts []llb.ConstraintsOpt) llb.ConstraintsOpt {
return WithConstraints(opts...)
}
// SortMapKeys is a convenience generic function to sort the keys of a map[string]T
func SortMapKeys[T any](m map[string]T) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func DuplicateMap[K comparable, V any](m map[K]V) map[K]V {
newM := make(map[K]V, len(m))
for k, v := range m {
newM[k] = v
}
return newM
}
// MergeAtPath merges the given states into the given destination path in the given input state.
func MergeAtPath(input llb.State, states []llb.State, dest string) llb.State {
if disableDiffMerge.Load() {
output := input
for _, st := range states {
output = output.
File(llb.Copy(st, "/", dest, WithCreateDestPath()))
}
return output
}
diffs := make([]llb.State, 0, len(states)+1)
diffs = append(diffs, input)
for _, src := range states {
st := src
if dest != "" && dest != "/" {
st = llb.Scratch().
File(llb.Copy(src, "/", dest, WithCreateDestPath()))
}
diffs = append(diffs, llb.Diff(input, st))
}
return llb.Merge(diffs)
}
type localOptionFunc func(*llb.LocalInfo)
func (f localOptionFunc) SetLocalOption(li *llb.LocalInfo) {
f(li)
}
func localIncludeExcludeMerge(src *Source) localOptionFunc {
return func(li *llb.LocalInfo) {
if len(src.Excludes) > 0 {
excludes := src.Excludes
if li.ExcludePatterns != "" {
var ls []string
if err := json.Unmarshal([]byte(li.ExcludePatterns), &ls); err != nil {
panic(err)
}
excludes = append(excludes, ls...)
}
llb.ExcludePatterns(excludes).SetLocalOption(li)
}
if len(src.Includes) > 0 {
includes := src.Includes
if li.IncludePatterns != "" {
var ls []string
if err := json.Unmarshal([]byte(li.IncludePatterns), &ls); err != nil {
panic(err)
}
includes = append(includes, ls...)
}
llb.IncludePatterns(includes).SetLocalOption(li)
}
}
}
// CacheDirsToRunOpt converts the given cache directories into a RunOption.
func CacheDirsToRunOpt(mounts map[string]CacheDirConfig, distroKey, archKey string) llb.RunOption {
var opts []llb.RunOption
for p, cfg := range mounts {
mode, err := sharingMode(cfg.Mode)
if err != nil {
panic(err)
}
key := cfg.Key
if cfg.IncludeDistroKey {
key = path.Join(distroKey, key)
}
if cfg.IncludeArchKey {
key = path.Join(archKey, key)
}
opts = append(opts, llb.AddMount(p, llb.Scratch(), llb.AsPersistentCacheDir(key, mode)))
}
return runOptFunc(func(ei *llb.ExecInfo) {
for _, opt := range opts {
opt.SetRunOption(ei)
}
})
}
type runOptFunc func(*llb.ExecInfo)
func (f runOptFunc) SetRunOption(ei *llb.ExecInfo) {
f(ei)
}
// ProgressGroup creates a progress group with the given name.
// If a progress group is already set in the constraints the id is reused.
// If no progress group is set a new id is generated.
func ProgressGroup(name string) llb.ConstraintsOpt {
return constraintsOptFunc(func(c *llb.Constraints) {
if c.Metadata.ProgressGroup != nil {
id := c.Metadata.ProgressGroup.Id
llb.ProgressGroup(id, name, false).SetConstraintsOption(c)
return
}
llb.ProgressGroup(identity.NewID(), name, false).SetConstraintsOption(c)
})
}