/
main.go
362 lines (322 loc) · 10.2 KB
/
main.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
/*
Command line tool for generating a Cloud Build yaml file based on versions.yaml.
*/
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"text/template"
"github.com/GoogleCloudPlatform/click-to-deploy/tools/dockerversioning/versions"
)
type cloudBuildOptions struct {
// Whether to restrict to a particular set of Dockerfile directories.
// If empty, all directories are used.
Directories []string
// Whether to run tests as part of the build.
RunTests bool
// Whether to require that image tags do not already exist in the repo.
RequireNewTags bool
// Whether to push to all declared tags
FirstTagOnly bool
// Optional timeout duration. If not specified, the Cloud Builder default timeout is used.
TimeoutSeconds int
// Optional machine type used to run the build, must be one of: N1_HIGHCPU_8, N1_HIGHCPU_32, E2_HIGHCPU_8, E2_HIGHCPU_32. If not specified, the default machine is used.
MachineType string
// Optional parallel build. If specified, images can be build on bigger machines in parallel.
EnableParallel bool
// Forces parallel build. If specified, images are build on bigger machines in parallel. Overrides EnableParallel.
ForceParallel bool
}
// TODO(huyhg): Replace "gcr.io/$PROJECT_ID/functional_test" with gcp-runtimes one.
const cloudBuildTemplateString = `steps:
{{- $parallel := .Parallel }}
{{- if .RequireNewTags }}
# Check if tags exist.
{{- range .Images }}
- name: gcr.io/gcp-runtimes/check_if_tag_exists
args:
- 'python'
- '/main.py'
- '--image={{ . }}'
{{- end }}
{{- end }}
# Build images
{{- range .ImageBuilds }}
{{- if .Builder }}
- name: gcr.io/cloud-builders/docker
args:
- 'build'
- '--tag={{ .Tag }}'
- '{{ .Directory }}'
{{- if $parallel }}
waitFor: ['-']
id: 'image-{{ .Tag }}'
{{- end }}
{{- else }}
{{- if .BuilderImage }}
- name: {{ .BuilderImage }}
args: {{ .BuilderArgs }}
{{- if $parallel }}
waitFor: ['image-{{ .BuilderImage }}']
id: 'image-{{ .Tag }}'
{{- end }}
{{- else }}
- name: gcr.io/cloud-builders/docker
args:
- 'build'
- '--tag={{ .Tag }}'
- '{{ .Directory }}'
{{- if $parallel }}
waitFor: ['-']
id: 'image-{{ .Tag }}'
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- range $imageIndex, $image := .ImageBuilds }}
{{- $primary := $image.Tag }}
{{- range $testIndex, $test := $image.StructureTests }}
{{- if and (eq $imageIndex 0) (eq $testIndex 0) }}
# Run structure tests
{{- end}}
- name: gcr.io/gcp-runtimes/structure_test
args:
- '--image'
- '{{ $primary }}'
- '--config'
- '{{ $test }}'
{{- end }}
{{- end }}
{{- range $imageIndex, $image := .ImageBuilds }}
{{- $primary := $image.Tag }}
{{- range $testIndex, $test := $image.FunctionalTests }}
{{- if and (eq $imageIndex 0) (eq $testIndex 0) }}
# Run functional tests
{{- end }}
- name: gcr.io/$PROJECT_ID/functional_test
args:
- '--verbose'
- '--vars'
- 'IMAGE={{ $primary }}'
- '--vars'
- 'UNIQUE={{ $imageIndex }}-{{ $testIndex }}'
- '--test_spec'
- '{{ $test }}'
{{- if $parallel }}
waitFor: ['image-{{ $primary }}']
id: 'test-{{ $primary }}-{{ $testIndex }}'
{{- end }}
{{- end }}
{{- end }}
# Add alias tags
{{- range $imageIndex, $image := .ImageBuilds }}
{{- $primary := $image.Tag }}
{{- range .Aliases }}
- name: gcr.io/cloud-builders/docker
args:
- 'tag'
- '{{ $primary }}'
- '{{ . }}'
{{- if $parallel }}
waitFor:
- 'image-{{ $primary }}'
{{- range $testIndex, $test := $image.FunctionalTests }}
- 'test-{{ $primary }}-{{ $testIndex }}'
{{- end }}
{{- end }}
{{- end }}
{{- end }}
images:
{{- range .AllImages }}
- '{{ . }}'
{{- end }}
{{- if not (eq .TimeoutSeconds 0) }}
timeout: {{ .TimeoutSeconds }}s
{{- end }}
{{- if $parallel }}
options:
machineType: 'E2_HIGHCPU_8'
{{- else }}
{{- if .MachineType }}
options:
machineType: '{{ .MachineType }}'
{{- end }}
{{- end }}
`
const testsDir = "tests"
const functionalTestsDir = "tests/functional_tests"
const structureTestsDir = "tests/structure_tests"
const testJsonSuffix = "_test.json"
const testYamlSuffix = "_test.yaml"
const workspacePrefix = "/workspace/"
type imageBuildTemplateData struct {
Directory string
Tag string
Aliases []string
StructureTests []string
FunctionalTests []string
Builder bool
BuilderImage string
BuilderArgs []string
ImageNameFromBuilder string
}
type cloudBuildTemplateData struct {
RequireNewTags bool
Parallel bool
ImageBuilds []imageBuildTemplateData
AllImages []string
TimeoutSeconds int
MachineType string
}
func shouldParallelize(options cloudBuildOptions, numberOfVersions int, numberOfTests int) bool {
if options.ForceParallel {
return true
}
if !options.EnableParallel {
return false
}
return numberOfVersions > 1 || numberOfTests > 1
}
func newCloudBuildTemplateData(
registry string, spec versions.Spec, options cloudBuildOptions) cloudBuildTemplateData {
data := cloudBuildTemplateData{}
data.RequireNewTags = options.RequireNewTags
// Determine the set of directories to operate on.
dirs := make(map[string]bool)
if len(options.Directories) > 0 {
for _, d := range options.Directories {
dirs[d] = true
}
} else {
for _, v := range spec.Versions {
dirs[v.Dir] = true
}
}
// Extract tests to run.
var structureTests []string
var functionalTests []string
if options.RunTests {
// Legacy structure tests reside in the root tests/ directory.
structureTests = append(structureTests, readTests(testsDir)...)
structureTests = append(structureTests, readTests(structureTestsDir)...)
functionalTests = append(functionalTests, readTests(functionalTestsDir)...)
}
// Extract a list of full image names to build.
for _, v := range spec.Versions {
if !dirs[v.Dir] {
continue
}
var images []string
for _, t := range v.Tags {
image := fmt.Sprintf("%v/%v:%v", registry, v.Repo, t)
images = append(images, image)
if options.FirstTagOnly {
break
}
}
// Ignore builder images from images list
if !v.Builder {
data.AllImages = append(data.AllImages, images...)
}
versionSTests, versionFTests := filterTests(structureTests, functionalTests, v)
// Enforce to use ImageNameFromBuilder as reference to create tags
if v.BuilderImage != "" {
BuilderImageFull := fmt.Sprintf("%v/%v", registry, v.BuilderImage)
data.ImageBuilds = append(
data.ImageBuilds, imageBuildTemplateData{v.Dir, v.ImageNameFromBuilder, images, versionSTests, versionFTests, v.Builder, BuilderImageFull, v.BuilderArgs, v.ImageNameFromBuilder})
} else {
data.ImageBuilds = append(
data.ImageBuilds, imageBuildTemplateData{v.Dir, images[0], images[1:], versionSTests, versionFTests, v.Builder, v.BuilderImage, v.BuilderArgs, v.ImageNameFromBuilder})
}
}
data.TimeoutSeconds = options.TimeoutSeconds
data.MachineType = options.MachineType
data.Parallel = shouldParallelize(options, len(spec.Versions), len(functionalTests))
return data
}
func readTests(testsDir string) (tests []string) {
if info, err := os.Stat(testsDir); err == nil && info.IsDir() {
files, err := ioutil.ReadDir(testsDir)
check(err)
for _, f := range files {
if f.IsDir() {
continue
}
if strings.HasSuffix(f.Name(), testJsonSuffix) || strings.HasSuffix(f.Name(), testYamlSuffix) {
tests = append(tests, workspacePrefix+fmt.Sprintf("%s/%s", testsDir, f.Name()))
}
}
}
return
}
func filterTests(structureTests []string, functionalTests []string, version versions.Version) (outStructureTests []string, outFunctionalTests []string) {
included := make(map[string]bool, len(structureTests)+len(functionalTests))
for _, test := range append(structureTests, functionalTests...) {
included[test] = true
}
for _, excluded := range version.ExcludeTests {
if !included[workspacePrefix+excluded] {
log.Fatalf("No such test to exclude: %s", excluded)
}
included[workspacePrefix+excluded] = false
}
outStructureTests = make([]string, 0, len(structureTests))
for _, test := range structureTests {
if included[test] {
outStructureTests = append(outStructureTests, test)
}
}
outFunctionalTests = make([]string, 0, len(functionalTests))
for _, test := range functionalTests {
if included[test] {
outFunctionalTests = append(outFunctionalTests, test)
}
}
return
}
func renderCloudBuildConfig(
registry string, spec versions.Spec, options cloudBuildOptions) string {
data := newCloudBuildTemplateData(registry, spec, options)
tmpl, _ := template.
New("cloudBuildTemplate").
Parse(cloudBuildTemplateString)
var result bytes.Buffer
tmpl.Execute(&result, data)
return result.String()
}
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
config := versions.LoadConfig("versions.yaml", "cloudbuild")
registryPtr := config.StringOption("registry", "gcr.io/$PROJECT_ID", "Registry, e.g: 'gcr.io/my-project'")
dirsPtr := config.StringOption("dirs", "", "Comma separated list of Dockerfile dirs to use.")
testsPtr := config.BoolOption("tests", true, "Run tests.")
newTagsPtr := config.BoolOption("new_tags", false, "Require that image tags do not already exist.")
firstTagOnly := config.BoolOption("first_tag", false, "Build only the first per version.")
timeoutPtr := config.IntOption("timeout", 0, "Timeout in seconds. If not set, the default Cloud Build timeout is used.")
machineTypePtr := config.StringOption("machineType","", "Optional machine type used to run the build, , must be one of: N1_HIGHCPU_8, N1_HIGHCPU_32, E2_HIGHCPU_8, E2_HIGHCPU_32. If not specified, the default machine is used.")
enableParallel := config.BoolOption("enable_parallel", false, "Enable parallel build and bigger VM")
forceParallel := config.BoolOption("force_parallel", false, "Force parallel build and bigger VM")
config.Parse()
if *registryPtr == "" {
log.Fatalf("--registry flag is required")
}
if strings.Contains(*registryPtr, ":") {
*registryPtr = strings.Replace(*registryPtr, ":", "/", 1)
}
var dirs []string
if *dirsPtr != "" {
dirs = strings.Split(*dirsPtr, ",")
}
spec := versions.LoadVersions("versions.yaml")
options := cloudBuildOptions{dirs, *testsPtr, *newTagsPtr, *firstTagOnly, *timeoutPtr, *machineTypePtr, *enableParallel, *forceParallel}
result := renderCloudBuildConfig(*registryPtr, spec, options)
fmt.Println(result)
}