-
Notifications
You must be signed in to change notification settings - Fork 2
/
controller.go
62 lines (53 loc) · 1.67 KB
/
controller.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
package main
import (
"fmt"
"k8s.io/client-go/informers"
coreinformers "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/tools/cache"
klog "k8s.io/klog/v2"
v1 "k8s.io/api/core/v1"
)
// EventExporterController exports the kubernetes events that are added
type EventExporterController struct {
informerFactory informers.SharedInformerFactory
eventInformer coreinformers.EventInformer
}
// Run starts shared informers and waits for the shared informer cache to
// synchronize.
func (c *EventExporterController) Run(stopCh <-chan struct{}) error {
// Starts all the shared informers that have been created by the factory so far.
c.informerFactory.Start(stopCh)
// wait for the initial synchronization of the local cache.
if !cache.WaitForCacheSync(stopCh, c.eventInformer.Informer().HasSynced) {
return fmt.Errorf("failed to sync")
}
return nil
}
func (c *EventExporterController) eventAdd(obj interface{}) {
event := obj.(*v1.Event)
IncSummaryEvent(event)
switch event.Type {
case "Normal":
IncNormalEvent(event)
case "Warning":
IncWarningEvent(event)
}
}
// NewEventExporterController creates a EventExporterController
func NewEventExporterController(informerFactory informers.SharedInformerFactory) *EventExporterController {
eventInformer := informerFactory.Core().V1().Events()
c := &EventExporterController{
informerFactory: informerFactory,
eventInformer: eventInformer,
}
if _, err := eventInformer.Informer().AddEventHandler(
// Your custom resource event handlers.
cache.ResourceEventHandlerFuncs{
// Called on creation
AddFunc: c.eventAdd,
},
); err != nil {
klog.ErrorS(err, "failed to add informer event handler")
}
return c
}