generated from wuhan005/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
52 lines (43 loc) · 1.35 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
package main
import (
"flag"
"net/http"
"github.com/flamego/flamego"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
kwhhttp "github.com/slok/kubewebhook/v2/pkg/http"
"github.com/wuhan005/k8s-image-replacer/internal/conf"
"github.com/wuhan005/k8s-image-replacer/internal/webhook"
)
func main() {
configFile := flag.String("config", "config.yaml", "configuration file")
flag.Parse()
if err := conf.Init(*configFile); err != nil {
logrus.WithError(err).Fatal("Failed to init config")
}
wh, err := webhook.NewHandler()
if err != nil {
logrus.WithError(err).Fatal("Failed to create webhook handler")
}
whHandler, err := kwhhttp.HandlerFor(kwhhttp.HandlerConfig{Webhook: wh})
if err != nil {
logrus.WithError(err).Fatal("Failed to create webhook HTTP handler")
}
f := flamego.Classic()
f.Any("/webhook", func(ctx flamego.Context) {
whHandler.ServeHTTP(ctx.ResponseWriter(), ctx.Request().Request)
})
f.Any("/metrics", promhttp.Handler())
f.Get("/healthz")
tlsCertFile := conf.ImageReplacer.TlsCrtFile
if tlsCertFile == "" {
tlsCertFile = "crt/tls.crt"
}
tlsKeyFile := conf.ImageReplacer.TlsKeyFile
if tlsKeyFile == "" {
tlsKeyFile = "crt/tls.key"
}
if err := http.ListenAndServeTLS(":443", tlsCertFile, tlsKeyFile, f); err != nil {
logrus.WithError(err).Fatal("Failed to start server")
}
}