From 08f8d471dfb1417d7527858c6618e1777c1b017c Mon Sep 17 00:00:00 2001 From: Maruthi Vemuri Date: Wed, 24 Apr 2024 20:46:15 +0000 Subject: [PATCH] support https configuration --- docs/reference-config.yaml | 8 ++++++-- main.go | 15 +++++++++++---- prometheus/config.go | 8 +++++--- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/docs/reference-config.yaml b/docs/reference-config.yaml index f8570b28..cbb4a612 100644 --- a/docs/reference-config.yaml +++ b/docs/reference-config.yaml @@ -168,7 +168,11 @@ minion: exporter: # Namespace is the prefix for all exported Prometheus metrics namespace: "kminion" - # Host that shall be used to bind the HTTP server on + # Host that shall be used to bind the HTTP(S) server on host: "" - # Port that shall be used to bind the HTTP server on + # Port that shall be used to bind the HTTP(S) server on port: 8080 + # Certificate file to be used if configuring as a HTTPS server, leave empty to configure as a HTTP server + tlsCertificate: "" + # Key file to be used if configuring as a HTTPS server, ignored if tlsCertificate isnt set + tlsKey: "" diff --git a/main.go b/main.go index 2020f439..c011ed7c 100644 --- a/main.go +++ b/main.go @@ -116,14 +116,21 @@ func main() { go func() { <-ctx.Done() if err := srv.Shutdown(context.Background()); err != nil { - logger.Error("error stopping HTTP server", zap.Error(err)) + logger.Error("error stopping server", zap.Error(err)) os.Exit(1) } }() logger.Info("listening on address", zap.String("listen_address", address)) - if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { - logger.Error("error starting HTTP server", zap.Error(err)) - os.Exit(1) + if cfg.Exporter.TLSCertFile != "" { + if err := srv.ListenAndServeTLS(cfg.Exporter.TLSCertFile, cfg.Exporter.TLSKeyFile); err != nil && !errors.Is(err, http.ErrServerClosed) { + logger.Error("error starting HTTPS server", zap.Error(err)) + os.Exit(1) + } + } else { + if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { + logger.Error("error starting HTTP server", zap.Error(err)) + os.Exit(1) + } } logger.Info("kminion stopped") diff --git a/prometheus/config.go b/prometheus/config.go index 15649b80..75031549 100644 --- a/prometheus/config.go +++ b/prometheus/config.go @@ -1,9 +1,11 @@ package prometheus type Config struct { - Host string `koanf:"host"` - Port int `koanf:"port"` - Namespace string `koanf:"namespace"` + Host string `koanf:"host"` + Port int `koanf:"port"` + Namespace string `koanf:"namespace"` + TLSCertFile string `koanf:"tlsCertificate"` + TLSKeyFile string `koanf:"tlsKey"` } func (c *Config) SetDefaults() {