-
Notifications
You must be signed in to change notification settings - Fork 9
/
buildkit.go
167 lines (132 loc) · 4.57 KB
/
buildkit.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
// 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 golang
import (
"context"
"fmt"
"strings"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/util/system"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"namespacelabs.dev/foundation/internal/artifacts/oci"
"namespacelabs.dev/foundation/internal/build"
"namespacelabs.dev/foundation/internal/build/baseimage"
"namespacelabs.dev/foundation/internal/build/buildkit"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/dependencies/pins"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/git"
"namespacelabs.dev/foundation/internal/llbutil"
"namespacelabs.dev/foundation/internal/sdk/golang"
"namespacelabs.dev/foundation/std/pkggraph"
)
var (
useSeparateGoModPhase = false
)
func buildUsingBuildkit(ctx context.Context, env pkggraph.SealedContext, bin GoBinary, conf build.Configuration) (compute.Computable[oci.Image], error) {
local := buildkit.LocalContents{
Module: conf.Workspace(),
Path: bin.GoWorkspacePath,
}
src := buildkit.MakeLocalState(local)
if conf.TargetPlatform() == nil {
return nil, fnerrors.InternalError("go: target platform is missing")
}
version := golang.MatchLatestVersion(bin.GoVersion)
base := makeGoBuildBase(ctx, version, *conf.TargetPlatform())
var prodBase llb.State
if !bin.BinaryOnly {
prodBase0, err := baseProdImage(ctx, env, *conf.TargetPlatform())
if err != nil {
return nil, err
}
prodBase, err = baseimage.State(ctx, prodBase0)
if err != nil {
return nil, err
}
} else {
prodBase = llb.Scratch()
}
label := "building"
if bin.PackageName != "" {
label += fmt.Sprintf(" %s", bin.PackageName)
}
goBuild := []string{"build"}
goBuild = append(goBuild, quoteArgs(goBuildArgs(version, bin.StripBinary))...)
goBuild = append(goBuild, fmt.Sprintf("-o=/out/%s", bin.BinaryName))
relPath, err := makePkg(bin.GoWorkspacePath, bin.SourcePath)
if err != nil {
return nil, err
}
goBuild = append(goBuild, relPath)
state := (llbutil.RunGo{
Base: prepareGoMod(base, src, conf.TargetPlatform()).Root(),
SrcMount: src,
WorkingDir: ".",
Platform: conf.TargetPlatform(),
}).With(
llbutil.PrefixSh(label, conf.TargetPlatform(), "go "+strings.Join(goBuild, " "))...).
AddMount("/out", prodBase)
return buildkit.BuildImage(ctx, buildkit.DeferClient(env.Configuration(), conf.TargetPlatform()), conf, state, local)
}
func quoteArgs(m map[string]string) []string {
keys := maps.Keys(m)
slices.Sort(keys)
var args []string
for _, k := range keys {
if v := m[k]; v != "" {
args = append(args, fmt.Sprintf("%s=%q", k, v))
} else {
args = append(args, k)
}
}
return args
}
func prepareGoMod(base, src llb.State, platform *specs.Platform) llb.ExecState {
r := llbutil.RunGo{
Base: base,
SrcMount: src,
WorkingDir: ".",
Platform: platform,
}
ro := llbutil.PrefixSh("updating deps", platform, "go mod download")
if git.AssumeSSHAuth {
ro = append(ro, llb.AddSSHSocket(llb.SSHID(buildkit.SSHAgentProviderID), llb.SSHSocketTarget("/root/ssh-agent.sock")))
ro = append(ro, llb.AddEnv("SSH_AUTH_SOCK", "/root/ssh-agent.sock"))
}
if !useSeparateGoModPhase {
return r.With(ro...)
}
return r.PrepareGoMod(ro...)
}
func makeGoImage(version string) string {
return fmt.Sprintf("docker.io/library/golang:%s", version)
}
func goBase(ctx context.Context, version string, platform specs.Platform) llb.State {
img := makeGoImage(version)
if r, err := pins.CheckImage(img); err == nil {
return llbutil.Image(r, platform)
}
fmt.Fprintf(console.Warnings(ctx), "go: no pinned version of %q\n", img)
return llbutil.Image(img, platform)
}
func makeGoBuildBase(ctx context.Context, version string, platform specs.Platform) llb.State {
st := goBase(ctx, version, platform).
AddEnv("CGO_ENABLED", "0").
AddEnv("PATH", "/usr/local/go/bin:"+system.DefaultPathEnvUnix).
AddEnv("GOPATH", "/go")
for _, ent := range git.NoPromptEnv() {
st = st.AddEnv(ent[0], ent[1])
}
// Don't block builds on checking the pubkey of the target ssh host.
// XXX security
st = st.AddEnv("GIT_SSH_COMMAND", "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no")
if llbutil.GitCredentialsBuildkitSecret != "" {
st = st.Run(llb.Shlex("git config --global credential.helper store")).Root()
}
return st
}