Skip to content

Commit 1440cc8

Browse files
committed
feat: add TLS endpoint to kepler exporter
Add TLS support via the new web configuration file, following the Prometheus Exporter Toolkit style for consistency across exporters. * Usage: kepler --web.config.file=web-config.yml * Content of web-config.yml: tls_server_config: cert_file: /path/to/server.crt key_file: /path/to/server.key Signed-off-by: Anthony Harivel <[email protected]>
1 parent 70145df commit 1440cc8

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

cmd/exporter/exporter.go

+49-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/sustainable-computing-io/kepler/pkg/sensors/accelerator"
3838
"github.com/sustainable-computing-io/kepler/pkg/sensors/components"
3939
"github.com/sustainable-computing-io/kepler/pkg/sensors/platform"
40+
"gopkg.in/yaml.v3"
4041

4142
"github.com/prometheus/client_golang/prometheus"
4243
"github.com/prometheus/client_golang/prometheus/promhttp"
@@ -51,6 +52,15 @@ const (
5152
startedMsg = "Started Kepler in %s"
5253
)
5354

55+
type TLSConfig struct {
56+
CertFile string `yaml:"cert_file"`
57+
KeyFile string `yaml:"key_file"`
58+
}
59+
60+
type TLSServerConfig struct {
61+
TLSConfig TLSConfig `yaml:"tls_server_config"`
62+
}
63+
5464
// AppConfig holds the configuration info for the application.
5565
type AppConfig struct {
5666
BaseDir string
@@ -66,6 +76,7 @@ type AppConfig struct {
6676
ExposeEstimatedIdlePower bool
6777
MachineSpecFilePath string
6878
DisablePowerMeter bool
79+
TLSFilePath string
6980
}
7081

7182
func newAppConfig() *AppConfig {
@@ -84,6 +95,7 @@ func newAppConfig() *AppConfig {
8495
flag.BoolVar(&cfg.ExposeEstimatedIdlePower, "expose-estimated-idle-power", false, "Whether to expose the estimated idle power as a metric")
8596
flag.StringVar(&cfg.MachineSpecFilePath, "machine-spec", "", "path to the machine spec file in json format")
8697
flag.BoolVar(&cfg.DisablePowerMeter, "disable-power-meter", false, "whether manually disable power meter read and forcefully apply the estimator for node powers")
98+
flag.StringVar(&cfg.TLSFilePath, "web.config.file", "", "path to TLS web config file")
8799

88100
return cfg
89101
}
@@ -181,6 +193,32 @@ func main() {
181193
metricPathConfig := config.GetMetricPath(appConfig.MetricsPath)
182194
bindAddressConfig := config.GetBindAddress(appConfig.Address)
183195

196+
var certFile, keyFile string
197+
tlsConfigured := false
198+
199+
// Retrieve the TLS config
200+
if appConfig.TLSFilePath != "" {
201+
configPath := appConfig.TLSFilePath
202+
203+
configFile, err := os.Open(configPath)
204+
if err != nil {
205+
klog.Errorf("Error opening config file: %v\n", err)
206+
}
207+
defer configFile.Close()
208+
209+
var tlsServerConfig TLSServerConfig
210+
decoder := yaml.NewDecoder(configFile)
211+
if err := decoder.Decode(&tlsServerConfig); err != nil {
212+
klog.Errorf("Error parsing config file: %v\n", err)
213+
}
214+
215+
if tlsServerConfig.TLSConfig.CertFile != "" && tlsServerConfig.TLSConfig.KeyFile != "" {
216+
certFile = tlsServerConfig.TLSConfig.CertFile
217+
keyFile = tlsServerConfig.TLSConfig.KeyFile
218+
tlsConfigured = true
219+
}
220+
}
221+
184222
handler := http.ServeMux{}
185223
reg := m.PrometheusCollector.RegisterMetrics()
186224
handler.Handle(metricPathConfig, promhttp.HandlerFor(
@@ -207,7 +245,17 @@ func main() {
207245
wg.Add(1)
208246
go func() {
209247
defer wg.Done()
210-
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
248+
if tlsConfigured {
249+
// Run server in TLS mode
250+
klog.Infof("Starting server with TLS")
251+
err = srv.ListenAndServeTLS(certFile, keyFile)
252+
} else {
253+
// Fall back to non-TLS mode
254+
klog.Infof("Starting server without TLS")
255+
err = srv.ListenAndServe()
256+
}
257+
258+
if err != nil && !errors.Is(err, http.ErrServerClosed) {
211259
errChan <- err
212260
}
213261
}()

0 commit comments

Comments
 (0)