-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs.go
471 lines (413 loc) · 14.5 KB
/
jobs.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
import (
"fmt"
"regexp"
"time"
pipelinev1alpha1 "github.com/knative/build-pipeline/pkg/apis/pipeline/v1alpha1"
buildv1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/test-infra/prow/kube"
)
// Preset is intended to match the k8s' PodPreset feature, and may be removed
// if that feature goes beta.
type Preset struct {
Labels map[string]string `json:"labels"`
Env []v1.EnvVar `json:"env"`
Volumes []v1.Volume `json:"volumes"`
VolumeMounts []v1.VolumeMount `json:"volumeMounts"`
}
func mergePreset(preset Preset, labels map[string]string, pod *v1.PodSpec) error {
if pod == nil {
return nil
}
for l, v := range preset.Labels {
if v2, ok := labels[l]; !ok || v2 != v {
return nil
}
}
for _, e1 := range preset.Env {
for i := range pod.Containers {
for _, e2 := range pod.Containers[i].Env {
if e1.Name == e2.Name {
return fmt.Errorf("env var duplicated in pod spec: %s", e1.Name)
}
}
pod.Containers[i].Env = append(pod.Containers[i].Env, e1)
}
}
for _, v1 := range preset.Volumes {
for _, v2 := range pod.Volumes {
if v1.Name == v2.Name {
return fmt.Errorf("volume duplicated in pod spec: %s", v1.Name)
}
}
pod.Volumes = append(pod.Volumes, v1)
}
for _, vm1 := range preset.VolumeMounts {
for i := range pod.Containers {
for _, vm2 := range pod.Containers[i].VolumeMounts {
if vm1.Name == vm2.Name {
return fmt.Errorf("volume mount duplicated in pod spec: %s", vm1.Name)
}
}
pod.Containers[i].VolumeMounts = append(pod.Containers[i].VolumeMounts, vm1)
}
}
return nil
}
// JobBase contains attributes common to all job types
type JobBase struct {
// The name of the job. Must match regex [A-Za-z0-9-._]+
// e.g. pull-test-infra-bazel-build
Name string `json:"name"`
// Labels are added to prowjobs and pods created for this job.
Labels map[string]string `json:"labels,omitempty"`
// MaximumConcurrency of this job, 0 implies no limit.
MaxConcurrency int `json:"max_concurrency,omitempty"`
// Agent that will take care of running this job.
Agent string `json:"agent"`
// Cluster is the alias of the cluster to run this job in.
// (Default: kube.DefaultClusterAlias)
Cluster string `json:"cluster,omitempty"`
// Namespace is the namespace in which pods schedule.
// nil: results in config.PodNamespace (aka pod default)
// empty: results in config.ProwJobNamespace (aka same as prowjob)
Namespace *string `json:"namespace,omitempty"`
// ErrorOnEviction indicates that the ProwJob should be completed and given
// the ErrorState status if the pod that is executing the job is evicted.
// If this field is unspecified or false, a new pod will be created to replace
// the evicted one.
ErrorOnEviction bool `json:"error_on_eviction,omitempty"`
// SourcePath contains the path where this job is defined
SourcePath string `json:"-"`
// Spec is the Kubernetes pod spec used if Agent is kubernetes.
Spec *v1.PodSpec `json:"spec,omitempty"`
// BuildSpec is the Knative build spec used if Agent is knative-build.
BuildSpec *buildv1alpha1.BuildSpec `json:"build_spec,omitempty"`
// PipelineRunSpec is the Knative build pipeline spec used if Agent is knative-pipeline-run.
PipelineRunSpec *pipelinev1alpha1.PipelineRunSpec `json:"pipeline_run_spec,omitempty"`
UtilityConfig
}
// Presubmit runs on PRs.
type Presubmit struct {
JobBase
// AlwaysRun automatically for every PR, or only when a comment triggers it.
AlwaysRun bool `json:"always_run"`
// Context is the name of the GitHub status context for the job.
Context string `json:"context"`
// Optional indicates that the job's status context should not be required for merge.
Optional bool `json:"optional,omitempty"`
// SkipReport skips commenting and setting status on GitHub.
SkipReport bool `json:"skip_report,omitempty"`
// Trigger is the regular expression to trigger the job.
// e.g. `@k8s-bot e2e test this`
// RerunCommand must also be specified if this field is specified.
// (Default: `(?m)^/test (?:.*? )?<job name>(?: .*?)?$`)
Trigger string `json:"trigger"`
// The RerunCommand to give users. Must match Trigger.
// Trigger must also be specified if this field is specified.
// (Default: `/test <job name>`)
RerunCommand string `json:"rerun_command"`
// RunAfterSuccess is a list of jobs to run after successfully running this one.
RunAfterSuccess []Presubmit `json:"run_after_success,omitempty"`
Brancher
RegexpChangeMatcher
// We'll set these when we load it.
re *regexp.Regexp // from Trigger.
}
// Postsubmit runs on push events.
type Postsubmit struct {
JobBase
RegexpChangeMatcher
Brancher
// Run these jobs after successfully running this one.
RunAfterSuccess []Postsubmit `json:"run_after_success,omitempty"`
}
// Periodic runs on a timer.
type Periodic struct {
JobBase
// (deprecated)Interval to wait between two runs of the job.
Interval string `json:"interval"`
// Cron representation of job trigger time
Cron string `json:"cron"`
// Tags for config entries
Tags []string `json:"tags,omitempty"`
// Run these jobs after successfully running this one.
RunAfterSuccess []Periodic `json:"run_after_success,omitempty"`
interval time.Duration
}
// SetInterval updates interval, the frequency duration it runs.
func (p *Periodic) SetInterval(d time.Duration) {
p.interval = d
}
// GetInterval returns interval, the frequency duration it runs.
func (p *Periodic) GetInterval() time.Duration {
return p.interval
}
// Brancher is for shared code between jobs that only run against certain
// branches. An empty brancher runs against all branches.
type Brancher struct {
// Do not run against these branches. Default is no branches.
SkipBranches []string `json:"skip_branches,omitempty"`
// Only run against these branches. Default is all branches.
Branches []string `json:"branches,omitempty"`
// We'll set these when we load it.
re *regexp.Regexp
reSkip *regexp.Regexp
}
// RegexpChangeMatcher is for code shared between jobs that run only when certain files are changed.
type RegexpChangeMatcher struct {
// RunIfChanged defines a regex used to select which subset of file changes should trigger this job.
// If any file in the changeset matches this regex, the job will be triggered
RunIfChanged string `json:"run_if_changed,omitempty"`
reChanges *regexp.Regexp // from RunIfChanged
}
// RunsAgainstAllBranch returns true if there are both branches and skip_branches are unset
func (br Brancher) RunsAgainstAllBranch() bool {
return len(br.SkipBranches) == 0 && len(br.Branches) == 0
}
// RunsAgainstBranch returns true if the input branch matches, given the whitelist/blacklist.
func (br Brancher) RunsAgainstBranch(branch string) bool {
if br.RunsAgainstAllBranch() {
return true
}
// Favor SkipBranches over Branches
if len(br.SkipBranches) != 0 && br.reSkip.MatchString(branch) {
return false
}
if len(br.Branches) == 0 || br.re.MatchString(branch) {
return true
}
return false
}
// Intersects checks if other Brancher would trigger for the same branch.
func (br Brancher) Intersects(other Brancher) bool {
if br.RunsAgainstAllBranch() || other.RunsAgainstAllBranch() {
return true
}
if len(br.Branches) > 0 {
baseBranches := sets.NewString(br.Branches...)
if len(other.Branches) > 0 {
otherBranches := sets.NewString(other.Branches...)
if baseBranches.Intersection(otherBranches).Len() > 0 {
return true
}
return false
}
if !baseBranches.Intersection(sets.NewString(other.SkipBranches...)).Equal(baseBranches) {
return true
}
return false
}
if len(other.Branches) == 0 {
// There can only be one Brancher with skip_branches.
return true
}
return other.Intersects(br)
}
// RunsAgainstChanges returns true if any of the changed input paths match the run_if_changed regex.
func (cm RegexpChangeMatcher) RunsAgainstChanges(changes []string) bool {
if cm.RunIfChanged == "" {
return true
}
for _, change := range changes {
if cm.reChanges.MatchString(change) {
return true
}
}
return false
}
// TriggerMatches returns true if the comment body should trigger this presubmit.
//
// This is usually a /test foo string.
func (ps Presubmit) TriggerMatches(body string) bool {
return ps.re.MatchString(body)
}
// ContextRequired checks whether a context is required from github points of view (required check).
func (ps Presubmit) ContextRequired() bool {
if ps.Optional || ps.SkipReport {
return false
}
return true
}
// ChangedFilesProvider returns a slice of modified files.
type ChangedFilesProvider func() ([]string, error)
func matching(j Presubmit, body string, testAll bool) []Presubmit {
// When matching ignore whether the job runs for the branch or whether the job runs for the
// PR's changes. Even if the job doesn't run, it still matches the PR and may need to be marked
// as skipped on github.
var result []Presubmit
if (testAll && (j.AlwaysRun || j.RunIfChanged != "")) || j.TriggerMatches(body) {
result = append(result, j)
}
for _, child := range j.RunAfterSuccess {
result = append(result, matching(child, body, testAll)...)
}
return result
}
// MatchingPresubmits returns a slice of presubmits to trigger based on the repo and a comment text.
func (c *JobConfig) MatchingPresubmits(fullRepoName, body string, testAll bool) []Presubmit {
var result []Presubmit
if jobs, ok := c.Presubmits[fullRepoName]; ok {
for _, job := range jobs {
result = append(result, matching(job, body, testAll)...)
}
}
return result
}
// UtilityConfig holds decoration metadata, such as how to clone and additional containers/etc
type UtilityConfig struct {
// Decorate determines if we decorate the PodSpec or not
Decorate bool `json:"decorate,omitempty"`
// PathAlias is the location under <root-dir>/src
// where the repository under test is cloned. If this
// is not set, <root-dir>/src/github.com/org/repo will
// be used as the default.
PathAlias string `json:"path_alias,omitempty"`
// CloneURI is the URI that is used to clone the
// repository. If unset, will default to
// `http://github.com/org/repo.git`.
CloneURI string `json:"clone_uri,omitempty"`
// SkipSubmodules determines if submodules should be
// cloned when the job is run. Defaults to true.
SkipSubmodules bool `json:"skip_submodules,omitempty"`
// ExtraRefs are auxiliary repositories that
// need to be cloned, determined from config
ExtraRefs []kube.Refs `json:"extra_refs,omitempty"`
// DecorationConfig holds configuration options for
// decorating PodSpecs that users provide
DecorationConfig *kube.DecorationConfig `json:"decoration_config,omitempty"`
}
// RetestPresubmits returns all presubmits that should be run given a /retest command.
// This is the set of all presubmits intersected with ((alwaysRun + runContexts) - skipContexts)
func (c *JobConfig) RetestPresubmits(fullRepoName string, skipContexts, runContexts map[string]bool) []Presubmit {
var result []Presubmit
if jobs, ok := c.Presubmits[fullRepoName]; ok {
for _, job := range jobs {
if skipContexts[job.Context] {
continue
}
if job.AlwaysRun || job.RunIfChanged != "" || runContexts[job.Context] {
result = append(result, job)
}
}
}
return result
}
// GetPresubmit returns the presubmit job for the provided repo and job name.
func (c *JobConfig) GetPresubmit(repo, jobName string) *Presubmit {
presubmits := c.AllPresubmits([]string{repo})
for i := range presubmits {
ps := presubmits[i]
if ps.Name == jobName {
return &ps
}
}
return nil
}
// SetPresubmits updates c.Presubmits to jobs, after compiling and validating their regexes.
func (c *JobConfig) SetPresubmits(jobs map[string][]Presubmit) error {
nj := map[string][]Presubmit{}
for k, v := range jobs {
nj[k] = make([]Presubmit, len(v))
copy(nj[k], v)
if err := SetPresubmitRegexes(nj[k]); err != nil {
return err
}
}
c.Presubmits = nj
return nil
}
// SetPostsubmits updates c.Postsubmits to jobs, after compiling and validating their regexes.
func (c *JobConfig) SetPostsubmits(jobs map[string][]Postsubmit) error {
nj := map[string][]Postsubmit{}
for k, v := range jobs {
nj[k] = make([]Postsubmit, len(v))
copy(nj[k], v)
if err := SetPostsubmitRegexes(nj[k]); err != nil {
return err
}
}
c.Postsubmits = nj
return nil
}
// listPresubmits list all the presubmit for a given repo including the run after success jobs.
func listPresubmits(ps []Presubmit) []Presubmit {
var res []Presubmit
for _, p := range ps {
res = append(res, p)
res = append(res, listPresubmits(p.RunAfterSuccess)...)
}
return res
}
// AllPresubmits returns all prow presubmit jobs in repos.
// if repos is empty, return all presubmits.
func (c *JobConfig) AllPresubmits(repos []string) []Presubmit {
var res []Presubmit
for repo, v := range c.Presubmits {
if len(repos) == 0 {
res = append(res, listPresubmits(v)...)
} else {
for _, r := range repos {
if r == repo {
res = append(res, listPresubmits(v)...)
break
}
}
}
}
return res
}
// listPostsubmits list all the postsubmits for a given repo including the run after success jobs.
func listPostsubmits(ps []Postsubmit) []Postsubmit {
var res []Postsubmit
for _, p := range ps {
res = append(res, p)
res = append(res, listPostsubmits(p.RunAfterSuccess)...)
}
return res
}
// AllPostsubmits returns all prow postsubmit jobs in repos.
// if repos is empty, return all postsubmits.
func (c *JobConfig) AllPostsubmits(repos []string) []Postsubmit {
var res []Postsubmit
for repo, v := range c.Postsubmits {
if len(repos) == 0 {
res = append(res, listPostsubmits(v)...)
} else {
for _, r := range repos {
if r == repo {
res = append(res, listPostsubmits(v)...)
break
}
}
}
}
return res
}
// AllPeriodics returns all prow periodic jobs.
func (c *JobConfig) AllPeriodics() []Periodic {
var listPeriodic func(ps []Periodic) []Periodic
listPeriodic = func(ps []Periodic) []Periodic {
var res []Periodic
for _, p := range ps {
res = append(res, p)
res = append(res, listPeriodic(p.RunAfterSuccess)...)
}
return res
}
return listPeriodic(c.Periodics)
}