-
Notifications
You must be signed in to change notification settings - Fork 9
/
computable.go
99 lines (81 loc) · 2.75 KB
/
computable.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package buildkit
import (
"context"
"crypto/sha256"
"fmt"
"path/filepath"
"sort"
"strings"
"github.com/moby/buildkit/client/llb"
"namespacelabs.dev/foundation/internal/artifacts/oci"
"namespacelabs.dev/foundation/internal/build"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/environment"
"namespacelabs.dev/foundation/schema"
)
const (
maxExpectedWorkspaceSize uint64 = 32 * 1024 * 1024 // 32MB should be enough for everyone (famous last words).
)
var SkipExpectedMaxWorkspaceSizeCheck = false
// XXX make this a flag instead. The assumption here is that in CI the filesystem is readonly.
var PreDigestLocalInputs = environment.IsRunningInCI()
type LocalContents struct {
Module build.Workspace
Path string
// Added to the base exclude patterns.
ExcludePatterns []string
}
func (l LocalContents) Abs() string {
return filepath.Join(l.Module.Abs(), l.Path)
}
func precomputedReq(req *FrontendRequest, target build.BuildTarget) compute.Computable[*FrontendRequest] {
return compute.Precomputed(req, func(ctx context.Context, req *FrontendRequest) (schema.Digest, error) {
return digestRequest(ctx, req, target)
})
}
func digestRequest(ctx context.Context, req *FrontendRequest, target build.BuildTarget) (schema.Digest, error) {
var kvs []keyValue
for k, v := range req.FrontendInputs {
def, err := MarshalForTarget(ctx, v, target)
if err != nil {
return schema.Digest{}, err
}
kvs = append(kvs, keyValue{k, def})
}
// Make order stable.
sort.Slice(kvs, func(i, j int) bool {
return strings.Compare(kvs[i].Name, kvs[j].Name) < 0
})
w := sha256.New()
for _, kv := range kvs {
if _, err := fmt.Fprintf(w, "%s:", kv.Name); err != nil {
return schema.Digest{}, err
}
if err := llb.WriteTo(kv.Value, w); err != nil {
return schema.Digest{}, err
}
}
if req.Def != nil {
if err := llb.WriteTo(req.Def, w); err != nil {
return schema.Digest{}, err
}
}
return schema.FromHash("sha256", w), nil
}
type ClientFactory interface {
MakeClient(context.Context) (*GatewayClient, error)
}
func MakeImage(makeClient ClientFactory, conf build.BuildTarget, req compute.Computable[*FrontendRequest], localDirs []LocalContents, targetName compute.Computable[oci.RepositoryWithParent]) compute.Computable[oci.Image] {
base := &baseRequest[oci.Image]{
sourceLabel: conf.SourceLabel(),
sourcePackage: conf.SourcePackage(),
makeClient: makeClient,
targetPlatform: conf.TargetPlatform(),
req: req,
localDirs: localDirs,
}
return &reqToImage{baseRequest: base, targetName: targetName}
}