-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (61 loc) · 2.18 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
package main
import (
"flag"
"net/http"
"time"
"github.com/sirupsen/logrus"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
const (
workerProcessCount = 2
)
var version = "dev"
func main() {
var (
kubeconfig, listen, logLevel, metricsPrefix, masterURL string
rescanInterval time.Duration
)
flag.StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
flag.StringVar(&listen, "listen", ":3000", "Address to listen on for HTTP requests")
flag.StringVar(&logLevel, "log-level", "info", "Log-level to use for output")
flag.StringVar(&masterURL, "master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.")
flag.StringVar(&metricsPrefix, "metrics-prefix", "k8s_cert_monitor", "How to prefix the metrics generated by this controller")
flag.DurationVar(&rescanInterval, "rescan-interval", time.Hour, "How often to re-scan existing secrets without events")
flag.Parse()
logrusLevel, err := logrus.ParseLevel(logLevel)
if err != nil {
logrus.WithError(err).Fatal("parsing given log-level")
}
logrus.SetLevel(logrusLevel)
metricsHandler, err := newPrometheusHandler(metricsPrefix)
if err != nil {
logrus.WithError(err).Fatal("initializing prometheus metrics")
}
cfg, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfig)
if err != nil {
logrus.WithError(err).Fatal("building kubeconfig")
}
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
logrus.WithError(err).Fatal("building kubernetes clientset")
}
kubeInformerFactory := informers.NewSharedInformerFactory(kubeClient, rescanInterval)
ctrl := newController(
kubeClient,
kubeInformerFactory.Core().V1().Secrets(),
metricsHandler,
)
stopCh := make(chan struct{})
defer close(stopCh)
metricsHandler.AddHandler()
go func() {
if err := http.ListenAndServe(listen, nil); err != nil {
logrus.WithError(err).Fatal("HTTP server reported error")
}
}()
logrus.WithField("version", version).Info("cert-monitor-controller started")
kubeInformerFactory.Start(stopCh)
ctrl.Run(workerProcessCount, stopCh)
}