-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
143 lines (119 loc) · 4.21 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
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"context"
"fmt"
"net"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/client-go/informers"
)
// See https://blog.heptio.com/straighten-out-your-kubernetes-client-go-dependencies-heptioprotip-8baeed46fe7d
// https://github.com/coreos/prometheus-operator/blob/master/pkg/prometheus/operator.go
// https://github.com/upmc-enterprises/registry-creds
// https://github.com/jbeda/tgik-controller
// https://engineering.bitnami.com/articles/a-deep-dive-into-kubernetes-controllers.html
// https://github.com/skippbox/kubewatch/
// https://github.com/aaronlevy/kube-controller-demo
// https://github.com/heptio/ark
var (
version = "NotSet"
repoBranch = "NotSet"
repoVersion = "NotSet"
)
func main() {
if err := runMain(); err != nil {
glog.Error(err.Error())
os.Exit(2)
}
}
func runMain() error {
defer glog.Flush()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
config, err := getConfig(os.Args)
if err != nil {
return errors.Wrap(err, "getConfig failed")
}
glog.Infof("Starting Version=%s Branch=%s RepoVersion=%s golang=%s\n", version, repoBranch, repoVersion, runtime.Version())
glog.Infof("Starting listener on port %d\n", config.Port)
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Port))
if err != nil {
return errors.Wrap(err, "listener failed")
}
glog.Infoln("Newing up k8s client")
k8sClient, err := newK8sClient(config.KubeConfigFilePath)
if err != nil {
return errors.Wrap(err, "newK8sClient failed")
}
glog.Infoln("Newing up ECR")
ecr := newECRClient()
glog.Infoln("Newing up shared informer factory and namesapce informer")
informersFactory := informers.NewSharedInformerFactory(k8sClient.ClientSet, config.InformersResyncInterval)
nsInformer := informersFactory.Core().V1().Namespaces()
glog.Infoln("Getting prometheus registry and gatherer - defaults")
promRegistry := prometheus.DefaultRegisterer.(*prometheus.Registry)
promGatherer := prometheus.DefaultGatherer
glog.Infoln("Newing up controller")
controller, err := newController(config, k8sClient, nsInformer.Informer(), promRegistry, ecr)
if err != nil {
return errors.Wrap(err, "newController failure")
}
glog.Infoln("Newing up diagnostic HTTP server")
srv := newDiagnosticHTTPServer(promGatherer)
glog.Infoln("Starting informers factory")
informersFactory.Start(ctx.Done())
glog.Infoln("Starting controller go routine")
go func() {
controller.Run(ctx.Done())
glog.Infoln("Controller run completed")
}()
glog.Infoln("Starting diagnostic HTTP server go routine")
// PENDING: Can I use errgroup package, see https://godoc.org/golang.org/x/sync/errgroup
go func() error {
err := srv.Serve(listener)
if err != http.ErrServerClosed {
return errors.Wrap(err, "HTTP serve failed")
}
glog.Infoln("HTTP serve completed")
return nil
}()
glog.Infoln("Starting diagnostic HTTP server graceful shutdown go routine")
// PENDING: Can I use errgroup package, see https://godoc.org/golang.org/x/sync/errgroup
go func() error {
<-ctx.Done()
glog.Infoln("Shutting down HTTP server")
if err := srv.Shutdown(context.Background()); err != nil {
return errors.Wrap(err, "HTTP server shutdown failed")
}
glog.Infoln("HTTP server shutdown completed")
return nil
}()
glog.Infoln("Waiting...")
term := make(chan os.Signal)
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
<-term
cancel()
glog.Infof("Allowing %s to shutdown\n", config.ShutdownGracePeriod)
time.Sleep(config.ShutdownGracePeriod)
glog.Infoln("Done")
return nil
}
func newDiagnosticHTTPServer(promGatherer prometheus.Gatherer) *http.Server {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.HandlerFor(promGatherer, promhttp.HandlerOpts{}))
mux.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
mux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
mux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
return &http.Server{Handler: mux}
}