-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
101 lines (80 loc) · 2.76 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
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package main
import (
"aws-signingproxy-admissioncontroller/controller"
"context"
"crypto/tls"
"flag"
"fmt"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"log"
"net/http"
"os"
signal "os/signal"
"syscall"
"time"
)
type WhSvrParameters struct {
port int // Webhook server port
certFile string // Path to the x509 HTTPS certificate
keyFile string // Path to the x509 private key matching the certFile
}
func main() {
var parameters WhSvrParameters
flag.IntVar(¶meters.port, "port", 443, "Webhook server port.")
flag.StringVar(¶meters.certFile, "tlsCertFile", "/etc/webhook/certs/cert.pem", "File containing the x509 Certificate for HTTPS.")
flag.StringVar(¶meters.keyFile, "tlsKeyFile", "/etc/webhook/certs/key.pem", "File containing the x509 private key to --tlsCertFile.")
flag.Parse()
keyPair, err := tls.LoadX509KeyPair(parameters.certFile, parameters.keyFile)
if err != nil {
fmt.Errorf("Error loading key pair: %v", err)
}
server := &http.Server{
Addr: fmt.Sprintf(":%v", parameters.port),
TLSConfig: &tls.Config{Certificates: []tls.Certificate{keyPair}},
}
client, err := newKubernetesClient()
if err != nil {
fmt.Errorf("Error creating Kubernetes client: %v", err)
}
whsvr := controller.NewWebhookServer(server, client)
mux := http.NewServeMux()
mux.HandleFunc("/mutate", whsvr.Handler)
server.Handler = mux
go func() {
if err := server.ListenAndServeTLS("", ""); err != nil {
fmt.Errorf("Error listening and serving webhook server: %v", err)
}
}()
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
<-signalChan
log.Println("Got OS shutdown signal, shutting down webhook server gracefully")
shutdownCtx, _ := context.WithTimeout(context.Background(), 10 * time.Second)
server.Shutdown(shutdownCtx)
}
func newKubernetesClient() (*kubernetes.Clientset, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, fmt.Errorf("Error initializing Kubernetes client: %v", err)
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("Error describing namespace: %v", err)
}
return client, nil
}