Skip to content

Commit 9cd5d3c

Browse files
committed
Add cert watcher for target allocator TLS config
1 parent 47132e6 commit 9cd5d3c

File tree

1 file changed

+61
-0
lines changed
  • receiver/prometheusreceiver/targetallocator

1 file changed

+61
-0
lines changed

receiver/prometheusreceiver/targetallocator/manager.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"sort"
1616
"time"
1717

18+
"github.com/fsnotify/fsnotify"
1819
commonconfig "github.com/prometheus/common/config"
1920
"github.com/prometheus/common/model"
2021
promconfig "github.com/prometheus/prometheus/config"
@@ -36,6 +37,7 @@ type Manager struct {
3637
scrapeManager *scrape.Manager
3738
discoveryManager *discovery.Manager
3839
enableNativeHistograms bool
40+
watcher *fsnotify.Watcher
3941
}
4042

4143
func NewManager(set receiver.Settings, cfg *Config, promCfg *promconfig.Config, enableNativeHistograms bool) *Manager {
@@ -90,11 +92,70 @@ func (m *Manager) Start(ctx context.Context, host component.Host, sm *scrape.Man
9092
}
9193
}
9294
}()
95+
96+
certFile := m.cfg.TLSSetting.CertFile
97+
keyFile := m.cfg.TLSSetting.KeyFile
98+
caFile := m.cfg.TLSSetting.CAFile
99+
100+
if (certFile != "") || (keyFile != "") || (caFile != "") {
101+
watcher, werr := fsnotify.NewWatcher()
102+
if werr != nil {
103+
m.settings.Logger.Warn("Failed to create fsnotify watcher", zap.Error(werr))
104+
} else {
105+
m.watcher = watcher
106+
go m.watchTLSFiles(watcher, &savedHash, httpClient)
107+
108+
if certFile != "" {
109+
_ = watcher.Add(certFile)
110+
}
111+
if keyFile != "" {
112+
_ = watcher.Add(keyFile)
113+
}
114+
if caFile != "" {
115+
_ = watcher.Add(caFile)
116+
}
117+
}
118+
}
119+
93120
return nil
94121
}
95122

123+
func (m *Manager) watchTLSFiles(watcher *fsnotify.Watcher, savedHash *uint64, httpClient *http.Client) {
124+
for {
125+
select {
126+
case event, ok := <-watcher.Events:
127+
if !ok {
128+
return
129+
}
130+
if event.Op&(fsnotify.Write|fsnotify.Create|fsnotify.Remove) != 0 {
131+
m.settings.Logger.Info("TLS file changed; re-syncing config", zap.String("file", event.Name), zap.String("op", event.Op.String()))
132+
if event.Op&fsnotify.Remove != 0 {
133+
time.Sleep(200 * time.Millisecond)
134+
_ = watcher.Add(event.Name)
135+
}
136+
hash, newErr := m.sync(*savedHash, httpClient)
137+
if newErr != nil {
138+
m.settings.Logger.Error(newErr.Error())
139+
continue
140+
}
141+
*savedHash = hash
142+
}
143+
case werr, ok := <-watcher.Errors:
144+
if !ok {
145+
return
146+
}
147+
m.settings.Logger.Warn("fsnotify error", zap.Error(werr))
148+
case <-m.shutdown:
149+
return
150+
}
151+
}
152+
}
153+
96154
func (m *Manager) Shutdown() {
97155
close(m.shutdown)
156+
if m.watcher != nil {
157+
_ = m.watcher.Close()
158+
}
98159
}
99160

100161
// sync request jobs from targetAllocator and update underlying receiver, if the response does not match the provided compareHash.

0 commit comments

Comments
 (0)