-
Notifications
You must be signed in to change notification settings - Fork 13
/
handle_container.go
136 lines (109 loc) 路 3.38 KB
/
handle_container.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
package windows
import (
"context"
"fmt"
"runtime"
"github.com/Azure/dalec"
"github.com/Azure/dalec/frontend"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/frontend/dockerui"
gwclient "github.com/moby/buildkit/frontend/gateway/client"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
)
const (
defaultBaseImage = "mcr.microsoft.com/windows/nanoserver:1809"
windowsSystemDir = "/Windows/System32/"
)
var (
defaultPlatform = ocispecs.Platform{
OS: outputKey,
Architecture: runtime.GOARCH,
}
)
func handleContainer(ctx context.Context, client gwclient.Client) (*gwclient.Result, error) {
return frontend.BuildWithPlatform(ctx, client, func(ctx context.Context, client gwclient.Client, platform *ocispecs.Platform, spec *dalec.Spec, targetKey string) (gwclient.Reference, *dalec.DockerImageSpec, error) {
sOpt, err := frontend.SourceOptFromClient(ctx, client)
if err != nil {
return nil, nil, err
}
if err := validateRuntimeDeps(spec, targetKey); err != nil {
return nil, nil, fmt.Errorf("error validating windows spec: %w", err)
}
bc, err := dockerui.NewClient(client)
if err != nil {
return nil, nil, err
}
targetPlatform, err := getTargetPlatform(bc)
if err != nil {
return nil, nil, err
}
pg := dalec.ProgressGroup("Build windows container: " + spec.Name)
worker := workerImg(sOpt, pg)
bin, err := buildBinaries(spec, worker, sOpt, targetKey)
if err != nil {
return nil, nil, fmt.Errorf("unable to build binary %w", err)
}
if signer, ok := spec.GetSigner(targetKey); ok {
signed, err := frontend.ForwardToSigner(ctx, client, platform, signer, bin)
if err != nil {
return nil, nil, err
}
bin = signed
}
baseImgName := getBaseOutputImage(spec, targetKey, defaultBaseImage)
baseImage := llb.Image(baseImgName, llb.Platform(targetPlatform))
out := baseImage.
File(llb.Copy(bin, "/", windowsSystemDir)).
With(copySymlinks(spec.GetSymlinks(targetKey)))
def, err := out.Marshal(ctx)
if err != nil {
return nil, nil, err
}
res, err := client.Solve(ctx, gwclient.SolveRequest{
Definition: def.ToPB(),
})
if err != nil {
return nil, nil, err
}
base := frontend.GetBaseOutputImage(spec, targetKey, defaultBaseImage)
img, err := frontend.BuildImageConfig(ctx, client, spec, targetKey, base, frontend.WithPlatform(targetPlatform))
if err != nil {
return nil, nil, err
}
ref, err := res.SingleRef()
return ref, img, err
})
}
func copySymlinks(lm map[string]dalec.SymlinkTarget) llb.StateOption {
return func(s llb.State) llb.State {
if len(lm) == 0 {
return s
}
keys := dalec.SortMapKeys(lm)
for _, srcPath := range keys {
l := lm[srcPath]
dstPath := l.Path
s = s.File(llb.Copy(s, srcPath, dstPath))
}
return s
}
}
func getTargetPlatform(bc *dockerui.Client) (ocispecs.Platform, error) {
platform := defaultPlatform
switch len(bc.TargetPlatforms) {
case 0:
case 1:
platform = bc.TargetPlatforms[0]
default:
return ocispecs.Platform{},
fmt.Errorf("multiple target supplied for build: %v. note: only amd64 is supported for windows outputs", bc.TargetPlatforms)
}
return platform, nil
}
func getBaseOutputImage(spec *dalec.Spec, target, defaultBase string) string {
baseRef := defaultBase
if spec.Targets[target].Image != nil && spec.Targets[target].Image.Base != "" {
baseRef = spec.Targets[target].Image.Base
}
return baseRef
}