-
Notifications
You must be signed in to change notification settings - Fork 35
/
informer.go
98 lines (86 loc) · 3 KB
/
informer.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
package visualizer
import (
"context"
"strings"
"time"
jenkinsv1 "github.com/jenkins-x/jx-api/v4/pkg/apis/jenkins.io/v1"
jxclientset "github.com/jenkins-x/jx-api/v4/pkg/client/clientset/versioned"
informers "github.com/jenkins-x/jx-api/v4/pkg/client/informers/externalversions"
"github.com/sirupsen/logrus"
"k8s.io/client-go/tools/cache"
)
type Informer struct {
JXClient *jxclientset.Clientset
Namespace string
ResyncInterval time.Duration
Store *Store
RunningPipelines *RunningPipelines
Logger *logrus.Logger
}
func (i *Informer) Start(ctx context.Context) {
informerFactory := informers.NewSharedInformerFactoryWithOptions(
i.JXClient,
i.ResyncInterval,
informers.WithNamespace(i.Namespace),
)
informerFactory.Jenkins().V1().PipelineActivities().Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
pa, ok := obj.(*jenkinsv1.PipelineActivity)
if !ok {
return
}
i.indexPipelineActivity(pa, "index")
i.RunningPipelines.Add(pa)
},
UpdateFunc: func(old, new interface{}) {
pa, ok := new.(*jenkinsv1.PipelineActivity)
if !ok {
return
}
i.indexPipelineActivity(pa, "re-index")
i.RunningPipelines.Add(pa)
},
DeleteFunc: func(obj interface{}) {
pa, ok := obj.(*jenkinsv1.PipelineActivity)
if !ok {
return
}
if i.Logger != nil && i.Logger.IsLevelEnabled(logrus.DebugLevel) {
i.Logger.WithField("PipelineActivity", pa.Name).Debug("Deleting PipelineActivity")
}
err := i.Store.Delete(pa.Name)
if err != nil && i.Logger != nil {
i.Logger.WithError(err).WithField("PipelineActivity", pa.Name).Error("failed to delete PipelineActivity")
}
i.RunningPipelines.Add(pa)
},
})
informerFactory.Start(ctx.Done())
}
func (i *Informer) indexPipelineActivity(pa *jenkinsv1.PipelineActivity, operation string) {
if isJenkinsPipelineActivity(pa) {
if i.Logger != nil && i.Logger.IsLevelEnabled(logrus.DebugLevel) {
i.Logger.WithField("PipelineActivity", pa.Name).Debug("Ignoring PipelineActivity created by Jenkins")
}
return
}
if i.Logger != nil && i.Logger.IsLevelEnabled(logrus.DebugLevel) {
i.Logger.WithField("PipelineActivity", pa.Name).Debugf("%sing new PipelineActivity", strings.Title(operation))
}
p := PipelineFromPipelineActivity(pa)
err := i.Store.Add(p)
if err != nil && i.Logger != nil {
i.Logger.WithError(err).WithField("PipelineActivity", pa.Name).Errorf("failed to %s new PipelineActivity", operation)
}
}
// isJenkinsPipelineActivity returns true if the given PipelineActivity has been created by Jenkins
// see https://github.com/jenkinsci/jx-resources-plugin/blob/master/src/main/java/org/jenkinsci/plugins/jx/resources/BuildSyncRunListener.java#L106
func isJenkinsPipelineActivity(pa *jenkinsv1.PipelineActivity) bool {
if strings.Contains(pa.Spec.BuildURL, "/blue/organizations/jenkins/") {
return true
}
if strings.Contains(pa.Spec.BuildLogsURL, "/job/") && strings.HasSuffix(pa.Spec.BuildLogsURL, "/console") {
return true
}
return false
}