forked from kubernetes/test-infra
/
config.go
1248 lines (1109 loc) · 39.2 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
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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 knows how to read and parse config.yaml.
package config
import (
"errors"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"
"time"
"github.com/sirupsen/logrus"
"gopkg.in/robfig/cron.v2"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation"
"sigs.k8s.io/yaml"
prowjobv1 "k8s.io/test-infra/prow/apis/prowjobs/v1"
"k8s.io/test-infra/prow/config/org"
"k8s.io/test-infra/prow/github"
"k8s.io/test-infra/prow/kube"
"k8s.io/test-infra/prow/pod-utils/decorate"
"k8s.io/test-infra/prow/pod-utils/downwardapi"
)
// Config is a read-only snapshot of the config.
type Config struct {
JobConfig
ProwConfig
}
// JobConfig is config for all prow jobs
type JobConfig 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"`
}
// ProwConfig is config for all prow controllers
type ProwConfig struct {
Tide Tide `json:"tide,omitempty"`
Plank Plank `json:"plank,omitempty"`
Sinker Sinker `json:"sinker,omitempty"`
Deck Deck `json:"deck,omitempty"`
BranchProtection BranchProtection `json:"branch-protection,omitempty"`
Orgs map[string]org.Config `json:"orgs,omitempty"`
Gerrit Gerrit `json:"gerrit,omitempty"`
// TODO: Move this out of the main config.
JenkinsOperators []JenkinsOperator `json:"jenkins_operators,omitempty"`
// ProwJobNamespace is the namespace in the cluster that prow
// components will use for looking up ProwJobs. The namespace
// needs to exist and will not be created by prow.
// Defaults to "default".
ProwJobNamespace string `json:"prowjob_namespace,omitempty"`
// PodNamespace is the namespace in the cluster that prow
// components will use for looking up Pods owned by ProwJobs.
// The namespace needs to exist and will not be created by prow.
// Defaults to "default".
PodNamespace string `json:"pod_namespace,omitempty"`
// LogLevel enables dynamically updating the log level of the
// standard logger that is used by all prow components.
//
// Valid values:
//
// "debug", "info", "warn", "warning", "error", "fatal", "panic"
//
// Defaults to "info".
LogLevel string `json:"log_level,omitempty"`
// PushGateway is a prometheus push gateway.
PushGateway PushGateway `json:"push_gateway,omitempty"`
// OwnersDirBlacklist is used to configure which directories to ignore when
// searching for OWNERS{,_ALIAS} files in a repo.
OwnersDirBlacklist OwnersDirBlacklist `json:"owners_dir_blacklist,omitempty"`
// Pub/Sub Subscriptions that we want to listen to
PubSubSubscriptions PubsubSubscriptions `json:"pubsub_subscriptions,omitempty"`
}
// OwnersDirBlacklist is used to configure which directories to ignore when
// searching for OWNERS{,_ALIAS} files in a repo.
type OwnersDirBlacklist struct {
// Repos configures a directory blacklist per repo (or org)
Repos map[string][]string `json:"repos"`
// Default configures a default blacklist for repos (or orgs) not
// specifically configured
Default []string `json:"default"`
}
// PushGateway is a prometheus push gateway.
type PushGateway struct {
// Endpoint is the location of the prometheus pushgateway
// where prow will push metrics to.
Endpoint string `json:"endpoint,omitempty"`
// IntervalString compiles into Interval at load time.
IntervalString string `json:"interval,omitempty"`
// Interval specifies how often prow will push metrics
// to the pushgateway. Defaults to 1m.
Interval time.Duration `json:"-"`
}
// Controller holds configuration applicable to all agent-specific
// prow controllers.
type Controller struct {
// JobURLTemplateString compiles into JobURLTemplate at load time.
JobURLTemplateString string `json:"job_url_template,omitempty"`
// JobURLTemplate is compiled at load time from JobURLTemplateString. It
// will be passed a kube.ProwJob and is used to set the URL for the
// "Details" link on GitHub as well as the link from deck.
JobURLTemplate *template.Template `json:"-"`
// ReportTemplateString compiles into ReportTemplate at load time.
ReportTemplateString string `json:"report_template,omitempty"`
// ReportTemplate is compiled at load time from ReportTemplateString. It
// will be passed a kube.ProwJob and can provide an optional blurb below
// the test failures comment.
ReportTemplate *template.Template `json:"-"`
// MaxConcurrency is the maximum number of tests running concurrently that
// will be allowed by the controller. 0 implies no limit.
MaxConcurrency int `json:"max_concurrency,omitempty"`
// MaxGoroutines is the maximum number of goroutines spawned inside the
// controller to handle tests. Defaults to 20. Needs to be a positive
// number.
MaxGoroutines int `json:"max_goroutines,omitempty"`
// AllowCancellations enables aborting presubmit jobs for commits that
// have been superseded by newer commits in Github pull requests.
AllowCancellations bool `json:"allow_cancellations,omitempty"`
}
// Plank is config for the plank controller.
type Plank struct {
Controller `json:",inline"`
// PodPendingTimeoutString compiles into PodPendingTimeout at load time.
PodPendingTimeoutString string `json:"pod_pending_timeout,omitempty"`
// PodPendingTimeout is after how long the controller will perform a garbage
// collection on pending pods. Defaults to one day.
PodPendingTimeout time.Duration `json:"-"`
// DefaultDecorationConfig are defaults for shared fields for ProwJobs
// that request to have their PodSpecs decorated
DefaultDecorationConfig *kube.DecorationConfig `json:"default_decoration_config,omitempty"`
// JobURLPrefix is the host and path prefix under
// which job details will be viewable
JobURLPrefix string `json:"job_url_prefix,omitempty"`
}
// Gerrit is config for the gerrit controller.
type Gerrit struct {
// TickInterval is how often we do a sync with binded gerrit instance
TickIntervalString string `json:"tick_interval,omitempty"`
TickInterval time.Duration `json:"-"`
// RateLimit defines how many changes to query per gerrit API call
// default is 5
RateLimit int `json:"ratelimit,omitempty"`
}
// JenkinsOperator is config for the jenkins-operator controller.
type JenkinsOperator struct {
Controller `json:",inline"`
// LabelSelectorString compiles into LabelSelector at load time.
// If set, this option needs to match --label-selector used by
// the desired jenkins-operator. This option is considered
// invalid when provided with a single jenkins-operator config.
//
// For label selector syntax, see below:
// https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors
LabelSelectorString string `json:"label_selector,omitempty"`
// LabelSelector is used so different jenkins-operator replicas
// can use their own configuration.
LabelSelector labels.Selector `json:"-"`
}
// Sinker is config for the sinker controller.
type Sinker struct {
// ResyncPeriodString compiles into ResyncPeriod at load time.
ResyncPeriodString string `json:"resync_period,omitempty"`
// ResyncPeriod is how often the controller will perform a garbage
// collection. Defaults to one hour.
ResyncPeriod time.Duration `json:"-"`
// MaxProwJobAgeString compiles into MaxProwJobAge at load time.
MaxProwJobAgeString string `json:"max_prowjob_age,omitempty"`
// MaxProwJobAge is how old a ProwJob can be before it is garbage-collected.
// Defaults to one week.
MaxProwJobAge time.Duration `json:"-"`
// MaxPodAgeString compiles into MaxPodAge at load time.
MaxPodAgeString string `json:"max_pod_age,omitempty"`
// MaxPodAge is how old a Pod can be before it is garbage-collected.
// Defaults to one day.
MaxPodAge time.Duration `json:"-"`
}
// Spyglass holds config for Spyglass
type Spyglass struct {
// Viewers is a map of Regexp strings to viewer names that defines which sets
// of artifacts need to be consumed by which viewers. The keys are compiled
// and stored in RegexCache at load time.
Viewers map[string][]string `json:"viewers,omitempty"`
// RegexCache is a map of viewer regexp strings to their compiled equivalents.
RegexCache map[string]*regexp.Regexp `json:"-"`
// SizeLimit is the max size artifact in bytes that Spyglass will attempt to
// read in entirety. This will only affect viewers attempting to use
// artifact.ReadAll(). To exclude outlier artifacts, set this limit to
// expected file size + variance. To include all artifacts with high
// probability, use 2*maximum observed artifact size.
SizeLimit int64 `json:"size_limit,omitempty"`
}
// Deck holds config for deck.
type Deck struct {
// Spyglass specifies which viewers will be used for which artifacts when viewing a job in Deck
Spyglass Spyglass `json:"spyglass,omitempty"`
// TideUpdatePeriodString compiles into TideUpdatePeriod at load time.
TideUpdatePeriodString string `json:"tide_update_period,omitempty"`
// TideUpdatePeriod specifies how often Deck will fetch status from Tide. Defaults to 10s.
TideUpdatePeriod time.Duration `json:"-"`
// HiddenRepos is a list of orgs and/or repos that should not be displayed by Deck.
HiddenRepos []string `json:"hidden_repos,omitempty"`
// ExternalAgentLogs ensures external agents can expose
// their logs in prow.
ExternalAgentLogs []ExternalAgentLog `json:"external_agent_logs,omitempty"`
// Branding of the frontend
Branding *Branding `json:"branding,omitempty"`
}
// ExternalAgentLog ensures an external agent like Jenkins can expose
// its logs in prow.
type ExternalAgentLog struct {
// Agent is an external prow agent that supports exposing
// logs via deck.
Agent string `json:"agent,omitempty"`
// SelectorString compiles into Selector at load time.
SelectorString string `json:"selector,omitempty"`
// Selector can be used in prow deployments where the workload has
// been sharded between controllers of the same agent. For more info
// see https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors
Selector labels.Selector `json:"-"`
// URLTemplateString compiles into URLTemplate at load time.
URLTemplateString string `json:"url_template,omitempty"`
// URLTemplate is compiled at load time from URLTemplateString. It
// will be passed a kube.ProwJob and the generated URL should provide
// logs for the ProwJob.
URLTemplate *template.Template `json:"-"`
}
// Branding holds branding configuration for deck.
type Branding struct {
// Logo is the location of the logo that will be loaded in deck.
Logo string `json:"logo,omitempty"`
// Favicon is the location of the favicon that will be loaded in deck.
Favicon string `json:"favicon,omitempty"`
// BackgroundColor is the color of the background.
BackgroundColor string `json:"background_color,omitempty"`
// HeaderColor is the color of the header.
HeaderColor string `json:"header_color,omitempty"`
}
// PubSubSubscriptions maps GCP projects to a list of Topics.
type PubsubSubscriptions map[string][]string
// Load loads and parses the config at path.
func Load(prowConfig, jobConfig string) (c *Config, err error) {
// we never want config loading to take down the prow components
defer func() {
if r := recover(); r != nil {
c, err = nil, fmt.Errorf("panic loading config: %v", r)
}
}()
c, err = loadConfig(prowConfig, jobConfig)
if err != nil {
return nil, err
}
if err := c.finalizeJobConfig(); err != nil {
return nil, err
}
if err := c.validateComponentConfig(); err != nil {
return nil, err
}
if err := c.validateJobConfig(); err != nil {
return nil, err
}
return c, nil
}
// loadConfig loads one or multiple config files and returns a config object.
func loadConfig(prowConfig, jobConfig string) (*Config, error) {
stat, err := os.Stat(prowConfig)
if err != nil {
return nil, err
}
if stat.IsDir() {
return nil, fmt.Errorf("prowConfig cannot be a dir - %s", prowConfig)
}
var nc Config
if err := yamlToConfig(prowConfig, &nc); err != nil {
return nil, err
}
if err := parseProwConfig(&nc); err != nil {
return nil, err
}
// TODO(krzyzacy): temporary allow empty jobconfig
// also temporary allow job config in prow config
if jobConfig == "" {
return &nc, nil
}
stat, err = os.Stat(jobConfig)
if err != nil {
return nil, err
}
if !stat.IsDir() {
// still support a single file
var jc JobConfig
if err := yamlToConfig(jobConfig, &jc); err != nil {
return nil, err
}
if err := nc.mergeJobConfig(jc); err != nil {
return nil, err
}
return &nc, nil
}
// we need to ensure all config files have unique basenames,
// since updateconfig plugin will use basename as a key in the configmap
uniqueBasenames := sets.String{}
err = filepath.Walk(jobConfig, func(path string, info os.FileInfo, err error) error {
if err != nil {
logrus.WithError(err).Errorf("walking path %q.", path)
// bad file should not stop us from parsing the directory
return nil
}
if strings.HasPrefix(info.Name(), "..") {
// kubernetes volumes also include files we
// should not look be looking into for keys
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
if filepath.Ext(path) != ".yaml" && filepath.Ext(path) != ".yml" {
return nil
}
if info.IsDir() {
return nil
}
base := filepath.Base(path)
if uniqueBasenames.Has(base) {
return fmt.Errorf("duplicated basename is not allowed: %s", base)
}
uniqueBasenames.Insert(base)
var subConfig JobConfig
if err := yamlToConfig(path, &subConfig); err != nil {
return err
}
return nc.mergeJobConfig(subConfig)
})
if err != nil {
return nil, err
}
return &nc, nil
}
// yamlToConfig converts a yaml file into a Config object
func yamlToConfig(path string, nc interface{}) error {
b, err := ioutil.ReadFile(path)
if err != nil {
return fmt.Errorf("error reading %s: %v", path, err)
}
if err := yaml.Unmarshal(b, nc); err != nil {
return fmt.Errorf("error unmarshaling %s: %v", path, err)
}
var jc *JobConfig
switch v := nc.(type) {
case *JobConfig:
jc = v
case *Config:
jc = &v.JobConfig
}
for rep := range jc.Presubmits {
var fix func(*Presubmit)
fix = func(job *Presubmit) {
job.SourcePath = path
for i := range job.RunAfterSuccess {
fix(&job.RunAfterSuccess[i])
}
}
for i := range jc.Presubmits[rep] {
fix(&jc.Presubmits[rep][i])
}
}
for rep := range jc.Postsubmits {
var fix func(*Postsubmit)
fix = func(job *Postsubmit) {
job.SourcePath = path
for i := range job.RunAfterSuccess {
fix(&job.RunAfterSuccess[i])
}
}
for i := range jc.Postsubmits[rep] {
fix(&jc.Postsubmits[rep][i])
}
}
var fix func(*Periodic)
fix = func(job *Periodic) {
job.SourcePath = path
for i := range job.RunAfterSuccess {
fix(&job.RunAfterSuccess[i])
}
}
for i := range jc.Periodics {
fix(&jc.Periodics[i])
}
return nil
}
// mergeConfig merges two JobConfig together
// It will try to merge:
// - Presubmits
// - Postsubmits
// - Periodics
// - PodPresets
func (c *Config) mergeJobConfig(jc JobConfig) error {
// Merge everything
// *** Presets ***
c.Presets = append(c.Presets, jc.Presets...)
// validate no duplicated presets
validLabels := map[string]string{}
for _, preset := range c.Presets {
for label, val := range preset.Labels {
if _, ok := validLabels[label]; ok {
return fmt.Errorf("duplicated preset label : %s", label)
}
validLabels[label] = val
}
}
// *** Periodics ***
c.Periodics = append(c.Periodics, jc.Periodics...)
// *** Presubmits ***
if c.Presubmits == nil {
c.Presubmits = make(map[string][]Presubmit)
}
for repo, jobs := range jc.Presubmits {
c.Presubmits[repo] = append(c.Presubmits[repo], jobs...)
}
// *** Postsubmits ***
if c.Postsubmits == nil {
c.Postsubmits = make(map[string][]Postsubmit)
}
for repo, jobs := range jc.Postsubmits {
c.Postsubmits[repo] = append(c.Postsubmits[repo], jobs...)
}
return nil
}
func setPresubmitDecorationDefaults(c *Config, ps *Presubmit) {
if ps.Decorate {
ps.DecorationConfig = ps.DecorationConfig.ApplyDefault(c.Plank.DefaultDecorationConfig)
}
for i := range ps.RunAfterSuccess {
setPresubmitDecorationDefaults(c, &ps.RunAfterSuccess[i])
}
}
func setPostsubmitDecorationDefaults(c *Config, ps *Postsubmit) {
if ps.Decorate {
ps.DecorationConfig = ps.DecorationConfig.ApplyDefault(c.Plank.DefaultDecorationConfig)
}
for i := range ps.RunAfterSuccess {
setPostsubmitDecorationDefaults(c, &ps.RunAfterSuccess[i])
}
}
func setPeriodicDecorationDefaults(c *Config, ps *Periodic) {
if ps.Decorate {
ps.DecorationConfig = ps.DecorationConfig.ApplyDefault(c.Plank.DefaultDecorationConfig)
}
for i := range ps.RunAfterSuccess {
setPeriodicDecorationDefaults(c, &ps.RunAfterSuccess[i])
}
}
// finalizeJobConfig mutates and fixes entries for jobspecs
func (c *Config) finalizeJobConfig() error {
if c.decorationRequested() {
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 {
setPresubmitDecorationDefaults(c, &vs[i])
}
}
for _, js := range c.Postsubmits {
for i := range js {
setPostsubmitDecorationDefaults(c, &js[i])
}
}
for i := range c.Periodics {
setPeriodicDecorationDefaults(c, &c.Periodics[i])
}
}
// Ensure that regexes are valid and set defaults.
for _, vs := range c.Presubmits {
c.defaultPresubmitFields(vs)
if err := SetPresubmitRegexes(vs); err != nil {
return fmt.Errorf("could not set regex: %v", err)
}
}
for _, js := range c.Postsubmits {
c.defaultPostsubmitFields(js)
if err := SetPostsubmitRegexes(js); err != nil {
return fmt.Errorf("could not set regex: %v", err)
}
}
c.defaultPeriodicFields(c.Periodics)
for _, v := range c.AllPresubmits(nil) {
if err := resolvePresets(v.Name, v.Labels, v.Spec, c.Presets); err != nil {
return err
}
}
for _, v := range c.AllPostsubmits(nil) {
if err := resolvePresets(v.Name, v.Labels, v.Spec, c.Presets); err != nil {
return err
}
}
for _, v := range c.AllPeriodics() {
if err := resolvePresets(v.Name, v.Labels, v.Spec, c.Presets); err != nil {
return err
}
}
return nil
}
// validateComponentConfig validates the infrastructure component configuration
func (c *Config) validateComponentConfig() error {
if _, err := url.Parse(c.Plank.JobURLPrefix); c.Plank.JobURLPrefix != "" && err != nil {
return fmt.Errorf("plank declares an invalid job URL prefix %q: %v", c.Plank.JobURLPrefix, err)
}
return nil
}
var jobNameRegex = regexp.MustCompile(`^[A-Za-z0-9-._]+$`)
func validateJobBase(v JobBase, jobType kube.ProwJobType, podNamespace string) error {
if !jobNameRegex.MatchString(v.Name) {
return fmt.Errorf("name: must match regex %q", jobNameRegex.String())
}
// Ensure max_concurrency is non-negative.
if v.MaxConcurrency < 0 {
return fmt.Errorf("max_concurrency: %d must be a non-negative number", v.MaxConcurrency)
}
if err := validateAgent(v, podNamespace); err != nil {
return err
}
if err := validatePodSpec(jobType, v.Spec); err != nil {
return err
}
if err := validateLabels(v.Labels); err != nil {
return err
}
if v.Spec == nil || len(v.Spec.Containers) == 0 {
return nil // knative-build and jenkins jobs have no spec
}
return validateDecoration(v.Spec.Containers[0], v.DecorationConfig)
}
// validateJobConfig validates if all the jobspecs/presets are valid
// if you are mutating the jobs, please add it to finalizeJobConfig above
func (c *Config) validateJobConfig() 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 listPresubmits(jobs) {
repoJobName := orgRepoJobName{repo, job.Name}
for _, existingJob := range validPresubmits[repoJobName] {
if existingJob.Brancher.Intersects(job.Brancher) {
return fmt.Errorf("duplicated presubmit job: %s", job.Name)
}
}
validPresubmits[repoJobName] = append(validPresubmits[repoJobName], job)
}
}
for _, v := range c.AllPresubmits(nil) {
if err := validateJobBase(v.JobBase, prowjobv1.PresubmitJob, c.PodNamespace); err != nil {
return fmt.Errorf("invalid presubmit job %s: %v", v.Name, err)
}
if err := validateTriggering(v); 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 listPostsubmits(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 _, j := range c.AllPostsubmits(nil) {
if err := validateJobBase(j.JobBase, prowjobv1.PostsubmitJob, c.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.AllPeriodics() {
if validPeriodics.Has(p.Name) {
return fmt.Errorf("duplicated periodic job : %s", p.Name)
}
validPeriodics.Insert(p.Name)
if err := validateJobBase(p.JobBase, prowjobv1.PeriodicJob, c.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 j, p := range c.Periodics {
if p.Cron != "" && p.Interval != "" {
return fmt.Errorf("cron and interval cannot be both set in periodic %s", p.Name)
} else if p.Cron == "" && p.Interval == "" {
return fmt.Errorf("cron and interval cannot be both empty in periodic %s", p.Name)
} else 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 {
d, err := time.ParseDuration(c.Periodics[j].Interval)
if err != nil {
return fmt.Errorf("cannot parse duration for %s: %v", c.Periodics[j].Name, err)
}
c.Periodics[j].interval = d
}
}
return nil
}
func parseProwConfig(c *Config) error {
if err := ValidateController(&c.Plank.Controller); err != nil {
return fmt.Errorf("validating plank config: %v", err)
}
if c.Plank.PodPendingTimeoutString == "" {
c.Plank.PodPendingTimeout = 24 * time.Hour
} else {
podPendingTimeout, err := time.ParseDuration(c.Plank.PodPendingTimeoutString)
if err != nil {
return fmt.Errorf("cannot parse duration for plank.pod_pending_timeout: %v", err)
}
c.Plank.PodPendingTimeout = podPendingTimeout
}
if c.Gerrit.TickIntervalString == "" {
c.Gerrit.TickInterval = time.Minute
} else {
tickInterval, err := time.ParseDuration(c.Gerrit.TickIntervalString)
if err != nil {
return fmt.Errorf("cannot parse duration for c.gerrit.tick_interval: %v", err)
}
c.Gerrit.TickInterval = tickInterval
}
if c.Gerrit.RateLimit == 0 {
c.Gerrit.RateLimit = 5
}
for i := range c.JenkinsOperators {
if err := ValidateController(&c.JenkinsOperators[i].Controller); err != nil {
return fmt.Errorf("validating jenkins_operators config: %v", err)
}
sel, err := labels.Parse(c.JenkinsOperators[i].LabelSelectorString)
if err != nil {
return fmt.Errorf("invalid jenkins_operators.label_selector option: %v", err)
}
c.JenkinsOperators[i].LabelSelector = sel
// TODO: Invalidate overlapping selectors more
if len(c.JenkinsOperators) > 1 && c.JenkinsOperators[i].LabelSelectorString == "" {
return errors.New("selector overlap: cannot use an empty label_selector with multiple selectors")
}
if len(c.JenkinsOperators) == 1 && c.JenkinsOperators[0].LabelSelectorString != "" {
return errors.New("label_selector is invalid when used for a single jenkins-operator")
}
}
for i, agentToTmpl := range c.Deck.ExternalAgentLogs {
urlTemplate, err := template.New(agentToTmpl.Agent).Parse(agentToTmpl.URLTemplateString)
if err != nil {
return fmt.Errorf("parsing template for agent %q: %v", agentToTmpl.Agent, err)
}
c.Deck.ExternalAgentLogs[i].URLTemplate = urlTemplate
// we need to validate selectors used by deck since these are not
// sent to the api server.
s, err := labels.Parse(c.Deck.ExternalAgentLogs[i].SelectorString)
if err != nil {
return fmt.Errorf("error parsing selector %q: %v", c.Deck.ExternalAgentLogs[i].SelectorString, err)
}
c.Deck.ExternalAgentLogs[i].Selector = s
}
if c.Deck.TideUpdatePeriodString == "" {
c.Deck.TideUpdatePeriod = time.Second * 10
} else {
period, err := time.ParseDuration(c.Deck.TideUpdatePeriodString)
if err != nil {
return fmt.Errorf("cannot parse duration for deck.tide_update_period: %v", err)
}
c.Deck.TideUpdatePeriod = period
}
if c.Deck.Spyglass.SizeLimit == 0 {
c.Deck.Spyglass.SizeLimit = 100e6
} else if c.Deck.Spyglass.SizeLimit <= 0 {
return fmt.Errorf("invalid value for deck.spyglass.size_limit, must be >=0")
}
c.Deck.Spyglass.RegexCache = make(map[string]*regexp.Regexp)
for k := range c.Deck.Spyglass.Viewers {
r, err := regexp.Compile(k)
if err != nil {
return fmt.Errorf("cannot compile regexp %s, err: %v", k, err)
}
c.Deck.Spyglass.RegexCache[k] = r
}
// Map old viewer names to the new ones for backwards compatibility.
// TODO(Katharine, #10274): remove this, eventually.
oldViewers := map[string]string{
"build-log-viewer": "buildlog",
"metadata-viewer": "metadata",
"junit-viewer": "junit",
}
for re, viewers := range c.Deck.Spyglass.Viewers {
for i, v := range viewers {
if rename, ok := oldViewers[v]; ok {
c.Deck.Spyglass.Viewers[re][i] = rename
}
}
}
if c.PushGateway.IntervalString == "" {
c.PushGateway.Interval = time.Minute
} else {
interval, err := time.ParseDuration(c.PushGateway.IntervalString)
if err != nil {
return fmt.Errorf("cannot parse duration for push_gateway.interval: %v", err)
}
c.PushGateway.Interval = interval
}
if c.Sinker.ResyncPeriodString == "" {
c.Sinker.ResyncPeriod = time.Hour
} else {
resyncPeriod, err := time.ParseDuration(c.Sinker.ResyncPeriodString)
if err != nil {
return fmt.Errorf("cannot parse duration for sinker.resync_period: %v", err)
}
c.Sinker.ResyncPeriod = resyncPeriod
}
if c.Sinker.MaxProwJobAgeString == "" {
c.Sinker.MaxProwJobAge = 7 * 24 * time.Hour
} else {
maxProwJobAge, err := time.ParseDuration(c.Sinker.MaxProwJobAgeString)
if err != nil {
return fmt.Errorf("cannot parse duration for max_prowjob_age: %v", err)
}
c.Sinker.MaxProwJobAge = maxProwJobAge
}
if c.Sinker.MaxPodAgeString == "" {
c.Sinker.MaxPodAge = 24 * time.Hour
} else {
maxPodAge, err := time.ParseDuration(c.Sinker.MaxPodAgeString)
if err != nil {
return fmt.Errorf("cannot parse duration for max_pod_age: %v", err)
}
c.Sinker.MaxPodAge = maxPodAge
}
if c.Tide.SyncPeriodString == "" {
c.Tide.SyncPeriod = time.Minute
} else {
period, err := time.ParseDuration(c.Tide.SyncPeriodString)
if err != nil {
return fmt.Errorf("cannot parse duration for tide.sync_period: %v", err)
}
c.Tide.SyncPeriod = period
}
if c.Tide.StatusUpdatePeriodString == "" {
c.Tide.StatusUpdatePeriod = c.Tide.SyncPeriod
} else {
period, err := time.ParseDuration(c.Tide.StatusUpdatePeriodString)
if err != nil {
return fmt.Errorf("cannot parse duration for tide.status_update_period: %v", err)
}
c.Tide.StatusUpdatePeriod = period
}
if c.Tide.MaxGoroutines == 0 {
c.Tide.MaxGoroutines = 20
}
if c.Tide.MaxGoroutines <= 0 {
return fmt.Errorf("tide has invalid max_goroutines (%d), it needs to be a positive number", c.Tide.MaxGoroutines)
}
for name, method := range c.Tide.MergeType {
if method != github.MergeMerge &&
method != github.MergeRebase &&
method != github.MergeSquash {
return fmt.Errorf("merge type %q for %s is not a valid type", method, name)
}
}
for i, tq := range c.Tide.Queries {
if err := tq.Validate(); err != nil {
return fmt.Errorf("tide query (index %d) is invalid: %v", i, err)
}
}
if c.ProwJobNamespace == "" {
c.ProwJobNamespace = "default"
}
if c.PodNamespace == "" {
c.PodNamespace = "default"
}
if c.LogLevel == "" {
c.LogLevel = "info"
}
lvl, err := logrus.ParseLevel(c.LogLevel)
if err != nil {
return err
}
logrus.SetLevel(lvl)
return nil
}
func (c *JobConfig) 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
}
func validateLabels(labels map[string]string) error {
for label, value := range labels {
for _, prowLabel := range decorate.Labels() {
if label == prowLabel {
return fmt.Errorf("label %s is reserved for decoration", label)
}
}
if errs := validation.IsQualifiedName(label); len(errs) != 0 {
return fmt.Errorf("invalid label %s: %v", label, errs)
}
if errs := validation.IsValidLabelValue(labels[label]); len(errs) != 0 {
return fmt.Errorf("label %s has invalid value %s: %v", label, value, errs)
}
}
return nil
}
func validateAgent(v JobBase, podNamespace string) error {
k := string(prowjobv1.KubernetesAgent)
b := string(prowjobv1.KnativeBuildAgent)
j := string(prowjobv1.JenkinsAgent)
agents := sets.NewString(k, b, j)
agent := v.Agent
switch {
case !agents.Has(agent):
return fmt.Errorf("agent must be one of %s (found %q)", strings.Join(agents.List(), ", "), agent)
case v.Spec != nil && agent != k:
return fmt.Errorf("job specs require agent: %s (found %q)", k, agent)
case agent == k && v.Spec == nil:
return errors.New("kubernetes jobs require a spec")
case v.BuildSpec != nil && agent != b:
return fmt.Errorf("job build_specs require agent: %s (found %q)", b, agent)
case agent == b && v.BuildSpec == nil:
return errors.New("knative-build jobs require a build_spec")
case v.DecorationConfig != nil && agent != k && agent != b:
// TODO(fejta): only source decoration supported...
return fmt.Errorf("decoration requires agent: %s or %s (found %q)", k, b, agent)
case v.ErrorOnEviction && agent != k:
return fmt.Errorf("error_on_eviction only applies to agent: %s (found %q)", k, agent)
case v.Namespace == nil || *v.Namespace == "":
return fmt.Errorf("failed to default namespace")
case *v.Namespace != podNamespace && agent != b:
// TODO(fejta): update plank to allow this (depends on client change)
return fmt.Errorf("namespace customization requires agent: %s (found %q)", b, agent)
}
return nil
}