...
1
16
17 package crdconvwebhook
18
19 import (
20 "fmt"
21 "net/http"
22
23 "github.com/spf13/cobra"
24
25 "k8s.io/kubernetes/test/images/agnhost/crd-conversion-webhook/converter"
26 )
27
28 var (
29 certFile string
30 keyFile string
31 port int
32 )
33
34
35 var CmdCrdConversionWebhook = &cobra.Command{
36 Use: "crd-conversion-webhook",
37 Short: "Starts HTTP server on port 443 for testing CustomResourceConversionWebhook",
38 Long: `The subcommand tests "CustomResourceConversionWebhook".
39
40 After deploying it to Kubernetes cluster, the administrator needs to create a "CustomResourceConversion.Webhook" in Kubernetes cluster to use remote webhook for conversions.
41
42 The subcommand starts a HTTP server, listening on port 443, and creating the "/crdconvert" endpoint.`,
43 Args: cobra.MaximumNArgs(0),
44 Run: main,
45 }
46
47 func init() {
48 CmdCrdConversionWebhook.Flags().StringVar(&certFile, "tls-cert-file", "",
49 "File containing the default x509 Certificate for HTTPS. (CA cert, if any, concatenated "+
50 "after server cert.")
51 CmdCrdConversionWebhook.Flags().StringVar(&keyFile, "tls-private-key-file", "",
52 "File containing the default x509 private key matching --tls-cert-file.")
53 CmdCrdConversionWebhook.Flags().IntVar(&port, "port", 443,
54 "Secure port that the webhook listens on")
55 }
56
57
58 type Config struct {
59 CertFile string
60 KeyFile string
61 }
62
63 func main(cmd *cobra.Command, args []string) {
64 config := Config{CertFile: certFile, KeyFile: keyFile}
65
66 http.HandleFunc("/crdconvert", converter.ServeExampleConvert)
67 http.HandleFunc("/readyz", func(w http.ResponseWriter, req *http.Request) { w.Write([]byte("ok")) })
68 clientset := getClient()
69 server := &http.Server{
70 Addr: fmt.Sprintf(":%d", port),
71 TLSConfig: configTLS(config, clientset),
72 }
73 err := server.ListenAndServeTLS("", "")
74 if err != nil {
75 panic(err)
76 }
77 }
78
View as plain text