forked from wfarr/k8s-event-logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (107 loc) · 2.97 KB
/
main.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
package main
import (
"flag"
"os"
"os/signal"
"syscall"
"time"
log "github.com/Sirupsen/logrus"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/pkg/api"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/pkg/fields"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
// Only required to authenticate against GKE clusters
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)
func main() {
kubeconfig := flag.String("kubeconfig", "", "Path to a kube config. Only required if out-of-cluster.")
flag.Parse()
if os.Getenv("BUGSNAG_API_KEY") != "" {
log.Info("Configuring bugsnag logger")
if err := configureBugsnag(os.Getenv("BUGSNAG_API_KEY"), os.Getenv("BUGSNAG_RELEASE_STAGE")); err != nil {
log.WithError(err).Fatal("Exiting...")
}
}
config, err := buildConfig(*kubeconfig)
if err != nil {
log.WithError(err).Fatal("Exiting...")
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.WithError(err).Fatal("Exiting...")
}
stop := make(chan struct{}, 1)
source := cache.NewListWatchFromClient(
clientset.Core().RESTClient(),
"events",
api.NamespaceAll,
fields.Everything())
create := func(obj interface{}) {
event := obj.(*v1.Event)
if os.Getenv("BUGSNAG_API_KEY") != "" {
log.WithFields(log.Fields{"event-uid": event.UID}).Debug("Sending event to bugsnag")
if err := sendEventToBugsnag(event); err != nil {
log.WithError(err)
}
}
if os.Getenv("STATSD_URL") != "" {
log.WithFields(log.Fields{"event-uid": event.UID}).Debug("Sending event to statsd")
if err := sendEventToDatadog(event); err != nil {
log.WithError(err)
}
}
if err := sendEventToSTDOUT(event); err != nil {
log.WithError(err)
}
}
update := func(old interface{}, obj interface{}) {
event := obj.(*v1.Event)
if os.Getenv("BUGSNAG_API_KEY") != "" {
log.WithFields(log.Fields{"event-uid": event.UID}).Debug("Sending event to bugsnag")
if err := sendEventToBugsnag(event); err != nil {
log.WithError(err)
}
}
if os.Getenv("STATSD_URL") != "" {
log.WithFields(log.Fields{"event-uid": event.UID}).Debug("Sending event to statsd")
if err := sendEventToDatadog(event); err != nil {
log.WithError(err)
}
}
if err := sendEventToSTDOUT(event); err != nil {
log.WithError(err)
}
}
_, controller := cache.NewInformer(
source,
&v1.Event{},
time.Second*0,
cache.ResourceEventHandlerFuncs{
AddFunc: create,
UpdateFunc: update,
})
go controller.Run(stop)
log.Info("Starting informer...")
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
log.Info("registered signal handler")
for {
select {
case s := <-signals:
log.Infof("received signal %#v, exiting...\n", s)
close(stop)
os.Exit(0)
}
}
}
func buildConfig(kubeconfig string) (*rest.Config, error) {
if kubeconfig != "" {
return clientcmd.BuildConfigFromFlags("", kubeconfig)
}
return rest.InClusterConfig()
}
func create(obj interface{}) {
}