@@ -14,7 +14,10 @@ import (
1414 "github.com/gorilla/mux"
1515 "github.com/sirupsen/logrus"
1616 "gopkg.in/yaml.v2"
17+ v1 "k8s.io/api/core/v1"
1718 "k8s.io/apiserver/pkg/server/dynamiccertificates"
19+ "k8s.io/client-go/kubernetes/scheme"
20+ "k8s.io/client-go/tools/record"
1821)
1922
2023var log = logrus .WithField ("module" , "server" )
@@ -27,7 +30,6 @@ type Config struct {
2730 StaticPath string
2831 ConfigPath string
2932 PluginConfigPath string
30- LogLevel string
3133}
3234
3335type PluginConfig struct {
@@ -60,33 +62,42 @@ func Start(cfg *Config) {
6062
6163 tlsEnabled := cfg .CertFile != "" && cfg .PrivateKeyFile != ""
6264 if tlsEnabled {
65+ ctx := context .Background ()
6366 // Build and run the controller which reloads the certificate and key
6467 // files whenever they change.
6568 certKeyPair , err := dynamiccertificates .NewDynamicServingContentFromFiles ("serving-cert" , cfg .CertFile , cfg .PrivateKeyFile )
6669 if err != nil {
67- logrus .WithError (err ).Fatal ("unable to create TLS controller" )
70+ log .WithError (err ).Fatal ("unable to create TLS controller" )
71+ }
72+
73+ if err := certKeyPair .RunOnce (ctx ); err != nil {
74+ log .WithError (err ).Fatal ("failed to initialize cert/key content" )
6875 }
76+
77+ eventBroadcaster := record .NewBroadcaster ()
78+ eventBroadcaster .StartLogging (func (format string , args ... interface {}) {
79+ log .Infof (format , args ... )
80+ })
81+
6982 ctrl := dynamiccertificates .NewDynamicServingCertificateController (
7083 tlsConfig ,
7184 nil ,
7285 certKeyPair ,
7386 nil ,
74- nil ,
87+ record .NewEventRecorderAdapter (
88+ eventBroadcaster .NewRecorder (scheme .Scheme , v1.EventSource {Component : "troubleshooting-panel-console-plugin" }),
89+ ),
7590 )
7691
77- // Check that the cert and key files are valid.
78- if err := ctrl .RunOnce (); err != nil {
79- logrus .WithError (err ).Fatal ("invalid certificate/key files" )
80- }
92+ // Configure the server to use the cert/key pair for all client connections.
93+ tlsConfig .GetConfigForClient = ctrl .GetConfigForClient
94+
95+ // Notify cert/key file changes to the controller.
96+ certKeyPair .AddListener (ctrl )
8197
82- ctx := context .Background ()
8398 go ctrl .Run (1 , ctx .Done ())
84- }
99+ go certKeyPair . Run ( ctx , 1 )
85100
86- logrusLevel , err := logrus .ParseLevel (cfg .LogLevel )
87- if err != nil {
88- logrus .WithError (err ).Fatal ("unable to set the log level" )
89- logrusLevel = logrus .ErrorLevel
90101 }
91102
92103 httpServer := & http.Server {
@@ -97,18 +108,16 @@ func Start(cfg *Config) {
97108 WriteTimeout : timeout ,
98109 }
99110
100- if logrusLevel == logrus .TraceLevel {
111+ if logrus . GetLevel () == logrus .TraceLevel {
101112 loggedRouter := handlers .LoggingHandler (log .Logger .Out , router )
102113 httpServer .Handler = loggedRouter
103114 }
104115
105116 if tlsEnabled {
106- log .Infof ("listening on https://:%d" , cfg .Port )
107- logrus .SetLevel (logrusLevel )
117+ log .Infof ("listening for https on %s" , httpServer .Addr )
108118 panic (httpServer .ListenAndServeTLS (cfg .CertFile , cfg .PrivateKeyFile ))
109119 } else {
110- log .Infof ("listening on http://:%d" , cfg .Port )
111- logrus .SetLevel (logrusLevel )
120+ log .Infof ("listening for http on %s" , httpServer .Addr )
112121 panic (httpServer .ListenAndServe ())
113122 }
114123}
0 commit comments