This repository has been archived by the owner on Feb 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
73 lines (59 loc) · 1.71 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
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/golang/glog"
"go.opencensus.io/trace"
)
func main() {
// read configuration location from command line arg
var configPath string
flag.StringVar(&configPath, "configPath", DefaultConfigPath, "Path that points to the YAML configuration for this webhook.")
flag.Parse()
// parse and validate configuration
config := Config{}
ok, err := ParseConfigFromPath(&config, configPath)
if !ok {
glog.Errorf("configuration parse failed with error: %v", err)
return
}
ok, err = config.Validate()
if !ok {
glog.Errorf("configuration validation failed with error: %v", err)
return
}
// configure global tracer
trace.ApplyConfig(trace.Config{DefaultSampler: trace.ProbabilitySampler(config.Trace.SampleRate)})
// configure certificates
pair, err := tls.LoadX509KeyPair("/etc/webhook/certs/cert.pem", "/etc/webhook/certs/key.pem")
if err != nil {
glog.Errorf("Failed to load key pair: %v", err)
}
whsvr := &WebhookServer{
server: &http.Server{
Addr: fmt.Sprintf(":%v", 443),
TLSConfig: &tls.Config{Certificates: []tls.Certificate{pair}},
},
}
mux := http.NewServeMux()
mux.HandleFunc("/mutate", whsvr.serve)
whsvr.server.Handler = mux
// begin webhook server
go func() {
if err := whsvr.server.ListenAndServeTLS("", ""); err != nil {
glog.Fatalf("Failed to listen and serve webhook server: %v", err)
}
}()
// listening OS shutdown singal
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
<-signalChan
glog.Errorf("Got OS shutdown signal, shutting down webhook server gracefully...")
whsvr.server.Shutdown(context.Background())
}