-
Notifications
You must be signed in to change notification settings - Fork 786
/
controller_commitstatus.go
509 lines (461 loc) · 15.3 KB
/
controller_commitstatus.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
package controller
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/kube/naming"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/prow/config"
"github.com/jenkins-x/jx/v2/pkg/gits"
"github.com/jenkins-x/jx/v2/pkg/prow"
"k8s.io/client-go/kubernetes"
"github.com/jenkins-x/jx/v2/pkg/extensions"
"github.com/pkg/errors"
"github.com/jenkins-x/jx/v2/pkg/builds"
corev1 "k8s.io/api/core/v1"
jenkinsv1client "github.com/jenkins-x/jx-api/pkg/client/clientset/versioned"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/tools/cache"
"github.com/jenkins-x/jx-logging/pkg/log"
jenkinsv1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/spf13/cobra"
)
// ControllerCommitStatusOptions the options for the controller
type ControllerCommitStatusOptions struct {
ControllerOptions
}
// NewCmdControllerCommitStatus creates a command object for the "create" command
func NewCmdControllerCommitStatus(commonOpts *opts.CommonOptions) *cobra.Command {
options := &ControllerCommitStatusOptions{
ControllerOptions: ControllerOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "commitstatus",
Short: "Updates commit status",
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
return cmd
}
// Run implements this command
func (o *ControllerCommitStatusOptions) Run() error {
// Always run in batch mode as a controller is never run interactively
o.BatchMode = true
jxClient, ns, err := o.JXClientAndDevNamespace()
if err != nil {
return err
}
kubeClient, _, err := o.KubeClientAndDevNamespace()
if err != nil {
return err
}
apisClient, err := o.ApiExtensionsClient()
if err != nil {
return err
}
err = kube.RegisterCommitStatusCRD(apisClient)
if err != nil {
return err
}
err = kube.RegisterPipelineActivityCRD(apisClient)
if err != nil {
return err
}
commitstatusListWatch := cache.NewListWatchFromClient(jxClient.JenkinsV1().RESTClient(), "commitstatuses", ns, fields.Everything())
kube.SortListWatchByName(commitstatusListWatch)
_, commitstatusController := cache.NewInformer(
commitstatusListWatch,
&jenkinsv1.CommitStatus{},
time.Minute*10,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
o.onCommitStatusObj(obj, jxClient, ns)
},
UpdateFunc: func(oldObj, newObj interface{}) {
o.onCommitStatusObj(newObj, jxClient, ns)
},
DeleteFunc: func(obj interface{}) {
},
},
)
stop := make(chan struct{})
go commitstatusController.Run(stop)
podListWatch := cache.NewListWatchFromClient(kubeClient.CoreV1().RESTClient(), "pods", ns, fields.Everything())
kube.SortListWatchByName(podListWatch)
_, podWatch := cache.NewInformer(
podListWatch,
&corev1.Pod{},
time.Minute*10,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
o.onPodObj(obj, jxClient, kubeClient, ns)
},
UpdateFunc: func(oldObj, newObj interface{}) {
o.onPodObj(newObj, jxClient, kubeClient, ns)
},
DeleteFunc: func(obj interface{}) {
},
},
)
stop = make(chan struct{})
podWatch.Run(stop)
if err != nil {
return err
}
return nil
}
func (o *ControllerCommitStatusOptions) onCommitStatusObj(obj interface{}, jxClient jenkinsv1client.Interface, ns string) {
check, ok := obj.(*jenkinsv1.CommitStatus)
if !ok {
log.Logger().Fatalf("commit status controller: unexpected type %v", obj)
} else {
err := o.onCommitStatus(check, jxClient, ns)
if err != nil {
log.Logger().Fatalf("commit status controller: %v", err)
}
}
}
func (o *ControllerCommitStatusOptions) onCommitStatus(check *jenkinsv1.CommitStatus, jxClient jenkinsv1client.Interface, ns string) error {
groupedBySha := make(map[string][]jenkinsv1.CommitStatusDetails, 0)
for _, v := range check.Spec.Items {
if _, ok := groupedBySha[v.Commit.SHA]; !ok {
groupedBySha[v.Commit.SHA] = make([]jenkinsv1.CommitStatusDetails, 0)
}
groupedBySha[v.Commit.SHA] = append(groupedBySha[v.Commit.SHA], v)
}
for _, vs := range groupedBySha {
var last jenkinsv1.CommitStatusDetails
for _, v := range vs {
lastBuildNumber, err := strconv.Atoi(getBuildNumber(last.PipelineActivity.Name))
if err != nil {
return err
}
buildNumber, err := strconv.Atoi(getBuildNumber(v.PipelineActivity.Name))
if err != nil {
return err
}
if lastBuildNumber < buildNumber {
last = v
}
}
err := o.update(&last, jxClient, ns)
if err != nil {
gitProvider, gitRepoInfo, err1 := o.getGitProvider(last.Commit.GitURL)
if err1 != nil {
return err1
}
_, err1 = extensions.NotifyCommitStatus(last.Commit, "error", "", "Internal Error performing commit status updates", "", last.Context, gitProvider, gitRepoInfo)
if err1 != nil {
return err
}
return err
}
}
return nil
}
func (o *ControllerCommitStatusOptions) onPodObj(obj interface{}, jxClient jenkinsv1client.Interface, kubeClient kubernetes.Interface, ns string) {
check, ok := obj.(*corev1.Pod)
if !ok {
log.Logger().Fatalf("pod watcher: unexpected type %v", obj)
} else {
err := o.onPod(check, jxClient, kubeClient, ns)
if err != nil {
log.Logger().Fatalf("pod watcher: %v", err)
}
}
}
func (o *ControllerCommitStatusOptions) onPod(pod *corev1.Pod, jxClient jenkinsv1client.Interface, kubeClient kubernetes.Interface, ns string) error {
if pod != nil {
labels := pod.Labels
if labels != nil {
buildName := labels[builds.LabelBuildName]
if buildName == "" {
buildName = labels[builds.LabelOldBuildName]
}
if buildName == "" {
buildName = labels[builds.LabelPipelineRunName]
}
if buildName != "" {
org := ""
repo := ""
pullRequest := ""
pullPullSha := ""
pullBaseSha := ""
buildNumber := ""
jxBuildNumber := ""
buildId := ""
sourceUrl := ""
branch := ""
containers, _, _ := kube.GetContainersWithStatusAndIsInit(pod)
for _, container := range containers {
for _, e := range container.Env {
switch e.Name {
case "REPO_OWNER":
org = e.Value
case "REPO_NAME":
repo = e.Value
case "PULL_NUMBER":
pullRequest = fmt.Sprintf("PR-%s", e.Value)
case "PULL_PULL_SHA":
pullPullSha = e.Value
case "PULL_BASE_SHA":
pullBaseSha = e.Value
case "JX_BUILD_NUMBER":
jxBuildNumber = e.Value
case "BUILD_NUMBER":
buildNumber = e.Value
case "BUILD_ID":
buildId = e.Value
case "SOURCE_URL":
sourceUrl = e.Value
case "PULL_BASE_REF":
branch = e.Value
}
}
}
sha := pullBaseSha
if pullRequest == "PR-" {
pullRequest = ""
} else {
sha = pullPullSha
branch = pullRequest
}
// if BUILD_ID is set, use it, otherwise if JX_BUILD_NUMBER is set, use it, otherwise use BUILD_NUMBER
if jxBuildNumber != "" {
buildNumber = jxBuildNumber
}
if buildId != "" {
buildNumber = buildId
}
pipelineActName := naming.ToValidName(fmt.Sprintf("%s-%s-%s-%s", org, repo, branch, buildNumber))
// PLM TODO This is a bit of hack, we need a working build controller
// Try to add the lastCommitSha and gitUrl to the PipelineActivity
act, err := jxClient.JenkinsV1().PipelineActivities(ns).Get(pipelineActName, metav1.GetOptions{})
if err != nil {
// An error just means the activity doesn't exist yet
log.Logger().Debugf("pod watcher: Unable to find PipelineActivity for %s", pipelineActName)
} else {
act.Spec.LastCommitSHA = sha
act.Spec.GitURL = sourceUrl
act.Spec.GitOwner = org
log.Logger().Debugf("pod watcher: Adding lastCommitSha: %s and gitUrl: %s to %s", act.Spec.LastCommitSHA, act.Spec.GitURL, pipelineActName)
_, err := jxClient.JenkinsV1().PipelineActivities(ns).PatchUpdate(act)
if err != nil {
// We can safely return this error as it will just get logged
return err
}
}
if org != "" && repo != "" && buildNumber != "" && (pullBaseSha != "" || pullPullSha != "") {
log.Logger().Debugf("pod watcher: build pod: %s, org: %s, repo: %s, buildNumber: %s, pullBaseSha: %s, pullPullSha: %s, pullRequest: %s, sourceUrl: %s", pod.Name, org, repo, buildNumber, pullBaseSha, pullPullSha, pullRequest, sourceUrl)
if sha == "" {
log.Logger().Warnf("pod watcher: No sha on %s, not upserting commit status", pod.Name)
} else {
prow := prow.Options{
KubeClient: kubeClient,
NS: ns,
}
prowConfig, _, err := prow.GetProwConfig()
if err != nil {
return errors.Wrap(err, "getting prow config")
}
contexts, err := config.GetBranchProtectionContexts(org, repo, prowConfig)
if err != nil {
return err
}
log.Logger().Debugf("pod watcher: Using contexts %v", contexts)
for _, ctx := range contexts {
if pullRequest != "" {
name := naming.ToValidName(fmt.Sprintf("%s-%s-%s-%s", org, repo, branch, ctx))
err = o.UpsertCommitStatusCheck(name, pipelineActName, sourceUrl, sha, pullRequest, ctx, pod.Status.Phase, jxClient, ns)
if err != nil {
return err
}
}
}
}
}
}
}
}
return nil
}
func (o *ControllerCommitStatusOptions) UpsertCommitStatusCheck(name string, pipelineActName string, url string, sha string, pullRequest string, context string, phase corev1.PodPhase, jxClient jenkinsv1client.Interface, ns string) error {
if name != "" {
status, err := jxClient.JenkinsV1().CommitStatuses(ns).Get(name, metav1.GetOptions{})
create := false
insert := false
actRef := jenkinsv1.ResourceReference{}
if err != nil {
create = true
} else {
log.Logger().Infof("pod watcher: commit status already exists for %s", name)
}
// Create the activity reference
act, err := jxClient.JenkinsV1().PipelineActivities(ns).Get(pipelineActName, metav1.GetOptions{})
if err == nil {
actRef.Name = act.Name
actRef.Kind = act.Kind
actRef.UID = act.UID
actRef.APIVersion = act.APIVersion
}
possibleStatusDetails := make([]int, 0)
for i, v := range status.Spec.Items {
if v.Commit.SHA == sha && v.PipelineActivity.Name == pipelineActName {
possibleStatusDetails = append(possibleStatusDetails, i)
}
}
statusDetails := jenkinsv1.CommitStatusDetails{}
log.Logger().Debugf("pod watcher: Discovered possible status details %v", possibleStatusDetails)
if len(possibleStatusDetails) == 1 {
log.Logger().Debugf("CommitStatus %s for pipeline %s already exists", name, pipelineActName)
} else if len(possibleStatusDetails) == 0 {
insert = true
} else {
return fmt.Errorf("More than %d status detail for sha %s, should 1 or 0, found %v", len(possibleStatusDetails), sha, possibleStatusDetails)
}
if create || insert {
// This is not the same pipeline activity the status was created for,
// or there is no existing status, so we make a new one
statusDetails = jenkinsv1.CommitStatusDetails{
Checked: false,
Commit: jenkinsv1.CommitStatusCommitReference{
GitURL: url,
PullRequest: pullRequest,
SHA: sha,
},
PipelineActivity: actRef,
Context: context,
}
}
if create {
log.Logger().Infof("pod watcher: Creating commit status for pipeline activity %s", pipelineActName)
status = &jenkinsv1.CommitStatus{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{
"lastCommitSha": sha,
},
},
Spec: jenkinsv1.CommitStatusSpec{
Items: []jenkinsv1.CommitStatusDetails{
statusDetails,
},
},
}
_, err := jxClient.JenkinsV1().CommitStatuses(ns).Create(status)
if err != nil {
return err
}
} else if insert {
status.Spec.Items = append(status.Spec.Items, statusDetails)
log.Logger().Infof("pod watcher: Adding commit status for pipeline activity %s", pipelineActName)
_, err := jxClient.JenkinsV1().CommitStatuses(ns).PatchUpdate(status)
if err != nil {
return err
}
} else {
log.Logger().Debugf("pod watcher: Not updating or creating pipeline activity %s", pipelineActName)
}
} else {
return errors.New("commit status controller: Must supply name")
}
return nil
}
func (o *ControllerCommitStatusOptions) update(statusDetails *jenkinsv1.CommitStatusDetails, jxClient jenkinsv1client.Interface, ns string) error {
gitProvider, gitRepoInfo, err := o.getGitProvider(statusDetails.Commit.GitURL)
if err != nil {
return err
}
pass := false
if statusDetails.Checked {
var commentBuilder strings.Builder
pass = true
for _, c := range statusDetails.Items {
if !c.Pass {
pass = false
fmt.Fprintf(&commentBuilder, "%s | %s | %s | TODO | `/test this`\n", c.Name, c.Description, statusDetails.Commit.SHA)
}
}
if pass {
_, err := extensions.NotifyCommitStatus(statusDetails.Commit, "success", "", "Completed successfully", "", statusDetails.Context, gitProvider, gitRepoInfo)
if err != nil {
return err
}
} else {
comment := fmt.Sprintf(
"The following commit statusDetails checks **failed**, say `/retest` to rerun them all:\n"+
"\n"+
"Name | Description | Commit | Details | Rerun command\n"+
"--- | --- | --- | --- | --- \n"+
"%s\n"+
"<details>\n"+
"\n"+
"Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).\n"+
"</details>", commentBuilder.String())
_, err := extensions.NotifyCommitStatus(statusDetails.Commit, "failure", "", fmt.Sprintf("%s failed", statusDetails.Context), comment, statusDetails.Context, gitProvider, gitRepoInfo)
if err != nil {
return err
}
}
} else {
_, err = extensions.NotifyCommitStatus(statusDetails.Commit, "pending", "", fmt.Sprintf("Waiting for %s to complete", statusDetails.Context), "", statusDetails.Context, gitProvider, gitRepoInfo)
if err != nil {
return err
}
}
return nil
}
func (o *ControllerCommitStatusOptions) getGitProvider(url string) (gits.GitProvider, *gits.GitRepository, error) {
// TODO This is an epic hack to get the git stuff working
gitInfo, err := gits.ParseGitURL(url)
if err != nil {
return nil, nil, err
}
authConfigSvc, err := o.GitAuthConfigService()
if err != nil {
return nil, nil, err
}
gitKind, err := o.GitServerKind(gitInfo)
if err != nil {
return nil, nil, err
}
for _, server := range authConfigSvc.Config().Servers {
if server.Kind == gitKind && len(server.Users) >= 1 {
// Just grab the first user for now
username := server.Users[0].Username
apiToken := server.Users[0].ApiToken
err = os.Setenv("GIT_USERNAME", username)
if err != nil {
return nil, nil, err
}
err = os.Setenv("GIT_API_TOKEN", apiToken)
if err != nil {
return nil, nil, err
}
break
}
}
return o.CreateGitProviderForURLWithoutKind(url)
}
func getBuildNumber(pipelineActName string) string {
if pipelineActName == "" {
return "-1"
}
pipelineParts := strings.Split(pipelineActName, "-")
if len(pipelineParts) > 3 {
return pipelineParts[len(pipelineParts)-1]
} else {
return ""
}
}