-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
73 lines (60 loc) · 1.94 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 (
"crypto/tls"
"flag"
"fmt"
"net/http"
"os"
"github.com/spf13/cobra"
"k8s.io/klog/v2"
"github.com/surajssd/validate-secrets/pkg/webhook"
)
var rootCmd = &cobra.Command{
Use: "validate-secrets",
Short: "Starts a HTTP server, useful for testing MutatingAdmissionWebhook and ValidatingAdmissionWebhook",
Long: `Starts a HTTP server, useful for testing MutatingAdmissionWebhook and ValidatingAdmissionWebhook.
After deploying it to Kubernetes cluster, the Administrator needs to create a ValidatingWebhookConfiguration
in the Kubernetes cluster to register remote webhook admission controllers.`,
// Args: cobra.MaximumNArgs(0),
Run: run,
}
var (
certFile string
keyFile string
port int
)
func init() {
rootCmd.Flags().StringVar(&certFile, "tls-cert-file", "/etc/webhook/cert.pem",
"File containing the default x509 Certificate for HTTPS. (CA cert, if any, concatenated after server cert).")
rootCmd.Flags().StringVar(&keyFile, "tls-private-key-file", "/etc/webhook/key.pem",
"File containing the default x509 private key matching --tls-cert-file.")
rootCmd.Flags().IntVar(&port, "port", 8443,
"Secure port that the webhook listens on")
fs := flag.NewFlagSet("", flag.PanicOnError)
klog.InitFlags(fs)
rootCmd.Flags().AddGoFlagSet(fs)
}
func run(cmd *cobra.Command, args []string) {
sCert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
klog.Fatal(err)
}
http.HandleFunc("/validate", webhook.ValidateSecretAccess)
// TODO: Run this on a HTTP port, so readyness can be setup
http.HandleFunc("/readyz", func(w http.ResponseWriter, req *http.Request) { w.Write([]byte("ok")) })
server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
TLSConfig: &tls.Config{Certificates: []tls.Certificate{sCert}},
}
klog.Info("Starting Server")
err = server.ListenAndServeTLS("", "")
if err != nil {
panic(err)
}
klog.Flush()
}
func main() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}