-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
71 lines (58 loc) · 1.91 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
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"k8s.io/klog"
"k8sWebhookPractice/pkg"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
)
func main() {
var parameters pkg.TLSServerParameters
// get command line parameters
// 都会使用默认值
flag.StringVar(¶meters.CertFile, "tlsCertFile", "/etc/webhook/certs/tls.crt", "File containing the x509 Certificate for HTTPS.")
flag.StringVar(¶meters.KeyFile, "tlsKeyFile", "/etc/webhook/certs/tls.key", "File containing the x509 private key to --tlsCertFile.")
flag.Parse()
klog.Info(fmt.Sprintf("port=%d, cert-file=%s, key-file=%s", parameters.Port, parameters.CertFile, parameters.KeyFile))
file, err := tls.LoadX509KeyPair(parameters.CertFile, parameters.KeyFile)
if err != nil {
klog.Errorf("Fail to load tls file %s", err)
}
tlsServer := &pkg.TLSServer{
Server: &http.Server{
Addr: fmt.Sprintf(":%v", os.Getenv("PORT")),
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{file},
},
},
WhiteOrBlock: os.Getenv("WhITE_OR_BLOCK"),
WhiteListRegistries: strings.Split(os.Getenv("WHITELIST_REGISTRIES"), ","),
BlackListRegistries: strings.Split(os.Getenv("BLACKLIST_REGISTRIES"), ","),
MutateObject: os.Getenv("MUTATE_OBJECT"),
}
mux := http.NewServeMux()
mux.HandleFunc("/validate", tlsServer.Serve)
mux.HandleFunc("/mutate", tlsServer.Serve)
tlsServer.Server.Handler = mux
// 启动https server
go func() {
if err := tlsServer.Server.ListenAndServeTLS("", ""); err != nil {
klog.Errorf("Fail to listen and serve webhook server: %v", err)
}
}()
klog.Info("Server start!!")
// 优雅关闭
stopC := make(chan os.Signal, 1)
signal.Notify(stopC, syscall.SIGINT, syscall.SIGTERM)
<-stopC
klog.Infof("Got OS shutdown signal, shutting down webhook server gracefully...")
if err := tlsServer.Server.Shutdown(context.Background()); err != nil {
klog.Errorf("HTTP server Shutdown: %v", err)
}
}