-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathpreprocess.go
More file actions
306 lines (254 loc) · 9.41 KB
/
preprocess.go
File metadata and controls
306 lines (254 loc) · 9.41 KB
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
package dalec
import (
_ "embed"
"fmt"
"path/filepath"
"strings"
"github.com/moby/buildkit/client/llb"
"github.com/pkg/errors"
)
//go:embed scripts/gomod-patch.sh
var gomodPatchScript string
const (
// Gomod preprocessing constants
gomodPatchSourcePrefix = "__gomod_patch_"
gomodPatchFilename = "gomod.patch"
gomodFilename = "go.mod"
gosumFilename = "go.sum"
defaultGitUsername = "git"
)
// Preprocess performs preprocessing on the spec after loading.
// This includes generating patches for gomod edits and potentially other
// generator-based transformations in the future.
//
// Preprocessing generates LLB states for patches and registers them as context sources
// that can be retrieved later when sources are fetched.
func (s *Spec) Preprocess(sOpt SourceOpts, worker llb.State, opts ...llb.ConstraintsOpt) error {
if err := s.preprocessGomodEdits(sOpt, worker, opts...); err != nil {
return errors.Wrap(err, "failed to preprocess gomod edits")
}
return nil
}
// preprocessGomodEdits generates patch LLB states for all gomod replace directives
// and registers them as context sources that can be retrieved later.
func (s *Spec) preprocessGomodEdits(sOpt SourceOpts, worker llb.State, opts ...llb.ConstraintsOpt) error {
gomodSources := s.gomodSources()
if len(gomodSources) == 0 {
return nil
}
// Get sources with base patches applied
baseSources := s.getPatchedSources(sOpt, worker, func(name string) bool {
_, ok := gomodSources[name]
return ok
}, opts...)
credHelper, err := sOpt.GitCredHelperOpt()
if err != nil {
return errors.Wrap(err, "failed to get git credential helper")
}
// Generate patch states for each source with gomod generators
for sourceName, src := range gomodSources {
baseState, ok := baseSources[sourceName]
if !ok {
continue
}
for _, gen := range src.Generate {
if gen == nil || gen.Gomod == nil {
continue
}
// Generate patch state (LLB state, not solved bytes)
patchSt, err := s.generateGomodPatchStateForSource(sourceName, gen, baseState, worker, credHelper, opts...)
if err != nil {
return errors.Wrapf(err, "failed to generate gomod patch state for source %s", sourceName)
}
if patchSt == nil {
// No changes needed
continue
}
// Create internal LLB source with the patch state
patchSourceName := fmt.Sprintf(gomodPatchSourcePrefix+"%s", sourceName)
s.Sources[patchSourceName] = Source{
LLB: newSourceLLB(*patchSt),
}
// Inject patch reference into spec.Patches
// Don't set Path - the patch file is the entire source (single file)
if s.Patches == nil {
s.Patches = make(map[string][]PatchSpec)
}
strip := 1
s.Patches[sourceName] = append(s.Patches[sourceName], PatchSpec{
Source: patchSourceName,
// Path is empty - the entire source is the patch file
Strip: &strip,
})
}
}
return nil
}
// gomodEditArgs generates the list of -replace= arguments for go mod edit.
// Returns a newline-separated list of arguments that can be safely passed to go mod edit.
func gomodEditArgs(g *GeneratorGomod) (string, error) {
if g == nil || g.Edits == nil {
return "", nil
}
var args []string
// Process replace directives
for _, r := range g.Edits.Replace {
arg, err := r.goModEditArg()
if err != nil {
return "", err
}
args = append(args, "-replace="+arg)
}
if len(args) == 0 {
return "", nil
}
// Return as newline-separated list for safe parsing in shell
return strings.Join(args, "\n"), nil
}
// buildGomodPatchEnv generates environment variables for the gomod patch script
func buildGomodPatchEnv(editArgs string, paths []string, gen *SourceGenerator, sourceName string, patchOutputDir string, origWorkDir string) (map[string]string, error) {
const (
workDir = "/work/src"
)
patchPath := filepath.Join(patchOutputDir, gomodPatchFilename)
joinedWorkDir := filepath.Join(workDir, sourceName, gen.Subpath)
// Build git config section
gitConfig := &strings.Builder{}
var goPrivate, goInsecure string
sortedHosts := SortMapKeys(gen.Gomod.Auth)
if len(sortedHosts) > 0 {
goPrivateHosts := make([]string, 0, len(sortedHosts))
for _, host := range sortedHosts {
auth := gen.Gomod.Auth[host]
gpHost, _, _ := strings.Cut(host, ":")
goPrivateHosts = append(goPrivateHosts, gpHost)
if sshConfig := auth.SSH; sshConfig != nil {
username := defaultGitUsername
if sshConfig.Username != "" {
username = sshConfig.Username
}
fmt.Fprintf(gitConfig, "git config --global url.\"ssh://%[1]s@%[2]s/\".insteadOf https://%[3]s/\n", username, host, gpHost)
continue
}
var kind string
switch {
case auth.Token != "":
kind = "token"
case auth.Header != "":
kind = "header"
default:
kind = ""
}
if kind != "" {
fmt.Fprintf(gitConfig, "git config --global credential.\"https://%[1]s.helper\" \"/usr/local/bin/frontend credential-helper --kind=%[2]s\"\n", host, kind)
}
}
joined := strings.Join(goPrivateHosts, ",")
goPrivate = joined
goInsecure = joined
}
// Build module info for each path
// Format: rel_module_path|gomod_path|gosum_path|module_dir|rel_gomod_path|rel_gosum_path|orig_module_dir
joinedOrigWorkDir := filepath.Join(origWorkDir, sourceName, gen.Subpath)
var modulePaths []string
for _, relPath := range paths {
moduleDir := filepath.Clean(filepath.Join(joinedWorkDir, relPath))
origModuleDir := filepath.Clean(filepath.Join(joinedOrigWorkDir, relPath))
relModulePath := filepath.Clean(filepath.Join(gen.Subpath, relPath))
if relModulePath == "." {
relModulePath = ""
}
relGoModPath := filepath.ToSlash(filepath.Join(relModulePath, gomodFilename))
relGoSumPath := filepath.ToSlash(filepath.Join(relModulePath, gosumFilename))
goModPath := filepath.Join(moduleDir, gomodFilename)
goSumPath := filepath.Join(moduleDir, gosumFilename)
moduleInfo := fmt.Sprintf("%s|%s|%s|%s|%s|%s|%s",
relModulePath, goModPath, goSumPath, moduleDir, relGoModPath, relGoSumPath, origModuleDir)
modulePaths = append(modulePaths, moduleInfo)
}
env := map[string]string{
"PATCH_PATH": patchPath,
"EDIT_ARGS": editArgs,
"GOMOD_FILENAME": gomodFilename,
"GOSUM_FILENAME": gosumFilename,
"MODULE_PATHS": strings.Join(modulePaths, ":"),
}
if gitConfig.Len() > 0 {
env["GIT_CONFIG_SCRIPT"] = gitConfig.String()
}
if goPrivate != "" {
env["GOPRIVATE"] = goPrivate
}
if goInsecure != "" {
env["GOINSECURE"] = goInsecure
}
return env, nil
}
// generateGomodPatchStateForSource generates a single merged patch LLB state for all paths
// in a gomod generator by running go mod edit + tidy and capturing the diff.
// Returns the LLB state containing the patch file, or nil if no changes are needed.
func (s *Spec) generateGomodPatchStateForSource(sourceName string, gen *SourceGenerator, baseState llb.State, worker llb.State, credHelper llb.RunOption, opts ...llb.ConstraintsOpt) (*llb.State, error) {
editArgs, err := gomodEditArgs(gen.Gomod)
if err != nil {
return nil, err
}
if editArgs == "" {
return nil, nil
}
paths := gen.Gomod.Paths
if len(paths) == 0 {
paths = []string{"."}
}
const (
workDir = "/work/src"
origWorkDir = "/work/src-orig" // Read-only mount of original state for diffing
proxyPath = "/go/pkg/mod" // Standard Go module cache path
)
// Create a temporary directory for patch generation
patchOutputDir := "/tmp/patch-work"
// Generate environment variables for the script
envVars, err := buildGomodPatchEnv(editArgs, paths, gen, sourceName, patchOutputDir, origWorkDir)
if err != nil {
return nil, err
}
opts = append(opts, ProgressGroup("Generate gomod patch for source: "+sourceName))
// Create a state with the script file
scriptState := llb.Scratch().File(
llb.Mkfile("/gomod-patch.sh", 0755, []byte(gomodPatchScript)),
WithConstraints(opts...),
)
// Create a scratch state to capture the patch output
patchOutput := llb.Scratch()
runOpts := []llb.RunOption{
llb.Args([]string{"/gomod-patch.sh"}),
llb.AddMount("/gomod-patch.sh", scriptState, llb.SourcePath("/gomod-patch.sh")),
llb.AddMount(workDir, baseState),
llb.AddMount(origWorkDir, baseState, llb.Readonly), // Read-only mount for diffing
llb.AddMount(proxyPath, llb.Scratch(), llb.AsPersistentCacheDir(GomodCacheKey, llb.CacheMountShared)),
llb.AddMount(patchOutputDir, patchOutput), // Mount scratch state to capture patch file
llb.AddEnv("GOPATH", "/go"),
llb.AddEnv("TMP_GOMODCACHE", proxyPath),
llb.AddEnv("GIT_SSH_COMMAND", "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"),
WithConstraints(opts...),
}
// Add environment variables from the script
for key, value := range envVars {
runOpts = append(runOpts, llb.AddEnv(key, value))
}
if credHelper != nil {
runOpts = append(runOpts, credHelper)
}
if secretOpt := gen.withGomodSecretsAndSockets(); secretOpt != nil {
runOpts = append(runOpts, secretOpt)
}
// Generate the LLB state that captures the patch output mount
// The AddMount call returns the state of the patchOutput scratch.
// Since we mounted at patchOutputDir and wrote to patchPath,
// the file in the mount will be at gomodPatchFilename (path relative to mount point)
patchMount := worker.Run(runOpts...).AddMount(patchOutputDir, patchOutput)
// Create a scratch state with the patch file at a generic location
// The sourceFilters will handle renaming it to the final source name
patchSt := llb.Scratch().
File(llb.Copy(patchMount, "/"+gomodPatchFilename, "/patch"), WithConstraints(opts...))
return &patchSt, nil
}