-
Notifications
You must be signed in to change notification settings - Fork 114
/
config.go
320 lines (298 loc) · 9.04 KB
/
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
/*
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 job
import (
"fmt"
"github.com/jenkins-x/lighthouse/pkg/config/lighthouse"
"gopkg.in/robfig/cron.v2"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
)
// Config is config for all prow jobs
type Config struct {
// Presets apply to all job types.
Presets []Preset `json:"presets,omitempty"`
// Full repo name (such as "kubernetes/kubernetes") -> list of jobs.
Presubmits map[string][]Presubmit `json:"presubmits,omitempty"`
Postsubmits map[string][]Postsubmit `json:"postsubmits,omitempty"`
// Periodics are not associated with any repo.
Periodics []Periodic `json:"periodics,omitempty"`
}
func resolvePresets(name string, labels map[string]string, spec *v1.PodSpec, presets []Preset) error {
for _, preset := range presets {
if err := MergePreset(preset, labels, spec); err != nil {
return fmt.Errorf("job %s failed to merge presets: %v", name, err)
}
}
return nil
}
// Merge merges one Config with another one
func (c *Config) Merge(other Config) error {
c.Presets = append(c.Presets, other.Presets...)
// validate no duplicated preset key-value pairs
validLabels := map[string]bool{}
for _, preset := range c.Presets {
for label, val := range preset.Labels {
pair := label + ":" + val
if _, ok := validLabels[pair]; ok {
return fmt.Errorf("duplicated preset 'label:value' pair : %s", pair)
}
validLabels[pair] = true
}
}
c.Periodics = append(c.Periodics, other.Periodics...)
if c.Presubmits == nil {
c.Presubmits = make(map[string][]Presubmit)
}
for repo, jobs := range other.Presubmits {
c.Presubmits[repo] = append(c.Presubmits[repo], jobs...)
}
if c.Postsubmits == nil {
c.Postsubmits = make(map[string][]Postsubmit)
}
for repo, jobs := range other.Postsubmits {
c.Postsubmits[repo] = append(c.Postsubmits[repo], jobs...)
}
return nil
}
// Init sets defaults and initializes Config
func (c *Config) Init(lh lighthouse.Config) error {
decoration := c.DecorationRequested()
if decoration {
// if c.Plank.DefaultDecorationConfig == nil {
// return errors.New("no default decoration config provided for plank")
// }
// if c.Plank.DefaultDecorationConfig.UtilityImages == nil {
// return errors.New("no default decoration image pull specs provided for plank")
// }
// if c.Plank.DefaultDecorationConfig.GCSConfiguration == nil {
// return errors.New("no default GCS decoration config provided for plank")
// }
// if c.Plank.DefaultDecorationConfig.GCSCredentialsSecret == "" {
// return errors.New("no default GCS credentials secret provided for plank")
// }
// for _, vs := range c.Presubmits {
// for i := range vs {
// // if ps.Decorate {
// // ps.DecorationConfig = ps.DecorationConfig.ApplyDefault(c.Plank.DefaultDecorationConfig)
// // }
// }
// }
// for _, js := range c.Postsubmits {
// for i := range js {
// // if ps.Decorate {
// // ps.DecorationConfig = ps.DecorationConfig.ApplyDefault(c.Plank.DefaultDecorationConfig)
// // }
// }
// }
// for i := range c.Periodics {
// // if ps.Decorate {
// // ps.DecorationConfig = ps.DecorationConfig.ApplyDefault(c.Plank.DefaultDecorationConfig)
// // }
// }
}
for _, ps := range c.Presubmits {
for i := range ps {
ps[i].SetDefaults(lh.PodNamespace)
if err := ps[i].SetRegexes(); err != nil {
return fmt.Errorf("could not set regex: %v", err)
}
if err := resolvePresets(ps[i].Name, ps[i].Labels, ps[i].Spec, c.Presets); err != nil {
return err
}
}
}
for _, ps := range c.Postsubmits {
for i := range ps {
ps[i].SetDefaults(lh.PodNamespace)
if err := ps[i].SetRegexes(); err != nil {
return fmt.Errorf("could not set regex: %v", err)
}
if err := resolvePresets(ps[i].Name, ps[i].Labels, ps[i].Spec, c.Presets); err != nil {
return err
}
}
}
for i := range c.Periodics {
c.Periodics[i].SetDefaults(lh.PodNamespace)
if err := resolvePresets(c.Periodics[i].Name, c.Periodics[i].Labels, c.Periodics[i].Spec, c.Presets); err != nil {
return err
}
}
return nil
}
// Validate validates Config
func (c *Config) Validate(lh lighthouse.Config) error {
type orgRepoJobName struct {
orgRepo, jobName string
}
// Validate presubmits.
// Checking that no duplicate job in prow config exists on the same org / repo / branch.
validPresubmits := map[orgRepoJobName][]Presubmit{}
for repo, jobs := range c.Presubmits {
for _, job := range jobs {
repoJobName := orgRepoJobName{repo, job.Name}
for _, existingJob := range validPresubmits[repoJobName] {
if existingJob.Name == job.Name {
return fmt.Errorf("duplicated presubmit job: %s", job.Name)
}
}
validPresubmits[repoJobName] = append(validPresubmits[repoJobName], job)
}
}
for _, ps := range c.Presubmits {
for _, j := range ps {
if err := j.Validate(lh.PodNamespace); err != nil {
return err
}
}
}
// Validate postsubmits.
// Checking that no duplicate job in prow config exists on the same org / repo / branch.
validPostsubmits := map[orgRepoJobName][]Postsubmit{}
for repo, jobs := range c.Postsubmits {
for _, job := range jobs {
repoJobName := orgRepoJobName{repo, job.Name}
for _, existingJob := range validPostsubmits[repoJobName] {
if existingJob.Brancher.Intersects(job.Brancher) {
return fmt.Errorf("duplicated postsubmit job: %s", job.Name)
}
}
validPostsubmits[repoJobName] = append(validPostsubmits[repoJobName], job)
}
}
for _, ps := range c.Postsubmits {
for _, j := range ps {
if err := j.Base.Validate(PostsubmitJob, lh.PodNamespace); err != nil {
return fmt.Errorf("invalid postsubmit job %s: %v", j.Name, err)
}
}
}
// validate no duplicated periodics
validPeriodics := sets.NewString()
// Ensure that the periodic durations are valid and specs exist.
for _, p := range c.Periodics {
if validPeriodics.Has(p.Name) {
return fmt.Errorf("duplicated periodic job : %s", p.Name)
}
validPeriodics.Insert(p.Name)
if err := p.Base.Validate(PeriodicJob, lh.PodNamespace); err != nil {
return fmt.Errorf("invalid periodic job %s: %v", p.Name, err)
}
}
// Set the interval on the periodic jobs. It doesn't make sense to do this
// for child jobs.
for _, p := range c.Periodics {
if p.Cron != "" {
if _, err := cron.Parse(p.Cron); err != nil {
return fmt.Errorf("invalid cron string %s in periodic %s: %v", p.Cron, p.Name, err)
}
} else {
return fmt.Errorf("cron cannot be empty in periodic %s", p.Name)
}
}
return nil
}
// DecorationRequested checks if decoration was requested
func (c *Config) DecorationRequested() bool {
for _, vs := range c.Presubmits {
for i := range vs {
if vs[i].Decorate {
return true
}
}
}
for _, js := range c.Postsubmits {
for i := range js {
if js[i].Decorate {
return true
}
}
}
for i := range c.Periodics {
if c.Periodics[i].Decorate {
return true
}
}
return false
}
// AllPresubmits returns all prow presubmit jobs in repos.
// if repos is empty, return all presubmits.
func (c *Config) AllPresubmits(repos []string) []Presubmit {
var res []Presubmit
for repo, v := range c.Presubmits {
if len(repos) == 0 {
res = append(res, v...)
} else {
for _, r := range repos {
if r == repo {
res = append(res, v...)
break
}
}
}
}
return res
}
// AllPostsubmits returns all prow postsubmit jobs in repos.
// if repos is empty, return all postsubmits.
func (c *Config) AllPostsubmits(repos []string) []Postsubmit {
var res []Postsubmit
for repo, v := range c.Postsubmits {
if len(repos) == 0 {
res = append(res, v...)
} else {
for _, r := range repos {
if r == repo {
res = append(res, v...)
break
}
}
}
}
return res
}
// AllPeriodics returns all prow periodic jobs.
func (c *Config) AllPeriodics() []Periodic {
return c.Periodics
}
// SetPresubmits updates c.Presubmits to jobs, after compiling and validating their regexes.
func (c *Config) SetPresubmits(jobs map[string][]Presubmit) error {
nj := map[string][]Presubmit{}
for k, v := range jobs {
for i := range v {
if err := v[i].SetRegexes(); err != nil {
return err
}
}
nj[k] = make([]Presubmit, len(v))
copy(nj[k], v)
}
c.Presubmits = nj
return nil
}
// SetPostsubmits updates c.Postsubmits to jobs, after compiling and validating their regexes.
func (c *Config) SetPostsubmits(jobs map[string][]Postsubmit) error {
nj := map[string][]Postsubmit{}
for k, v := range jobs {
for i := range v {
if err := v[i].SetRegexes(); err != nil {
return err
}
}
nj[k] = make([]Postsubmit, len(v))
copy(nj[k], v)
}
c.Postsubmits = nj
return nil
}