-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
96 lines (82 loc) · 2.22 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
package main
import (
"fmt"
. "go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"os/signal"
"github.com/mitchellh/go-homedir"
// see https://www.fullstory.com/blog/connect-to-google-kubernetes-with-gcp-credentials-and-pure-golang
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // register GCP auth provider
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
func createKubernetesConfig() (*rest.Config, error) {
config, err := rest.InClusterConfig()
if err == nil {
return config, nil
}
kubeConfigPath := os.Getenv("KUBECONFIG")
if kubeConfigPath == "" {
kubeConfigPath, err = homedir.Expand("~/.kube/config")
if err != nil {
return nil, err
}
}
config, err = clientcmd.BuildConfigFromFlags("", kubeConfigPath)
return config, err
}
func createRootLogger() (*SugaredLogger, error) {
config := NewDevelopmentConfig()
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
logger, err := config.Build()
if err != nil {
return nil, err
}
return logger.Sugar(), nil
}
func main() {
logger, err := createRootLogger()
if err != nil {
fmt.Printf("error creating logger! %v", err)
return
}
defer func(logger *SugaredLogger) {
// ignore errors - see https://github.com/uber-go/zap/issues/880
_ = logger.Sync()
}(logger)
cfg, err := createKubernetesConfig()
if err != nil {
logger.Fatal("could not create kubernetes config", Error(err))
}
watcher, err := NewWatcher(cfg, logger)
if err != nil {
logger.Fatal("could not create watcher", Error(err))
}
// TODO: fix url
//webhook := NewWebhookHandler(logger, "http://127.0.0.1")
//err = watcher.AddHandler(webhook, []string{
// "deployments.v1.apps",
// "events.v1.events.k8s.io",
//})
//if err != nil {
// logger.Fatal("could not add webhook listener", Error(err))
//}
//
cache := NewCacheHandler(logger)
cache.Start()
defer cache.Stop()
err = watcher.AddHandler(cache, []string{
"deployments.v1.apps",
"events.v1.events.k8s.io",
"pods.v1.", // we need the trailing dot to signify that this is in the "" group
})
if err != nil {
logger.Fatal("could not add cache listener", Error(err))
}
watcher.Start()
sigCh := make(chan os.Signal, 0)
signal.Notify(sigCh, os.Kill, os.Interrupt)
<- sigCh
watcher.Stop()
}