-
Notifications
You must be signed in to change notification settings - Fork 7
/
job_informer.go
73 lines (63 loc) · 1.93 KB
/
job_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
package webui
import (
"context"
"strings"
"time"
lhv1alpha1 "github.com/jenkins-x/lighthouse/pkg/apis/lighthouse/v1alpha1"
lhclientset "github.com/jenkins-x/lighthouse/pkg/client/clientset/versioned"
lhinformers "github.com/jenkins-x/lighthouse/pkg/client/informers/externalversions"
"github.com/sirupsen/logrus"
)
type JobInformer struct {
LHClient *lhclientset.Clientset
Namespace string
ResyncInterval time.Duration
Store *Store
Logger *logrus.Logger
}
func (i *JobInformer) Start(ctx context.Context) {
informerFactory := lhinformers.NewSharedInformerFactoryWithOptions(
i.LHClient,
i.ResyncInterval,
lhinformers.WithNamespace(i.Namespace),
)
informerFactory.Lighthouse().V1alpha1().LighthouseJobs().Informer().AddEventHandler(i)
informerFactory.Start(ctx.Done())
}
func (i *JobInformer) OnAdd(obj interface{}, _ bool) {
job, ok := obj.(*lhv1alpha1.LighthouseJob)
if !ok {
return
}
i.indexJob(job, "index")
}
func (i *JobInformer) OnUpdate(oldObj, newObj interface{}) {
job, ok := newObj.(*lhv1alpha1.LighthouseJob)
if !ok {
return
}
i.indexJob(job, "re-index")
}
func (i *JobInformer) OnDelete(obj interface{}) {
job, ok := obj.(*lhv1alpha1.LighthouseJob)
if !ok {
return
}
if i.Logger != nil && i.Logger.IsLevelEnabled(logrus.DebugLevel) {
i.Logger.WithField("Job", job.Name).Debug("Deleting Job")
}
err := i.Store.DeleteJob(job.Name)
if err != nil && i.Logger != nil {
i.Logger.WithError(err).WithField("Job", job.Name).Error("failed to delete Job")
}
}
func (i *JobInformer) indexJob(job *lhv1alpha1.LighthouseJob, operation string) {
if i.Logger != nil && i.Logger.IsLevelEnabled(logrus.DebugLevel) {
i.Logger.WithField("Job", job.Name).Debugf("%sing Job", strings.Title(operation))
}
j := JobFromLighthouseJob(job)
err := i.Store.AddJob(j)
if err != nil && i.Logger != nil {
i.Logger.WithError(err).WithField("Job", job.Name).Errorf("failed to %s Job", operation)
}
}