-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilder.go
383 lines (323 loc) · 10.6 KB
/
builder.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Package builder implements a build service
package builder
import (
"bytes"
"context"
"crypto/sha1" //nolint:gosec
"errors"
"fmt"
"io"
"maps"
"os"
"regexp"
"slices"
"strings"
"sync"
"github.com/grafana/k6build"
"github.com/grafana/k6build/pkg/catalog"
"github.com/grafana/k6build/pkg/store"
"github.com/grafana/k6foundry"
"github.com/prometheus/client_golang/prometheus"
)
const (
k6DependencyName = "k6"
k6Path = "go.k6.io/k6"
opRe = `(?<operator>[=|~|>|<|\^|>=|<=|!=]){0,1}(?:\s*)`
verRe = `(?P<version>[v|V](?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*))`
buildRe = `(?:[+|-|])(?P<build>(?:[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))`
)
var (
ErrAccessingArtifact = errors.New("accessing artifact") //nolint:revive
ErrBuildingArtifact = errors.New("building artifact")
ErrInitializingBuilder = errors.New("initializing builder")
ErrInvalidParameters = errors.New("invalid build parameters")
ErrBuildSemverNotAllowed = errors.New("semvers with build metadata not allowed")
constrainRe = regexp.MustCompile(opRe + verRe + buildRe)
)
// GoOpts defines the options for the go build environment
type GoOpts = k6foundry.GoOpts
// FoundryFactory is a function that creates a FoundryFactory
type FoundryFactory interface {
NewFoundry(ctx context.Context, opts k6foundry.NativeFoundryOpts) (k6foundry.Foundry, error)
}
// FoundryFactoryFunction defines a function that implements the FoundryFactory interface
type FoundryFactoryFunction func(context.Context, k6foundry.NativeFoundryOpts) (k6foundry.Foundry, error)
// NewFoundry implements the Foundry interface
func (f FoundryFactoryFunction) NewFoundry(
ctx context.Context,
opts k6foundry.NativeFoundryOpts,
) (k6foundry.Foundry, error) {
return f(ctx, opts)
}
// Opts defines the options for configuring the builder
type Opts struct {
// Allow semvers with build metadata
AllowBuildSemvers bool
// Generate build output
Verbose bool
// Build environment options
GoOpts
}
// Config defines the configuration for a Builder
type Config struct {
Opts Opts
Catalog string
Store store.ObjectStore
Foundry FoundryFactory
Registerer prometheus.Registerer
}
// Builder implements the BuildService interface
type Builder struct {
opts Opts
catalog string
store store.ObjectStore
mutexes sync.Map
foundry FoundryFactory
metrics *metrics
}
// New returns a new instance of Builder given a BuilderConfig
func New(_ context.Context, config Config) (*Builder, error) {
if config.Catalog == "" {
return nil, k6build.NewWrappedError(ErrInitializingBuilder, errors.New("catalog cannot be nil"))
}
if config.Store == nil {
return nil, k6build.NewWrappedError(ErrInitializingBuilder, errors.New("store cannot be nil"))
}
foundry := config.Foundry
if foundry == nil {
foundry = FoundryFactoryFunction(k6foundry.NewNativeFoundry)
}
metrics := newMetrics()
if config.Registerer != nil {
err := metrics.register(config.Registerer)
if err != nil {
return nil, k6build.NewWrappedError(ErrInitializingBuilder, err)
}
}
return &Builder{
catalog: config.Catalog,
opts: config.Opts,
store: config.Store,
foundry: foundry,
metrics: metrics,
}, nil
}
// Build builds a custom k6 binary with dependencies
func (b *Builder) Build( //nolint:funlen
ctx context.Context,
platform string,
k6Constrains string,
deps []k6build.Dependency,
) (artifact k6build.Artifact, buildErr error) {
b.metrics.requestCounter.Inc()
requestTimer := prometheus.NewTimer(b.metrics.requestTimeHistogram)
defer func() {
if buildErr == nil {
requestTimer.ObserveDuration()
}
// FIXME: this is a temporary solution because the logic has many paths that return
// an invalid parameters error and we need to increment the metrics in all of them
if errors.Is(buildErr, ErrInvalidParameters) {
b.metrics.buildsInvalidCounter.Inc()
}
}()
// check if the platform is valid early to avoid unnecessary work
_, err := k6foundry.ParsePlatform(platform)
if err != nil {
return k6build.Artifact{}, k6build.NewWrappedError(ErrInvalidParameters, err)
}
k6Mod, resolved, err := b.resolveDependencies(ctx, k6Constrains, deps)
if err != nil {
return k6build.Artifact{}, k6build.NewWrappedError(ErrInvalidParameters, err)
}
id := generateArtifactID(platform, k6Mod, resolved)
unlock := b.lockArtifact(id)
defer unlock()
artifactObject, err := b.store.Get(ctx, id)
if err == nil {
b.metrics.storeHitsCounter.Inc()
return k6build.Artifact{
ID: id,
Checksum: artifactObject.Checksum,
URL: artifactObject.URL,
Dependencies: resolvedVersions(k6Mod, resolved),
Platform: platform,
}, nil
}
if !errors.Is(err, store.ErrObjectNotFound) {
return k6build.Artifact{}, k6build.NewWrappedError(ErrAccessingArtifact, err)
}
b.metrics.buildCounter.Inc()
buildTimer := prometheus.NewTimer(b.metrics.buildTimeHistogram)
artifactBuffer := &bytes.Buffer{}
err = b.buildArtifact(ctx, platform, k6Mod.Version, resolved, artifactBuffer)
if err != nil {
return k6build.Artifact{}, k6build.NewWrappedError(ErrBuildingArtifact, err)
}
buildTimer.ObserveDuration()
artifactObject, err = b.store.Put(ctx, id, artifactBuffer)
// if there was a conflict creating the object, get returns the object
if errors.Is(err, store.ErrDuplicateObject) || (err != nil && strings.Contains(err.Error(), "duplicate object")) {
artifactObject, err = b.store.Get(ctx, id)
}
if err != nil {
return k6build.Artifact{}, k6build.NewWrappedError(ErrAccessingArtifact, err)
}
return k6build.Artifact{
ID: id,
Checksum: artifactObject.Checksum,
URL: artifactObject.URL,
Dependencies: resolvedVersions(k6Mod, resolved),
Platform: platform,
}, nil
}
func (b *Builder) resolveDependencies(
ctx context.Context,
k6Constrains string,
deps []k6build.Dependency,
) (catalog.Module, map[string]catalog.Module, error) {
ctlg, err := catalog.NewCatalog(ctx, b.catalog)
if err != nil {
return catalog.Module{}, nil, err
}
resolved := map[string]catalog.Module{}
// check if it is a semver of the form v0.0.0+<build>
// if it is, we don't check with the catalog, but instead we use
// the build metadata as version when building this module
var k6Mod catalog.Module
buildMetadata, err := hasBuildMetadata(k6Constrains)
if err != nil {
return catalog.Module{}, nil, err
}
if buildMetadata != "" {
if !b.opts.AllowBuildSemvers {
return catalog.Module{}, nil, ErrBuildSemverNotAllowed
}
// use a semantic version for the build metadata
k6Mod = catalog.Module{Path: k6Path, Version: "v0.0.0+" + buildMetadata}
} else {
k6Mod, err = ctlg.Resolve(ctx, catalog.Dependency{Name: k6DependencyName, Constrains: k6Constrains})
if err != nil {
return catalog.Module{}, nil, err
}
}
for _, d := range deps {
m, err := ctlg.Resolve(ctx, catalog.Dependency{Name: d.Name, Constrains: d.Constraints})
if err != nil {
return catalog.Module{}, nil, err
}
resolved[d.Name] = m
}
return k6Mod, resolved, nil
}
// lockArtifact obtains a mutex used to prevent concurrent builds of the same artifact and
// returns a function that will unlock the mutex associated to the given id in the object store.
// The lock is also removed from the map. Subsequent calls will get another lock on the same
// id but this is safe as the object should already be in the object store and no further
// builds are needed.
func (b *Builder) lockArtifact(id string) func() {
value, _ := b.mutexes.LoadOrStore(id, &sync.Mutex{})
mtx, _ := value.(*sync.Mutex)
mtx.Lock()
return func() {
b.mutexes.Delete(id)
mtx.Unlock()
}
}
// hasBuildMetadata checks if the constrain references a version with a build metadata.
// and if so, checks if the version is valid. Only v0.0.0 is allowed.
// E.g. v0.0.0+effa45f
func hasBuildMetadata(constrain string) (string, error) {
opInx := constrainRe.SubexpIndex("operator")
verIdx := constrainRe.SubexpIndex("version")
preIdx := constrainRe.SubexpIndex("build")
matches := constrainRe.FindStringSubmatch(constrain)
if matches == nil {
return "", nil
}
op := matches[opInx]
ver := matches[verIdx]
build := matches[preIdx]
if op != "" && op != "=" {
return "", k6build.NewWrappedError(
ErrInvalidParameters,
fmt.Errorf("only exact match is allowed for versions with build metadata"),
)
}
if ver != "v0.0.0" {
return "", k6build.NewWrappedError(
ErrInvalidParameters,
fmt.Errorf("version with build metadata must start with v0.0.0"),
)
}
return build, nil
}
// generateArtifactID generates a unique identifier for a build
func generateArtifactID(platform string, k6Mod catalog.Module, deps map[string]catalog.Module) string {
hashData := bytes.Buffer{}
hashData.WriteString(platform)
hashData.WriteString(fmt.Sprintf(":%s%s", k6DependencyName, k6Mod.Version))
for _, d := range slices.Sorted(maps.Keys(deps)) {
hashData.WriteString(fmt.Sprintf(":%s%s", d, deps[d].Version))
}
return fmt.Sprintf("%x", sha1.Sum(hashData.Bytes())) //nolint:gosec
}
func resolvedVersions(k6Dep catalog.Module, deps map[string]catalog.Module) map[string]string {
versions := map[string]string{}
versions[k6DependencyName] = k6Dep.Version
for d, m := range deps {
versions[d] = m.Version
}
return versions
}
func (b *Builder) buildArtifact(
ctx context.Context,
platform string,
k6Version string,
deps map[string]catalog.Module,
artifactBuffer io.Writer,
) error {
// already checked the platform is valid, should be safe to ignore the error
buildPlatform, _ := k6foundry.ParsePlatform(platform)
mods := []k6foundry.Module{}
cgoEnabled := false
for _, m := range deps {
mods = append(mods, k6foundry.Module{Path: m.Path, Version: m.Version})
cgoEnabled = cgoEnabled || m.Cgo
}
// set CGO_ENABLED if any of the dependencies require it
env := b.opts.Env
if cgoEnabled {
if env == nil {
env = map[string]string{}
}
env["CGO_ENABLED"] = "1"
}
builderOpts := k6foundry.NativeFoundryOpts{
GoOpts: k6foundry.GoOpts{
Env: env,
CopyGoEnv: b.opts.CopyGoEnv,
},
}
if b.opts.Verbose {
builderOpts.Stdout = os.Stdout
builderOpts.Stderr = os.Stderr
}
builder, err := b.foundry.NewFoundry(ctx, builderOpts)
if err != nil {
return k6build.NewWrappedError(ErrInitializingBuilder, err)
}
// if the version is a build version, we need the build metadata and ignore the version
// as go does not accept semvers with build metadata
_, build, found := strings.Cut(k6Version, "+")
if found {
k6Version = build
}
_, err = builder.Build(ctx, buildPlatform, k6Version, mods, nil, []string{}, artifactBuffer)
if err != nil {
b.metrics.buildsFailedCounter.Inc()
return k6build.NewWrappedError(ErrAccessingArtifact, err)
}
// TODO: complete artifact info
return nil
}