-
Notifications
You must be signed in to change notification settings - Fork 30
/
main.go
74 lines (60 loc) · 1.89 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
package main
import (
"encoding/json"
"flag"
"log"
"os"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
)
var (
ignoreNormal = flag.Bool("ignore-normal", false, "ignore events of type 'Normal' to reduce noise")
)
func main() {
flag.Parse()
loggerApplication := log.New(os.Stderr, "", log.LstdFlags)
loggerEvent := log.New(os.Stdout, "", 0)
// Using First sample from https://pkg.go.dev/k8s.io/client-go/tools/clientcmd to automatically deal with environment variables and default file paths
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
// if you want to change the loading rules (which files in which order), you can do so here
configOverrides := &clientcmd.ConfigOverrides{}
// if you want to change override values or bind them to flags, there are methods to help you
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
config, err := kubeConfig.ClientConfig()
if err != nil {
loggerApplication.Panicln(err.Error())
}
// Note that this *should* automatically sanitize sensitive fields
loggerApplication.Println("Using configuration:", config.String())
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
loggerApplication.Panicln(err.Error())
}
watchlist := cache.NewListWatchFromClient(
clientset.CoreV1().RESTClient(),
"events",
corev1.NamespaceAll,
fields.Everything(),
)
_, controller := cache.NewInformer(
watchlist,
&corev1.Event{},
0,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if (*ignoreNormal && obj.(*corev1.Event).Type == corev1.EventTypeNormal) {
return
}
j, _ := json.Marshal(obj)
loggerEvent.Printf("%s\n", string(j))
},
},
)
stop := make(chan struct{})
defer close(stop)
go controller.Run(stop)
select {}
}