forked from joeholley/supergloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.go
53 lines (41 loc) · 1.25 KB
/
webhook.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
package webhook
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"github.com/solo-io/supergloo/pkg/webhook/clients"
"github.com/solo-io/go-utils/contextutils"
)
const HandlePattern = "/pods/inject-sidecar"
type AdmissionWebhookServer struct {
*http.Server
}
func NewSidecarInjectionWebhook(ctx context.Context) *AdmissionWebhookServer {
logger := contextutils.LoggerFrom(ctx)
// Initialize global client set
if err := clients.InitClientSet(ctx); err != nil {
logger.Fatalf("failed to create webhook client set: %v", err)
}
// Register component that handles sidecar injection
RegisterSidecarInjectionHandler()
// Retrieve the webhook configuration
config := getConfig()
logger.Debugf("server config is: %v", config)
// Load key pair for TLS config
keyPair, err := tls.LoadX509KeyPair(config.certFilePath, config.keyFilePath)
if err != nil {
logger.Fatalf("failed to load key pair: %v", err)
}
logger.Debug("successfully loaded key pair")
// Register request handler
mux := http.NewServeMux()
mux.HandleFunc(HandlePattern, handlePodCreation)
return &AdmissionWebhookServer{
&http.Server{
Addr: fmt.Sprintf(":%v", config.port),
TLSConfig: &tls.Config{Certificates: []tls.Certificate{keyPair}},
Handler: mux,
},
}
}