-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (128 loc) · 4.2 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
144
145
146
147
package main
import (
"context"
"crypto/tls"
"net"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/alecthomas/kingpin"
"github.com/povilasv/prommod"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
log "github.com/sirupsen/logrus"
)
func main() {
app := kingpin.New("resource-requests-admission-controller", "Validates Statefulset,Deployment,Daemoneset,Pod resource requests and limits")
app.Version(version.Print("resource-requests-admission-controller"))
app.HelpFlag.Short('h')
certFile := app.Flag("tls-cert-file", "").Envar("TLS_CERT_FILE").Required().String()
keyFile := app.Flag("tls-private-key-file", "").Envar("TLS_KEY_FILE").Required().String()
configFile := app.Flag("config-file", "File path to the config").Envar("CONFIG_FILE").Required().String()
refreshInterval := app.Flag("refresh-interval", "Refresh interval in if no file change happens.").Envar("REFRESH_INTERVAL").Default("5m").Duration()
logLevel := app.Flag("log.level", "Log level.").Envar("LOG_LEVEL").
Default("info").Enum("error", "warn", "info", "debug")
logFormat := app.Flag("log.format", "Log format.").Envar("LOG_FORMAT").
Default("text").Enum("text", "json")
addr := app.Flag("addr", "Server address which will receive AdmissionReview requests.").Envar("ADDR").Default("0.0.0.0:8443").String()
opsAddr := app.Flag("ops-addr", "Server address which will serve prometheus metrics.").Envar("PROM_ADDR").Default("0.0.0.0:8090").String()
kingpin.MustParse(app.Parse(os.Args[1:]))
prometheus.MustRegister(version.NewCollector("rrac"))
prometheus.MustRegister(prommod.NewCollector("rrac"))
switch strings.ToLower(*logLevel) {
case "error":
log.SetLevel(log.ErrorLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "debug":
log.SetLevel(log.DebugLevel)
}
switch strings.ToLower(*logFormat) {
case "json":
log.SetFormatter(&log.JSONFormatter{})
case "text":
log.SetFormatter(&log.TextFormatter{DisableColors: true})
}
log.SetOutput(os.Stdout)
configer, err := NewConfigurer(*configFile, *refreshInterval)
if err != nil {
log.WithError(err).Fatalf("unable to load config file: %s", *configFile)
}
defer configer.Close()
rra := New(configer)
cert, err := tls.LoadX509KeyPair(*certFile, *keyFile)
if err != nil {
log.WithError(err).Fatal("unable to load certificates")
}
_, port, err := net.SplitHostPort(*addr)
if err != nil {
log.WithError(err).Fatal("unable to parse address")
}
hc, err := NewHealthChecker(port)
if err != nil {
log.WithError(err).Fatal("unable to create healthcheck")
}
http.Handle("/metrics", promhttp.Handler())
http.Handle("/health", hc)
opsServer := &http.Server{
Addr: *opsAddr,
Handler: http.DefaultServeMux,
}
go func() {
opsErr := opsServer.ListenAndServe()
switch opsErr {
case http.ErrServerClosed:
log.WithError(opsErr).Warn("ops server shutdown")
default:
log.WithError(opsErr).Panic("unable to start ops http server")
}
}()
defer func() {
err := opsServer.Shutdown(context.Background())
if err != nil {
log.WithError(err).Error("unable to shutdown ops http server")
}
}()
log.Infof("app started,listening on: %s, prometheus on: %s", *addr, *opsAddr)
server := &http.Server{
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
Handler: http.TimeoutHandler(&AdmissionControllerServer{
AdmissionController: rra,
Decoder: codecs.UniversalDeserializer(),
}, 20*time.Second, "Service Unavailable"),
Addr: *addr,
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
},
}
go func() {
err := server.ListenAndServeTLS("", "")
switch err {
case http.ErrServerClosed:
log.WithError(err).Warn("ops server shutdown")
default:
log.WithError(err).Panic("unable to start http server")
}
}()
defer func() {
err := server.Shutdown(context.Background())
if err != nil {
log.WithError(err).Error("unable to shutdown http server")
}
}()
waitForShutdown()
}
func waitForShutdown() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
<-sigChan
log.Warn("shutting down")
}