-
Notifications
You must be signed in to change notification settings - Fork 249
/
reconciler.go
294 lines (266 loc) · 8.77 KB
/
reconciler.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
package main
import (
"context"
"fmt"
"strings"
"sync"
"time"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/test-infra/prow/apis/prowjobs/v1"
"k8s.io/test-infra/prow/config"
"k8s.io/test-infra/prow/github"
"k8s.io/test-infra/prow/kube"
"sigs.k8s.io/controller-runtime/pkg/builder"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
const (
retention = 24 * time.Hour
)
type minimalGhClient interface {
GetPullRequest(org, repo string, number int) (*github.PullRequest, error)
CreateComment(org, repo string, number int, comment string) error
GetPullRequestChanges(org string, repo string, number int) ([]github.PullRequestChange, error)
}
type pullRequest struct {
closed bool
checkTime time.Time
}
type closedPRsCache struct {
prs map[string]pullRequest
m sync.Mutex
ghc minimalGhClient
clearTime time.Time
}
func composePRIdentifier(refs *v1.Refs) string {
return fmt.Sprintf("%s/%s/%d", refs.Org, refs.Repo, refs.Pulls[0].Number)
}
// isPRClosed quieries either github or short-term cache to determine if PR is closed. Draft PRs are
// also quialified as closed due to potential, unexpected side effects
func (c *closedPRsCache) isPRClosed(refs *v1.Refs) (bool, error) {
id := composePRIdentifier(refs)
c.m.Lock()
defer c.m.Unlock()
c.clearCache()
pr, ok := c.prs[id]
if ok && (time.Since(pr.checkTime) < 5*time.Minute || pr.closed) {
return pr.closed, nil
}
ghPr, err := c.ghc.GetPullRequest(refs.Org, refs.Repo, refs.Pulls[0].Number)
if err != nil {
return false, fmt.Errorf("error getting pull request: %w", err)
}
c.prs[id] = pullRequest{closed: ghPr.State != github.PullRequestStateOpen || ghPr.Draft, checkTime: time.Now()}
return ghPr.State != github.PullRequestStateOpen || ghPr.Draft, nil
}
func (c *closedPRsCache) clearCache() {
if time.Since(c.clearTime) < retention {
return
}
for k, v := range c.prs {
if time.Since(v.checkTime) >= retention {
delete(c.prs, k)
}
}
c.clearTime = time.Now()
}
func composeKey(refs *v1.Refs) string {
return fmt.Sprintf("%s/%s/%d/%s/%s", refs.Org, refs.Repo, refs.Pulls[0].Number, refs.BaseRef, refs.Pulls[0].SHA)
}
type reconciler struct {
pjclientset ctrlruntimeclient.Client
lister ctrlruntimeclient.Reader
configDataProvider *ConfigDataProvider
ghc minimalGhClient
closedPRsCache closedPRsCache
ids sync.Map
logger *logrus.Entry
watcher *watcher
}
func NewReconciler(
mgr manager.Manager,
configDataProvider *ConfigDataProvider,
ghc github.Client,
logger *logrus.Entry,
w *watcher,
) (*reconciler, error) {
reconciler := &reconciler{
pjclientset: mgr.GetClient(),
lister: mgr.GetCache(),
configDataProvider: configDataProvider,
ghc: ghc,
ids: sync.Map{},
logger: logger,
watcher: w,
closedPRsCache: closedPRsCache{
prs: map[string]pullRequest{},
m: sync.Mutex{},
ghc: ghc,
clearTime: time.Now(),
},
}
if err := builder.
ControllerManagedBy(mgr).
Named("pipeline-controller").
For(&v1.ProwJob{}).
WithOptions(controller.Options{MaxConcurrentReconciles: 3}).
Complete(reconciler); err != nil {
return nil, fmt.Errorf("failed to construct controller: %w", err)
}
return reconciler, nil
}
func (r *reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
log := r.logger.WithField("key", req.String()).WithField("prowjob", req.Name)
err := r.reconcile(ctx, req)
if err != nil {
log.WithError(err).Error("reconciliation failed")
}
return reconcile.Result{}, err
}
func (r *reconciler) cleanOldIds(interval time.Duration) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for range ticker.C {
r.ids.Range(func(key, value interface{}) bool {
if time.Since(value.(time.Time)) >= interval {
r.ids.Delete(key)
}
return true
})
}
}
func (r *reconciler) reconcile(ctx context.Context, req reconcile.Request) error {
var pj v1.ProwJob
if err := r.pjclientset.Get(ctx, req.NamespacedName, &pj); err != nil {
if errors.IsNotFound(err) {
return nil
}
return fmt.Errorf("failed to get prowjob %s: %w", req.String(), err)
}
if pj.Spec.Refs == nil || pj.Spec.Type != v1.PresubmitJob {
return nil
}
presubmits := r.configDataProvider.GetPresubmits(pj.Spec.Refs.Org + "/" + pj.Spec.Refs.Repo)
if len(presubmits.protected) == 0 && len(presubmits.alwaysRequired) == 0 &&
len(presubmits.conditionallyRequired) == 0 && len(presubmits.pipelineConditionallyRequired) == 0 {
return nil
}
currentCfg := r.watcher.getConfig()
repos, ok := currentCfg[pj.Spec.Refs.Org]
if !ok || !(repos.Len() == 0 || repos.Has(pj.Spec.Refs.Repo)) {
return nil
}
status, err := r.reportSuccessOnPR(ctx, &pj, presubmits)
if err != nil || !status {
return err
}
comment := "/test remaining-required"
testContexts, overrideContexts, err := r.acquireConditionalContexts(&pj, presubmits.pipelineConditionallyRequired)
if err != nil {
r.ids.Delete(composeKey(pj.Spec.Refs))
return err
}
if testContexts != "" {
comment += "\n\nScheduling tests matching the `pipeline_run_if_changed` parameter:" + testContexts
}
if overrideContexts != "" {
comment += "\n\nOverriding unmatched contexts:\n" + "/override " + overrideContexts
}
if err := r.ghc.CreateComment(pj.Spec.Refs.Org, pj.Spec.Refs.Repo, pj.Spec.Refs.Pulls[0].Number, comment); err != nil {
r.ids.Delete(composeKey(pj.Spec.Refs))
return err
}
return nil
}
func (r *reconciler) acquireConditionalContexts(pj *v1.ProwJob, pipelineConditionallyRequired []config.Presubmit) (string, string, error) {
repoBaseRef := pj.Spec.Refs.Repo + "-" + pj.Spec.Refs.BaseRef
var overrideCommands string
var testCommands string
if len(pipelineConditionallyRequired) != 0 {
cfp := config.NewGitHubDeferredChangedFilesProvider(r.ghc, pj.Spec.Refs.Org, pj.Spec.Refs.Repo, pj.Spec.Refs.Pulls[0].Number)
for _, presubmit := range pipelineConditionallyRequired {
if !strings.Contains(presubmit.Name, repoBaseRef) {
continue
}
if run, ok := presubmit.Annotations["pipeline_run_if_changed"]; ok && run != "" {
psList := []config.Presubmit{presubmit}
psList[0].RegexpChangeMatcher = config.RegexpChangeMatcher{RunIfChanged: run}
if err := config.SetPresubmitRegexes(psList); err != nil {
r.ids.Delete(composeKey(pj.Spec.Refs))
return "", "", err
}
_, shouldRun, err := psList[0].RegexpChangeMatcher.ShouldRun(cfp)
if err != nil {
r.ids.Delete(composeKey(pj.Spec.Refs))
return "", "", err
}
if shouldRun {
testCommands += "\n" + presubmit.RerunCommand
continue
}
if !presubmit.Optional {
overrideCommands += " " + presubmit.Context
}
}
}
}
return testCommands, overrideCommands, nil
}
func (r *reconciler) reportSuccessOnPR(ctx context.Context, pj *v1.ProwJob, presubmits presubmitTests) (bool, error) {
if pj == nil || pj.Spec.Refs == nil || len(pj.Spec.Refs.Pulls) != 1 {
return false, nil
}
selector := map[string]string{}
for _, l := range []string{kube.OrgLabel, kube.RepoLabel, kube.PullLabel, kube.BaseRefLabel} {
selector[l] = pj.ObjectMeta.Labels[l]
}
var pjs v1.ProwJobList
if err := r.lister.List(ctx, &pjs, ctrlruntimeclient.MatchingLabels(selector)); err != nil {
return false, fmt.Errorf("cannot list prowjob using selector %v", selector)
}
latestBatch := make(map[string]v1.ProwJob)
for _, pjob := range pjs.Items {
if pjob.Spec.Refs.Pulls[0].SHA == pj.Spec.Refs.Pulls[0].SHA {
if existing, ok := latestBatch[pjob.Spec.Job]; !ok {
latestBatch[pjob.Spec.Job] = pjob
} else if pjob.CreationTimestamp.After(existing.CreationTimestamp.Time) {
latestBatch[pjob.Spec.Job] = pjob
}
}
}
repoBaseRef := pj.Spec.Refs.Repo + "-" + pj.Spec.Refs.BaseRef
for _, presubmit := range presubmits.protected {
if !strings.Contains(presubmit, repoBaseRef) {
continue
}
if _, ok := latestBatch[presubmit]; ok {
return false, nil
}
}
for _, presubmit := range presubmits.alwaysRequired {
if !strings.Contains(presubmit, repoBaseRef) {
continue
}
if pjob, ok := latestBatch[presubmit]; !ok || (ok && pjob.Status.State != v1.SuccessState) {
return false, nil
}
}
for _, presubmit := range presubmits.conditionallyRequired {
if !strings.Contains(presubmit, repoBaseRef) {
continue
}
if pjob, ok := latestBatch[presubmit]; ok && pjob.Status.State != v1.SuccessState {
return false, nil
}
}
if closed, err := r.closedPRsCache.isPRClosed(pj.Spec.Refs); err != nil || closed {
return false, err
}
if _, loaded := r.ids.LoadOrStore(composeKey(pj.Spec.Refs), time.Now()); loaded {
return false, nil
}
return true, nil
}