/
prowgen_config.go
396 lines (341 loc) · 14.3 KB
/
prowgen_config.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package prowgen
import (
"context"
"fmt"
"log"
"math/rand"
"os"
"path/filepath"
"slices"
"strings"
"github.com/coreos/go-semver/semver"
cioperatorapi "github.com/openshift/ci-tools/pkg/api"
prowapi "k8s.io/test-infra/prow/apis/prowjobs/v1"
)
type Repository struct {
Org string `json:"org,omitempty" yaml:"org,omitempty"`
Repo string `json:"repo,omitempty" yaml:"repo,omitempty"`
Promotion Promotion `json:"promotion,omitempty" yaml:"promotion,omitempty"`
ImagePrefix string `json:"imagePrefix,omitempty" yaml:"imagePrefix,omitempty"`
ImageNameOverrides map[string]string `json:"imageNameOverrides,omitempty" yaml:"imageNameOverrides,omitempty"`
SlackChannel string `json:"slackChannel,omitempty" yaml:"slackChannel,omitempty"`
CanonicalGoRepository *string `json:"canonicalGoRepository,omitempty" yaml:"canonicalGoRepository,omitempty"`
E2ETests []E2ETest `json:"e2e,omitempty" yaml:"e2e,omitempty"`
Dockerfiles Dockerfiles `json:"dockerfiles,omitempty" yaml:"dockerfiles,omitempty"`
IgnoreConfigs IgnoreConfigs `json:"ignoreConfigs,omitempty" yaml:"ignoreConfigs,omitempty"`
CustomConfigs []CustomConfigs `json:"customConfigs,omitempty" yaml:"customConfigs,omitempty"`
Images []cioperatorapi.ProjectDirectoryImageBuildStepConfiguration `json:"images,omitempty" yaml:"images,omitempty"`
Tests []cioperatorapi.TestStepConfiguration `json:"tests,omitempty" yaml:"tests,omitempty"`
Resources cioperatorapi.ResourceConfiguration `json:"resources,omitempty" yaml:"resources,omitempty"`
}
type E2ETest struct {
Match string `json:"match,omitempty" yaml:"match,omitempty"`
OnDemand bool `json:"onDemand,omitempty" yaml:"onDemand,omitempty"`
IgnoreError bool `json:"ignoreError,omitempty" yaml:"ignoreError,omitempty"`
RunIfChanged string `json:"runIfChanged,omitempty" yaml:"runIfChanged,omitempty"`
// SkipCron ensures that no periodic job will be generated for the given test.
SkipCron bool `json:"skipCron,omitempty" yaml:"skipCron,omitempty"`
SkipImages []string `json:"skipImages,omitempty" yaml:"skipImages,omitempty"`
Timeout *prowapi.Duration `json:"timeout,omitempty" yaml:"timeout,omitempty"`
}
type Dockerfiles struct {
Matches []string `json:"matches,omitempty" yaml:"matches,omitempty"`
}
type IgnoreConfigs struct {
Matches []string `json:"matches,omitempty" yaml:"matches,omitempty"`
}
type Promotion struct {
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
}
type CustomConfigs struct {
// Name will be used together with OpenShift version to generate a specific variant.
Name string `json:"name,omitempty" yaml:"name,omitempty"`
// ReleaseBuildConfiguration allows defining configuration manually. The final configuration
// is extended with images and test steps with dependencies.
ReleaseBuildConfiguration cioperatorapi.ReleaseBuildConfiguration `json:"releaseBuildConfiguration,omitempty" yaml:"releaseBuildConfiguration,omitempty"`
}
func (r Repository) RepositoryDirectory() string {
return filepath.Join(r.Org, r.Repo)
}
type Branch struct {
OpenShiftVersions []OpenShift `json:"openShiftVersions,omitempty" yaml:"openShiftVersions,omitempty"`
SkipE2EMatches []string `json:"skipE2EMatches,omitempty" yaml:"skipE2EMatches,omitempty"`
SkipDockerFilesMatches []string `json:"skipDockerFilesMatches,omitempty" yaml:"skipDockerFilesMatches,omitempty"`
}
type OpenShift struct {
Version string `json:"version,omitempty" yaml:"version,omitempty"`
Cron string `json:"cron,omitempty" yaml:"cron,omitempty"`
// SkipCron ensures that no periodic jobs are generated for tests running on the given OpenShift version.
SkipCron bool `json:"skipCron,omitempty" yaml:"skipCron,omitempty"`
OnDemand bool `json:"onDemand,omitempty" yaml:"onDemand,omitempty"`
GenerateCustomConfigs bool `json:"generateCustomConfigs,omitempty" yaml:"generateCustomConfigs,omitempty"`
CandidateRelease bool `json:"candidateRelease,omitempty" yaml:"candidateRelease,omitempty"`
}
type CommonConfig struct {
Branches map[string]Branch `json:"branches,omitempty" yaml:"branches,omitempty"`
}
type ReleaseBuildConfigurationOption func(cfg *cioperatorapi.ReleaseBuildConfiguration) error
type ProjectDirectoryImageBuildStepConfigurationFunc func() (cioperatorapi.ProjectDirectoryImageBuildStepConfiguration, error)
type ReleaseBuildConfiguration struct {
cioperatorapi.ReleaseBuildConfiguration
Path string
Branch string
}
func NewGenerateConfigs(ctx context.Context, r Repository, cc CommonConfig, opts ...ReleaseBuildConfigurationOption) ([]ReleaseBuildConfiguration, error) {
// Use the same seed to always get the same sequence of random
// numbers for tests within the given repository. It means the cron schedules
// for jobs will change less often when generating jobs.
random := rand.New(rand.NewSource(seed))
cfgs := make([]ReleaseBuildConfiguration, 0, len(cc.Branches)*2)
if err := GitMirror(ctx, r); err != nil {
return nil, err
}
branches := make([]string, 0, len(cc.Branches))
for k := range cc.Branches {
branches = append(branches, k)
}
// Make sure to iterate every time in the same order to keep
// cron times consistent between runs.
slices.Sort(branches)
for _, branchName := range branches {
branch := cc.Branches[branchName]
if err := GitCheckout(ctx, r, branchName); err != nil {
return nil, fmt.Errorf("[%s] failed to checkout branch %s", r.RepositoryDirectory(), branchName)
}
var err error
openshiftVersions := branch.OpenShiftVersions
openshiftVersions, err = addCandidateRelease(branch.OpenShiftVersions)
if err != nil {
return nil, err
}
for i, ov := range openshiftVersions {
log.Println(r.RepositoryDirectory(), "Generating config", branchName, "OpenShiftVersion", ov)
variant := strings.ReplaceAll(ov.Version, ".", "")
images := make([]cioperatorapi.ProjectDirectoryImageBuildStepConfiguration, 0, len(r.Images))
for _, img := range r.Images {
images = append(images, *img.DeepCopy())
}
tests := make([]cioperatorapi.TestStepConfiguration, 0, len(r.Tests))
for _, test := range r.Tests {
tests = append(tests, *test.DeepCopy())
}
resources := make(cioperatorapi.ResourceConfiguration, 1)
resources["*"] = cioperatorapi.ResourceRequirements{
Requests: map[string]string{
"cpu": "500m",
"memory": "1Gi",
},
}
for k, v := range r.Resources {
resources[k] = v
}
metadata := cioperatorapi.Metadata{
Org: r.Org,
Repo: r.Repo,
Branch: branchName,
Variant: variant,
}
buildRootImage := &cioperatorapi.BuildRootImageConfiguration{
ProjectImageBuild: &cioperatorapi.ProjectDirectoryImageBuildInputs{
DockerfilePath: "openshift/ci-operator/build-image/Dockerfile",
},
}
// Include releases as it's required by clusters that start from scratch (vs. cluster-pools).
releases := map[string]cioperatorapi.UnresolvedRelease{
"latest": {
Release: &cioperatorapi.Release{
Version: ov.Version,
Channel: cioperatorapi.ReleaseChannelFast},
},
}
if ov.CandidateRelease {
releases = map[string]cioperatorapi.UnresolvedRelease{
"latest": {
Candidate: &cioperatorapi.Candidate{
Version: ov.Version,
Stream: "nightly",
ReleaseDescriptor: cioperatorapi.ReleaseDescriptor{
Product: "ocp",
},
}},
}
}
cfg := cioperatorapi.ReleaseBuildConfiguration{
Metadata: metadata,
InputConfiguration: cioperatorapi.InputConfiguration{
BuildRootImage: buildRootImage,
Releases: releases,
},
CanonicalGoRepository: r.CanonicalGoRepository,
Images: images,
Tests: tests,
Resources: resources,
}
options := make([]ReleaseBuildConfigurationOption, 0, len(opts))
copy(options, opts)
if i == 0 {
options = append(options, withNamePromotion(r, branchName))
} else if i == 1 {
options = append(options, withTagPromotion(r, branchName))
}
fromImage := srcImage
srcImageDockerfile, err := discoverSourceImageDockerfile(r)
if err != nil {
return nil, err
}
if srcImageDockerfile != "" {
fromImage = toImage(r, ImageInput{
Context: discoverImageContext(srcImageDockerfile),
DockerfilePath: strings.Join(strings.Split(srcImageDockerfile, string(os.PathSeparator))[2:], string(os.PathSeparator)),
})
}
options = append(
options,
DiscoverImages(r, branch.SkipDockerFilesMatches),
DiscoverTests(r, ov, fromImage, branch.SkipE2EMatches, random),
)
log.Println(r.RepositoryDirectory(), "Apply input options", len(options))
if err := applyOptions(&cfg, options...); err != nil {
return nil, fmt.Errorf("[%s] failed to apply option: %w", r.RepositoryDirectory(), err)
}
log.Println("numTests", len(cfg.Tests), "numImages", len(cfg.Images))
// openshift-knative/eventing-kafka-broker/openshift-knative-eventing-kafka-broker-release-next__411.yaml
buildConfigPath := filepath.Join(
r.RepositoryDirectory(),
r.Org+"-"+r.Repo+"-"+branchName+"__"+variant+".yaml",
)
cfgs = append(cfgs, ReleaseBuildConfiguration{
ReleaseBuildConfiguration: cfg,
Path: buildConfigPath,
Branch: branchName,
})
if !ov.GenerateCustomConfigs {
continue
}
// Generate custom configs.
for _, customCfg := range r.CustomConfigs {
customBuildCfg := customCfg.ReleaseBuildConfiguration.DeepCopy()
customBuildCfg.Metadata = metadata
if customBuildCfg.BuildRootImage == nil {
customBuildCfg.BuildRootImage = buildRootImage
}
if customBuildCfg.CanonicalGoRepository == nil {
customBuildCfg.CanonicalGoRepository = r.CanonicalGoRepository
}
if len(customBuildCfg.Resources) == 0 {
customBuildCfg.Resources = resources
}
if len(customBuildCfg.Releases) == 0 {
customBuildCfg.Releases = releases
}
customBuildOptions := append(
opts,
DiscoverImages(r, branch.SkipDockerFilesMatches),
DependenciesForTestSteps(),
)
log.Println(r.RepositoryDirectory(), "Apply input options", len(customBuildOptions))
if err := applyOptions(customBuildCfg, customBuildOptions...); err != nil {
return nil, fmt.Errorf("[%s] failed to apply option: %w", r.RepositoryDirectory(), err)
}
log.Println("numTests", len(customBuildCfg.Tests), "numImages", len(customBuildCfg.Images))
buildConfigPath = filepath.Join(
r.RepositoryDirectory(),
r.Org+"-"+r.Repo+"-"+branchName+"__"+variant+"-"+customCfg.Name+".yaml",
)
cfgs = append(cfgs, ReleaseBuildConfiguration{
ReleaseBuildConfiguration: *customBuildCfg,
Path: buildConfigPath,
Branch: branchName,
})
}
}
}
return cfgs, nil
}
// TODO: In 2023 we need to move forward to use the new `eventing` or `serving`, for _new_ repos,
// The tool should only generate desired updates, always all
func transformLegacyKnativeSourceImageName(r Repository) string {
// The old repository is called knative-eventing or knative-serving,
// and we need to keep this coordinate on the build
// For now: we can not use the new `eventing` or `serving` name
srcImage := r.Repo + "-src"
if r.Repo == "eventing" || r.Repo == "serving" {
srcImage = "knative-" + r.Repo + "-src"
}
return srcImage
}
func withNamePromotion(r Repository, branchName string) ReleaseBuildConfigurationOption {
return func(cfg *cioperatorapi.ReleaseBuildConfiguration) error {
ns := "openshift"
if r.Promotion.Namespace != "" {
ns = r.Promotion.Namespace
}
cfg.PromotionConfiguration = &cioperatorapi.PromotionConfiguration{
Targets: []cioperatorapi.PromotionTarget{
{
Namespace: ns,
Name: strings.ReplaceAll(strings.ReplaceAll(branchName, "release", "knative"), "next", "nightly"),
AdditionalImages: map[string]string{
// Add source image
transformLegacyKnativeSourceImageName(r): "src",
},
},
},
}
return nil
}
}
func withTagPromotion(r Repository, branchName string) ReleaseBuildConfigurationOption {
return func(cfg *cioperatorapi.ReleaseBuildConfiguration) error {
ns := "openshift"
if r.Promotion.Namespace != "" {
ns = r.Promotion.Namespace
}
cfg.PromotionConfiguration = &cioperatorapi.PromotionConfiguration{
Targets: []cioperatorapi.PromotionTarget{
{
Namespace: ns,
Tag: strings.ReplaceAll(strings.ReplaceAll(branchName, "release", "knative"), "next", "nightly"),
TagByCommit: false, // TODO: revisit this later
AdditionalImages: map[string]string{
// Add source image
transformLegacyKnativeSourceImageName(r): "src",
},
},
},
}
return nil
}
}
func addCandidateRelease(openshiftVersions []OpenShift) ([]OpenShift, error) {
semVersions := make([]*semver.Version, 0, len(openshiftVersions))
for _, ov := range openshiftVersions {
v := ov.Version
// Make sure version strings are in the format MAJOR.MINOR.MICRO
if len(strings.SplitN(v, ".", 3)) != 3 {
v = v + ".0"
}
ovSemVer, err := semver.NewVersion(v)
if err != nil {
return nil, err
}
semVersions = append(semVersions, ovSemVer)
}
semver.Sort(semVersions)
latest := *semVersions[len(semVersions)-1]
latest.BumpMinor()
extendedVersions := append(openshiftVersions, OpenShift{
Version: fmt.Sprintf("%d.%d", latest.Major, latest.Minor),
OnDemand: true,
CandidateRelease: true},
)
return extendedVersions, nil
}
func applyOptions(cfg *cioperatorapi.ReleaseBuildConfiguration, opts ...ReleaseBuildConfigurationOption) error {
for _, opt := range opts {
if err := opt(cfg); err != nil {
return err
}
}
return nil
}