@@ -15,6 +15,7 @@ import (
15
15
"sort"
16
16
"time"
17
17
18
+ "github.com/fsnotify/fsnotify"
18
19
commonconfig "github.com/prometheus/common/config"
19
20
"github.com/prometheus/common/model"
20
21
promconfig "github.com/prometheus/prometheus/config"
@@ -36,6 +37,7 @@ type Manager struct {
36
37
scrapeManager * scrape.Manager
37
38
discoveryManager * discovery.Manager
38
39
enableNativeHistograms bool
40
+ watcher * fsnotify.Watcher
39
41
}
40
42
41
43
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
90
92
}
91
93
}
92
94
}()
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
+
93
120
return nil
94
121
}
95
122
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
+
96
154
func (m * Manager ) Shutdown () {
97
155
close (m .shutdown )
156
+ if m .watcher != nil {
157
+ _ = m .watcher .Close ()
158
+ }
98
159
}
99
160
100
161
// sync request jobs from targetAllocator and update underlying receiver, if the response does not match the provided compareHash.
0 commit comments