-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
70 lines (59 loc) · 1.64 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
package main
import (
"crypto/tls"
"fmt"
"net/http"
"github.com/spf13/cobra"
"k8s.io/klog"
"github.com/oam-dev/oam-crd-migration/converter"
)
var (
certFile string
keyFile string
port int
)
var ConversionWebhookArgs = &cobra.Command{
Use: "crd-conversion-webhook",
Args: cobra.MaximumNArgs(0),
Run: transferArgs,
}
func init() {
ConversionWebhookArgs.Flags().StringVar(&certFile, "tls-cert-file", "",
"File containing the default x509 Certificate for HTTPS. (CA cert, if any, concatenated "+
"after server cert.")
ConversionWebhookArgs.Flags().StringVar(&keyFile, "tls-private-key-file", "",
"File containing the default x509 private key matching --tls-cert-file.")
ConversionWebhookArgs.Flags().IntVar(&port, "port", 443,
"Secure port that the webhook listens on")
}
// Config contains the server (the webhook) cert and key.
type Config struct {
CertFile string
KeyFile string
}
func main() {
ConversionWebhookArgs.Execute()
}
func transferArgs(cmd *cobra.Command, args []string) {
config := Config{CertFile: certFile, KeyFile: keyFile}
http.HandleFunc("/appconfigconvert", converter.ServeAppConfigConvert)
server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
TLSConfig: configTLS(config),
}
err := server.ListenAndServeTLS("", "")
if err != nil {
panic(err)
}
}
func configTLS(config Config) *tls.Config {
sCert, err := tls.LoadX509KeyPair(config.CertFile, config.KeyFile)
if err != nil {
klog.Error(err)
}
return &tls.Config{
Certificates: []tls.Certificate{sCert},
// TODO: uses mutual tls after we agree on what cert the apiserver should use.
// ClientAuth: tls.RequireAndVerifyClientCert,
}
}