forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprow.go
204 lines (181 loc) · 7.33 KB
/
prow.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
/*
Copyright 2019 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 main
import (
"fmt"
"path"
"strconv"
"strings"
"github.com/GoogleCloudPlatform/testgrid/config"
"github.com/GoogleCloudPlatform/testgrid/config/yamlcfg"
configpb "github.com/GoogleCloudPlatform/testgrid/pb/config"
prowapi "k8s.io/test-infra/prow/apis/prowjobs/v1"
prowConfig "k8s.io/test-infra/prow/config"
"k8s.io/test-infra/prow/pod-utils/downwardapi"
prowGCS "k8s.io/test-infra/prow/pod-utils/gcs"
)
const testgridCreateTestGroupAnnotation = "testgrid-create-test-group"
const testgridDashboardsAnnotation = "testgrid-dashboards"
const testgridTabNameAnnotation = "testgrid-tab-name"
const testgridEmailAnnotation = "testgrid-alert-email"
const testgridNumColumnsRecentAnnotation = "testgrid-num-columns-recent"
const testgridAlertStaleResultsHoursAnnotation = "testgrid-alert-stale-results-hours"
const testgridNumFailuresToAlertAnnotation = "testgrid-num-failures-to-alert"
const descriptionAnnotation = "description"
const minPresubmitNumColumnsRecent = 20
// Talk to @michelle192837 if you're thinking about adding more of these!
func applySingleProwjobAnnotations(c *configpb.Configuration, pc *prowConfig.Config, j prowConfig.JobBase, jobType prowapi.ProwJobType, repo string, dc *yamlcfg.DefaultConfiguration) error {
tabName := j.Name
testGroupName := j.Name
description := j.Name
mustMakeGroup := j.Annotations[testgridCreateTestGroupAnnotation] == "true"
mustNotMakeGroup := j.Annotations[testgridCreateTestGroupAnnotation] == "false"
dashboards, addToDashboards := j.Annotations[testgridDashboardsAnnotation]
mightMakeGroup := (mustMakeGroup || addToDashboards || jobType != prowapi.PresubmitJob) && !mustNotMakeGroup
var testGroup *configpb.TestGroup
if mightMakeGroup {
if testGroup = config.FindTestGroup(testGroupName, c); testGroup != nil {
if mustMakeGroup {
return fmt.Errorf("test group %q already exists", testGroupName)
}
} else {
var prefix string
if j.DecorationConfig != nil && j.DecorationConfig.GCSConfiguration != nil {
prefix = path.Join(j.DecorationConfig.GCSConfiguration.Bucket, j.DecorationConfig.GCSConfiguration.PathPrefix)
} else if pc.Plank.GetDefaultDecorationConfigs(repo) != nil && pc.Plank.GetDefaultDecorationConfigs(repo).GCSConfiguration != nil {
prefix = path.Join(pc.Plank.GetDefaultDecorationConfigs(repo).GCSConfiguration.Bucket, pc.Plank.GetDefaultDecorationConfigs(repo).GCSConfiguration.PathPrefix)
} else {
return fmt.Errorf("job %s: couldn't figure out a default decoration config", j.Name)
}
testGroup = &configpb.TestGroup{
Name: testGroupName,
GcsPrefix: path.Join(prefix, prowGCS.RootForSpec(&downwardapi.JobSpec{Job: j.Name, Type: jobType})),
}
if dc != nil {
yamlcfg.ReconcileTestGroup(testGroup, dc.DefaultTestGroup)
}
c.TestGroups = append(c.TestGroups, testGroup)
}
} else {
testGroup = config.FindTestGroup(testGroupName, c)
}
if testGroup == nil {
for _, a := range []string{testgridNumColumnsRecentAnnotation, testgridAlertStaleResultsHoursAnnotation,
testgridNumFailuresToAlertAnnotation, testgridTabNameAnnotation, testgridEmailAnnotation} {
_, ok := j.Annotations[a]
if ok {
return fmt.Errorf("no testgroup exists for job %q, but annotation %q implies one should exist", j.Name, a)
}
}
// exit early: with no test group, there's nothing else for us to usefully do with the job.
return nil
}
if ncr, ok := j.Annotations[testgridNumColumnsRecentAnnotation]; ok {
ncrInt, err := strconv.ParseInt(ncr, 10, 32)
if err != nil {
return fmt.Errorf("%s value %q is not a valid integer", testgridNumColumnsRecentAnnotation, ncr)
}
testGroup.NumColumnsRecent = int32(ncrInt)
} else if jobType == prowapi.PresubmitJob && testGroup.NumColumnsRecent < minPresubmitNumColumnsRecent {
testGroup.NumColumnsRecent = minPresubmitNumColumnsRecent
}
if srh, ok := j.Annotations[testgridAlertStaleResultsHoursAnnotation]; ok {
srhInt, err := strconv.ParseInt(srh, 10, 32)
if err != nil {
return fmt.Errorf("%s value %q is not a valid integer", testgridAlertStaleResultsHoursAnnotation, srh)
}
testGroup.AlertStaleResultsHours = int32(srhInt)
}
if nfta, ok := j.Annotations[testgridNumFailuresToAlertAnnotation]; ok {
nftaInt, err := strconv.ParseInt(nfta, 10, 32)
if err != nil {
return fmt.Errorf("%s value %q is not a valid integer", testgridNumFailuresToAlertAnnotation, nfta)
}
testGroup.NumFailuresToAlert = int32(nftaInt)
}
if tn, ok := j.Annotations[testgridTabNameAnnotation]; ok {
tabName = tn
}
if d := j.Annotations[descriptionAnnotation]; d != "" {
description = d
}
if addToDashboards {
firstDashboard := true
for _, dashboardName := range strings.Split(dashboards, ",") {
dashboardName = strings.TrimSpace(dashboardName)
d := config.FindDashboard(dashboardName, c)
if d == nil {
return fmt.Errorf("couldn't find dashboard %q for job %q", dashboardName, j.Name)
}
if repo == "" {
if len(j.ExtraRefs) > 0 {
repo = fmt.Sprintf("%s/%s", j.ExtraRefs[0].Org, j.ExtraRefs[0].Repo)
}
}
var codeSearchLinkTemplate, openBugLinkTemplate *configpb.LinkTemplate
if repo != "" {
codeSearchLinkTemplate = &configpb.LinkTemplate{
Url: fmt.Sprintf("https://github.com/%s/compare/<start-custom-0>...<end-custom-0>", repo),
}
openBugLinkTemplate = &configpb.LinkTemplate{
Url: fmt.Sprintf("https://github.com/%s/issues/", repo),
}
}
dt := &configpb.DashboardTab{
Name: tabName,
TestGroupName: testGroupName,
Description: description,
CodeSearchUrlTemplate: codeSearchLinkTemplate,
OpenBugTemplate: openBugLinkTemplate,
}
if firstDashboard {
firstDashboard = false
if emails, ok := j.Annotations[testgridEmailAnnotation]; ok {
dt.AlertOptions = &configpb.DashboardTabAlertOptions{AlertMailToAddresses: emails}
}
}
if dc != nil {
yamlcfg.ReconcileDashboardTab(dt, dc.DefaultDashboardTab)
}
d.DashboardTab = append(d.DashboardTab, dt)
}
}
return nil
}
func applyProwjobAnnotations(c *configpb.Configuration, reconcile *yamlcfg.DefaultConfiguration, prowConfigAgent *prowConfig.Agent) error {
pc := prowConfigAgent.Config()
if pc == nil {
return nil
}
jobs := prowConfigAgent.Config().JobConfig
for _, j := range jobs.AllPeriodics() {
if err := applySingleProwjobAnnotations(c, pc, j.JobBase, prowapi.PeriodicJob, "", reconcile); err != nil {
return err
}
}
for repo, js := range jobs.PostsubmitsStatic {
for _, j := range js {
if err := applySingleProwjobAnnotations(c, pc, j.JobBase, prowapi.PostsubmitJob, repo, reconcile); err != nil {
return err
}
}
}
for repo, js := range jobs.PresubmitsStatic {
for _, j := range js {
if err := applySingleProwjobAnnotations(c, pc, j.JobBase, prowapi.PresubmitJob, repo, reconcile); err != nil {
return err
}
}
}
return nil
}