1
1
package crdconversionwebhook
2
2
3
3
import (
4
+ "context"
4
5
"crypto/tls"
5
6
"fmt"
6
7
"net/http"
7
8
8
9
"github.com/spf13/cobra"
10
+ "k8s.io/klog/v2"
11
+ "sigs.k8s.io/controller-runtime/pkg/certwatcher"
12
+ "sigs.k8s.io/controller-runtime/pkg/log"
9
13
10
14
converter "github.com/openshift/console-operator/pkg/cmd/crdconversionwebhook/converter"
11
15
)
@@ -31,22 +35,63 @@ func NewConverter() *cobra.Command {
31
35
return cmd
32
36
}
33
37
34
- // Config contains the server (the webhook) cert and key.
35
- type Config struct {
36
- CertFile string
37
- KeyFile string
38
- }
39
-
40
38
func startServer () {
41
- config := Config {CertFile : certFile , KeyFile : keyFile }
39
+ ctx , cancel := context .WithCancel (context .Background ())
40
+ defer cancel ()
41
+
42
+ // Initialize klog
43
+ klog .InitFlags (nil )
44
+ log .SetLogger (klog .NewKlogr ()) // controller-runtime/log will complain if we don't set this
45
+
46
+ // Log flag values for debugging
47
+ klog .Infof ("Starting console conversion webhook server" )
48
+ klog .V (4 ).Infof ("Using flags:\n \t --tls-cert-file %s\n \t --tls-private-key-file %s\n \t --port %d" , certFile , keyFile , port )
49
+
50
+ // Initialize a new cert watcher with cert/key pair
51
+ klog .V (4 ).Infof ("Creating cert watcher" )
52
+ watcher , err := certwatcher .New (certFile , keyFile )
53
+ if err != nil {
54
+ klog .Fatalf ("Error creating cert watcher: %v" , err )
55
+ }
56
+
57
+ // Start goroutine with certwatcher running fsnotify against supplied certdir
58
+ go func () {
59
+ klog .V (4 ).Infof ("Starting cert watcher" )
60
+ if err := watcher .Start (ctx ); err != nil {
61
+ klog .Fatalf ("Cert watcher failed: %v" , err )
62
+ }
63
+ }()
42
64
43
- http .HandleFunc ("/crdconvert" , converter .ServeExampleConvert )
65
+ // Setup TLS config using GetCertficate for fetching the cert when it changes
66
+ tlsConfig := & tls.Config {
67
+ GetCertificate : watcher .GetCertificate ,
68
+ NextProtos : []string {"http/1.1" }, // Disable HTTP/2
69
+ }
70
+
71
+ // Create TLS listener
72
+ klog .V (4 ).Infof ("Creating TLS listener on port %d" , port )
73
+ listener , err := tls .Listen ("tcp" , fmt .Sprintf (":%d" , port ), tlsConfig )
74
+ if err != nil {
75
+ klog .Fatalf ("Error creating TLS listener: %v" , err )
76
+ }
77
+
78
+ // Setup handlers and server
79
+ http .HandleFunc ("/crdconvert" , converter .ServeConsolePluginConvert )
44
80
http .HandleFunc ("/readyz" , func (w http.ResponseWriter , req * http.Request ) { w .Write ([]byte ("ok" )) })
45
- clientset := getClient ()
46
- server := & http.Server {
47
- Addr : fmt .Sprintf (":%d" , port ),
48
- TLSConfig : configTLS (config , clientset ),
49
- TLSNextProto : make (map [string ]func (* http.Server , * tls.Conn , http.Handler )), // disable HTTP/2
81
+ server := & http.Server {}
82
+
83
+ // Shutdown server on context cancellation
84
+ go func () {
85
+ <- ctx .Done ()
86
+ klog .V (4 ).Info ("Shutting down server" )
87
+ if err := server .Shutdown (context .Background ()); err != nil {
88
+ klog .Fatalf ("Error shutting down server: %v" , err )
89
+ }
90
+ }()
91
+
92
+ // Serve
93
+ klog .Infof ("Serving on %s" , listener .Addr ().String ())
94
+ if err = server .Serve (listener ); err != nil && err != http .ErrServerClosed {
95
+ klog .Fatalf ("Error serving: %v" , err )
50
96
}
51
- server .ListenAndServeTLS ("" , "" )
52
97
}
0 commit comments