@@ -171,24 +171,34 @@ func newBasepinChecker() *healthChecker {
171
171
}
172
172
}
173
173
174
- var lastGitMirrorErrors atomic.Value // of []string
174
+ // gitMirrorStatus is the latest known status of the gitmirror service.
175
+ var gitMirrorStatus = struct {
176
+ sync.Mutex
177
+ Errors []string
178
+ Warnings []string
179
+ }{Warnings : []string {"still checking" }}
175
180
176
181
func monitorGitMirror () {
177
182
for {
178
- lastGitMirrorErrors .Store (gitMirrorErrors ())
183
+ errs , warns := gitMirrorErrors ()
184
+ gitMirrorStatus .Lock ()
185
+ gitMirrorStatus .Errors , gitMirrorStatus .Warnings = errs , warns
186
+ gitMirrorStatus .Unlock ()
179
187
time .Sleep (30 * time .Second )
180
188
}
181
189
}
182
190
183
191
// gitMirrorErrors queries the status pages of all
184
192
// running gitmirror instances and reports errors.
185
- func gitMirrorErrors () (errs []string ) {
193
+ //
194
+ // It makes use of pool.KubeGoClient() to do the query.
195
+ func gitMirrorErrors () (errs , warns []string ) {
186
196
ctx , cancel := context .WithTimeout (context .Background (), 20 * time .Second )
187
197
defer cancel ()
188
198
pods , err := pool .KubeGoClient ().GetPods (ctx )
189
199
if err != nil {
190
200
log .Println ("gitMirrorErrors: goKubeClient.GetPods:" , err )
191
- return []string {"failed to get pods; can't query gitmirror status" }
201
+ return []string {"failed to get pods; can't query gitmirror status" }, nil
192
202
}
193
203
var runningGitMirror []api.Pod
194
204
for _ , p := range pods {
@@ -198,29 +208,32 @@ func gitMirrorErrors() (errs []string) {
198
208
runningGitMirror = append (runningGitMirror , p )
199
209
}
200
210
if len (runningGitMirror ) == 0 {
201
- return []string {"no running gitmirror instances" }
211
+ return []string {"no running gitmirror instances" }, nil
202
212
}
203
213
for _ , pod := range runningGitMirror {
204
214
// The gitmirror -http=:8585 status page URL is hardcoded here.
205
215
// If the ReplicationController configuration changes (rare), this
206
216
// health check will begin to fail until it's updated accordingly.
207
- instanceErrors := gitMirrorInstanceErrors (ctx , fmt .Sprintf ("http://%s:8585/" , pod .Status .PodIP ))
208
- for _ , err := range instanceErrors {
217
+ instErrs , instWarns := gitMirrorInstanceErrors (ctx , fmt .Sprintf ("http://%s:8585/" , pod .Status .PodIP ))
218
+ for _ , err := range instErrs {
209
219
errs = append (errs , fmt .Sprintf ("instance %s: %s" , pod .Name , err ))
210
220
}
221
+ for _ , warn := range instWarns {
222
+ warns = append (warns , fmt .Sprintf ("instance %s: %s" , pod .Name , warn ))
223
+ }
211
224
}
212
- return errs
225
+ return errs , warns
213
226
}
214
227
215
- func gitMirrorInstanceErrors (ctx context.Context , url string ) (errs []string ) {
228
+ func gitMirrorInstanceErrors (ctx context.Context , url string ) (errs , warns []string ) {
216
229
req , _ := http .NewRequestWithContext (ctx , http .MethodGet , url , nil )
217
230
res , err := http .DefaultClient .Do (req )
218
231
if err != nil {
219
- return []string {err .Error ()}
232
+ return []string {err .Error ()}, nil
220
233
}
221
234
defer res .Body .Close ()
222
235
if res .StatusCode != 200 {
223
- return []string {res .Status }
236
+ return []string {res .Status }, nil
224
237
}
225
238
// TODO: add a JSON mode to gitmirror so we don't need to parse HTML.
226
239
// This works for now. We control its output.
@@ -240,14 +253,19 @@ func gitMirrorInstanceErrors(ctx context.Context, url string) (errs []string) {
240
253
if strings .Contains (line , "</html>" ) {
241
254
break
242
255
}
243
- return []string {fmt .Sprintf ("error parsing line %q" , line )}
256
+ return []string {fmt .Sprintf ("error parsing line %q" , line )}, nil
257
+ }
258
+ if strings .HasPrefix (m [2 ], "ok; " ) {
259
+ // If the status begins with "ok", it can't be that bad.
260
+ warns = append (warns , fmt .Sprintf ("repo %s: %s" , m [1 ], m [2 ]))
261
+ continue
244
262
}
245
263
errs = append (errs , fmt .Sprintf ("repo %s: %s" , m [1 ], m [2 ]))
246
264
}
247
265
if err := bs .Err (); err != nil {
248
266
errs = append (errs , err .Error ())
249
267
}
250
- return errs
268
+ return errs , warns
251
269
}
252
270
253
271
// $1 is repo; $2 is error message
@@ -259,14 +277,15 @@ func newGitMirrorChecker() *healthChecker {
259
277
Title : "Git mirroring" ,
260
278
DocURL : "https://github.com/golang/build/tree/master/cmd/gitmirror" ,
261
279
Check : func (w * checkWriter ) {
262
- ee , ok := lastGitMirrorErrors .Load ().([]string )
263
- if ! ok {
264
- w .warn ("still checking" )
265
- return
266
- }
267
- for _ , v := range ee {
280
+ gitMirrorStatus .Lock ()
281
+ errs , warns := gitMirrorStatus .Errors , gitMirrorStatus .Warnings
282
+ gitMirrorStatus .Unlock ()
283
+ for _ , v := range errs {
268
284
w .error (v )
269
285
}
286
+ for _ , v := range warns {
287
+ w .warn (v )
288
+ }
270
289
},
271
290
}
272
291
}
0 commit comments