Skip to content

Commit cff85f5

Browse files
Merge pull request #103 from zhuje/OU-539-reload-cert
OU-539: Dynamically Reload Certificates
2 parents f146b3d + 7285769 commit cff85f5

2 files changed

Lines changed: 36 additions & 21 deletions

File tree

cmd/plugin-backend.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var (
1818
staticPathArg = flag.String("static-path", "", "static files path to serve frontend (default: './web/dist')")
1919
configPathArg = flag.String("config-path", "", "config files path (default: './config')")
2020
pluginConfigArg = flag.String("plugin-config-path", "", "plugin yaml configuration")
21-
logLevelArg = flag.String("log-level", "error", "verbosity of logs\noptions: ['panic', 'fatal', 'error', 'warn', 'info', 'debug', 'trace']\n'trace' level will log all incoming requests\n(default 'error')")
21+
logLevelArg = flag.String("log-level", logrus.InfoLevel.String(), "verbosity of logs\noptions: ['panic', 'fatal', 'error', 'warn', 'info', 'debug', 'trace']\n'trace' level will log all incoming requests\n(default 'error')")
2222
log = logrus.WithField("module", "main")
2323
)
2424

@@ -32,14 +32,21 @@ func main() {
3232
staticPath := mergeEnvValue("TROUBLESHOOTING_PANEL_CONSOLE_PLUGIN_STATIC_PATH", *staticPathArg, "opt/app-root/web/dist")
3333
configPath := mergeEnvValue("TROUBLESHOOTING_PANEL_CONSOLE_PLUGIN_MANIFEST_CONFIG_PATH", *configPathArg, "opt/app-root/web/dist")
3434
pluginConfigPath := mergeEnvValue("TROUBLESHOOTING_PANEL_CONSOLE_PLUGIN_CONFIG_PATH", *pluginConfigArg, "/etc/plugin/config.yaml")
35-
logLevel := mergeEnvValue("TROUBLESHOOTING_PANEL_CONSOLE_PLUGIN_LOG_LEVEL", *logLevelArg, "error")
35+
logLevel := mergeEnvValue("TROUBLESHOOTING_PANEL_CONSOLE_PLUGIN_LOG_LEVEL", *logLevelArg, logrus.InfoLevel.String())
3636
featuresList := strings.Fields(strings.Join(strings.Split(strings.ToLower(features), ","), " "))
3737

3838
featuresSet := make(map[string]bool)
3939
for _, s := range featuresList {
4040
featuresSet[s] = true
4141
}
4242

43+
logrusLevel, err := logrus.ParseLevel(logLevel)
44+
if err != nil {
45+
logrusLevel = logrus.ErrorLevel
46+
logrus.WithError(err).Warnf("Invalid log level. Defaulting to %q", logrusLevel.String())
47+
}
48+
logrus.SetLevel(logrusLevel)
49+
4350
log.Infof("enabled features: %+q\n", featuresList)
4451

4552
server.Start(&server.Config{
@@ -50,7 +57,6 @@ func main() {
5057
StaticPath: staticPath,
5158
ConfigPath: configPath,
5259
PluginConfigPath: pluginConfigPath,
53-
LogLevel: logLevel,
5460
})
5561
}
5662

pkg/server.go

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2023
var 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

3335
type 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

Comments
 (0)