-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandler.go
581 lines (477 loc) · 18.4 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
package main
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"net/url"
"strings"
fthealth "github.com/Financial-Times/go-fthealth/v1_1"
log "github.com/Financial-Times/go-logger"
)
type httpHandler struct {
controller controller
pathPrefix string
clusterURL string
}
// IndividualHealthcheckParams struct used to populate HTML template with individual checks
type IndividualHealthcheckParams struct {
Name string
Status string
LastUpdated string
MoreInfoPath string
AddOrRemoveAckPath string
AddOrRemoveAckPathName string
AckMessage string
Output string
}
// AggregateHealthcheckParams struct used to populate HTML template with aggregate checks
type AggregateHealthcheckParams struct {
PageTitle string
GeneralStatus string
RefreshFromCachePath string
RefreshWithoutCachePath string
AckCount int
IndividualHealthChecks []IndividualHealthcheckParams
}
// AddAckForm struct used to populate HTML template for add acknowledge form
type AddAckForm struct {
ServiceName string
AddAckPath string
}
var defaultCategories = []string{"default"}
const (
timeLayout = "2006-01-02 15:04:05 MST"
healthcheckTemplateName = "html-templates/healthcheck-template.html"
addAckMsgTemplatePath = "html-templates/add-ack-message-form-template.html"
healthcheckPath = "/__health"
jsonContentType = "application/json"
)
func handleResponseWriterErr(err error) {
if err != nil {
log.WithError(err).Error("Cannot write the http response body.")
}
}
func (h *httpHandler) updateStickyCategory(w http.ResponseWriter, r *http.Request, isEnabled bool) {
categoryName := r.URL.Query().Get("category-name")
if categoryName == "" {
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte("Provided category name is not valid."))
handleResponseWriterErr(err)
return
}
log.Infof("Updating category [%s] with isEnabled flag value of [%t]", categoryName, isEnabled)
err := h.controller.updateStickyCategory(r.Context(), categoryName, isEnabled)
if err != nil {
log.WithError(err).Errorf("Failed to update category with name %s.", categoryName)
w.WriteHeader(http.StatusInternalServerError)
_, err := w.Write([]byte("Failed to enable category."))
handleResponseWriterErr(err)
return
}
}
func (h *httpHandler) handleDisableCategory(w http.ResponseWriter, r *http.Request) {
h.updateStickyCategory(w, r, false)
}
func (h *httpHandler) handleEnableCategory(w http.ResponseWriter, r *http.Request) {
h.updateStickyCategory(w, r, true)
}
func (h *httpHandler) handleRemoveAck(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
serviceName := getServiceNameFromURL(r.URL)
if serviceName == "" {
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte("Provided service name is not valid."))
handleResponseWriterErr(err)
return
}
log.Infof("Removing ack for service with name %s", serviceName)
err := h.controller.removeAck(r.Context(), serviceName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.WithError(err).Errorf("Cannot remove ack for service with name %s.", serviceName)
return
}
http.Redirect(w, r, fmt.Sprintf("%s?cache=false", h.pathPrefix), http.StatusMovedPermanently)
}
func (h *httpHandler) handleAddAck(w http.ResponseWriter, r *http.Request) {
serviceName := getServiceNameFromURL(r.URL)
ackMessage := r.PostFormValue("ack-msg")
if serviceName == "" {
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte("Provided service name is not valid."))
handleResponseWriterErr(err)
return
}
log.Infof("Acking service with name %s", serviceName)
err := h.controller.addAck(r.Context(), serviceName, ackMessage)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.WithError(err).Errorf("Cannot add acknowledge for service with name %s.", serviceName)
}
http.Redirect(w, r, fmt.Sprintf("%s?cache=false", h.pathPrefix), http.StatusMovedPermanently)
}
func (h *httpHandler) handleAddAckForm(w http.ResponseWriter, r *http.Request) {
serviceName := getServiceNameFromURL(r.URL)
if serviceName == "" {
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte("Provided service name is not valid."))
handleResponseWriterErr(err)
return
}
w.Header().Add("Content-Type", "text/html")
htmlTemplate := parseHTMLTemplate(w, addAckMsgTemplatePath)
if htmlTemplate == nil {
return
}
addAckForm := AddAckForm{
ServiceName: serviceName,
AddAckPath: fmt.Sprintf("%s/add-ack?service-name=%s", h.pathPrefix, serviceName),
}
if err := htmlTemplate.Execute(w, addAckForm); err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.WithError(err).Error("Cannot apply params to html template")
_, err := w.Write([]byte("Couldn't render template file for html response"))
handleResponseWriterErr(err)
return
}
}
func (h *httpHandler) handleServicesHealthCheck(w http.ResponseWriter, r *http.Request) {
categories := parseCategories(r.URL)
useCache := useCache(r.URL)
healthResult, validCategories, err := h.controller.buildServicesHealthResult(r.Context(), categories, useCache)
if len(validCategories) == 0 && err == nil {
w.WriteHeader(http.StatusBadRequest)
if r.Header.Get("Accept") != "application/json" {
_, err := w.Write([]byte("Provided categories are not valid."))
handleResponseWriterErr(err)
}
return
}
log.Infof("Checking services health for categories %s, useCache: %t", getCategoriesString(validCategories), useCache)
if err != nil {
log.WithError(err).Error("Cannot build services health result")
w.WriteHeader(http.StatusInternalServerError)
return
}
if r.Header.Get("Accept") == jsonContentType {
for i, serviceCheck := range healthResult.Checks {
serviceHealthcheckURL := getServiceHealthcheckURL(h.clusterURL, h.pathPrefix, serviceCheck.Name)
healthResult.Checks[i].TechnicalSummary = fmt.Sprintf("%s Service healthcheck: %s", serviceCheck.TechnicalSummary, serviceHealthcheckURL)
}
buildHealthcheckJSONResponse(w, healthResult)
} else {
env := h.controller.getEnvironment()
buildServicesCheckHTMLResponse(w, healthResult, env, getCategoriesString(validCategories), h.pathPrefix)
}
}
func (h *httpHandler) handlePodsHealthCheck(w http.ResponseWriter, r *http.Request) {
serviceName := getServiceNameFromURL(r.URL)
if serviceName == "" {
w.WriteHeader(http.StatusBadRequest)
if r.Header.Get("Accept") != jsonContentType {
_, err := w.Write([]byte("Couldn't get service name from url."))
handleResponseWriterErr(err)
}
return
}
healthResult, err := h.controller.buildPodsHealthResult(r.Context(), serviceName)
log.Infof("Checking pods health for service [%s]", serviceName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.WithError(err).Errorf("Cannot perform checks for service with name %s", serviceName)
_, err := w.Write([]byte(fmt.Sprintf("Cannot perform checks for service with name %s", serviceName)))
handleResponseWriterErr(err)
return
}
if r.Header.Get("Accept") == jsonContentType {
for i, podCheck := range healthResult.Checks {
serviceHealthcheckURL := getIndividualPodHealthcheckURL(h.clusterURL, h.pathPrefix, podCheck.Name)
healthResult.Checks[i].TechnicalSummary = fmt.Sprintf("%s Pod healthcheck: %s", podCheck.TechnicalSummary, serviceHealthcheckURL)
}
buildHealthcheckJSONResponse(w, healthResult)
} else {
env := h.controller.getEnvironment()
buildPodsCheckHTMLResponse(w, healthResult, env, serviceName, h.pathPrefix)
}
}
func (h *httpHandler) handleIndividualPodHealthCheck(w http.ResponseWriter, r *http.Request) {
podName := getPodNameFromURL(r.URL)
if podName == "" {
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte("Cannot parse pod name from url."))
handleResponseWriterErr(err)
return
}
log.Infof("Retrieving individual pod health check for pod with name %s", podName)
podHealth, contentTypeHeader, err := h.controller.getIndividualPodHealth(r.Context(), podName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.WithError(err).Errorf("Cannot get individual healthcheck for pod %s", podName)
_, err := w.Write([]byte(fmt.Sprintf("Cannot get individual healthcheck for pod %s", podName)))
handleResponseWriterErr(err)
return
}
w.Header().Add("Content-Type", contentTypeHeader)
_, err = w.Write(podHealth)
handleResponseWriterErr(err)
}
func (h *httpHandler) handleGoodToGo(w http.ResponseWriter, r *http.Request) {
categories := parseCategories(r.URL)
useCache := useCache(r.URL)
healthResults, validCategories, err := h.controller.buildServicesHealthResult(r.Context(), categories, useCache)
if len(validCategories) == 0 && err == nil {
w.WriteHeader(http.StatusBadRequest)
return
}
log.Debugf("Handling gtg for categories %s, useCache: %t", getCategoriesString(validCategories), useCache)
if err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
for _, validCategory := range validCategories {
if !validCategory.isEnabled {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
}
if !healthResults.Ok {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
}
func parseCategories(theURL *url.URL) []string {
queriedCategories := theURL.Query().Get("categories")
if queriedCategories == "" {
return defaultCategories
}
return strings.Split(queriedCategories, ",")
}
func getServiceNameFromURL(url *url.URL) string {
return url.Query().Get("service-name")
}
func getPodNameFromURL(url *url.URL) string {
return url.Query().Get("pod-name")
}
func useCache(theURL *url.URL) bool {
//use cache by default
return theURL.Query().Get("cache") != "false"
}
func buildHealthcheckJSONResponse(w http.ResponseWriter, healthResult fthealth.HealthResult) {
type CheckResultWithHeimdalAck struct {
fthealth.CheckResult
HeimdalAck string `json:"_acknowledged,omitempty"`
}
type HealthResult struct {
SchemaVersion float64 `json:"schemaVersion"`
SystemCode string `json:"systemCode"`
Name string `json:"name"`
Description string `json:"description"`
Checks []CheckResultWithHeimdalAck `json:"checks"`
Ok bool `json:"ok"`
Severity uint8 `json:"severity,omitempty"`
}
var newChecks []CheckResultWithHeimdalAck
for _, check := range healthResult.Checks {
newCheck := CheckResultWithHeimdalAck{
CheckResult: check,
HeimdalAck: check.Ack,
}
newChecks = append(newChecks, newCheck)
}
newHealthResult := &HealthResult{
SchemaVersion: healthResult.SchemaVersion,
SystemCode: healthResult.SystemCode,
Name: healthResult.Name,
Description: healthResult.Description,
Ok: healthResult.Ok,
Severity: healthResult.Severity,
Checks: newChecks,
}
w.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(w)
err := enc.Encode(newHealthResult)
if err != nil {
panic("Couldn't encode health results to ResponseWriter.")
}
}
func buildServicesCheckHTMLResponse(w http.ResponseWriter, healthResult fthealth.HealthResult, environment string, categories string, pathPrefix string) {
w.Header().Add("Content-Type", "text/html")
htmlTemplate := parseHTMLTemplate(w, healthcheckTemplateName)
if htmlTemplate == nil {
return
}
aggregateHealthcheckParams := populateAggregateServiceChecks(healthResult, environment, categories, pathPrefix)
if err := htmlTemplate.Execute(w, aggregateHealthcheckParams); err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.WithError(err).Error("Cannot apply params to html template")
_, err := w.Write([]byte("Couldn't render template file for html response"))
handleResponseWriterErr(err)
return
}
}
func buildPodsCheckHTMLResponse(w http.ResponseWriter, healthResult fthealth.HealthResult, environment string, serviceName string, pathPrefix string) {
w.Header().Add("Content-Type", "text/html")
htmlTemplate := parseHTMLTemplate(w, healthcheckTemplateName)
if htmlTemplate == nil {
return
}
aggregateHealthcheckParams := populateAggregatePodChecks(healthResult, environment, serviceName, pathPrefix)
if err := htmlTemplate.Execute(w, aggregateHealthcheckParams); err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.WithError(err).Error("Cannot apply params to html template")
_, err := w.Write([]byte("Couldn't render template file for html response"))
handleResponseWriterErr(err)
return
}
}
func parseHTMLTemplate(w http.ResponseWriter, templateName string) *template.Template {
htmlTemplate, err := template.ParseFiles(templateName)
if err != nil {
log.WithError(err).Errorf("Could not parse html template with name %s", templateName)
w.WriteHeader(http.StatusInternalServerError)
_, err := w.Write([]byte("Couldn't open template file for html response"))
handleResponseWriterErr(err)
return nil
}
return htmlTemplate
}
func populateAggregateServiceChecks(healthResult fthealth.HealthResult, environment string, categories string, pathPrefix string) *AggregateHealthcheckParams {
indiviualServiceChecks, ackCount := populateIndividualServiceChecks(healthResult.Checks, pathPrefix)
aggregateChecks := &AggregateHealthcheckParams{
PageTitle: buildPageTitle(environment, categories),
GeneralStatus: getGeneralStatus(healthResult),
RefreshFromCachePath: buildRefreshFromCachePath(categories, pathPrefix),
RefreshWithoutCachePath: buildRefreshWithoutCachePath(categories, pathPrefix),
AckCount: ackCount,
IndividualHealthChecks: indiviualServiceChecks,
}
return aggregateChecks
}
func buildRefreshFromCachePath(categories string, pathPrefix string) string {
if categories != "" {
return fmt.Sprintf("%s?categories=%s", pathPrefix, categories)
}
return healthcheckPath
}
func buildRefreshWithoutCachePath(categories string, pathPrefix string) string {
refreshWithoutCachePath := fmt.Sprintf("%s?cache=false", pathPrefix)
if categories != "" {
return fmt.Sprintf("%s&categories=%s", refreshWithoutCachePath, categories)
}
return refreshWithoutCachePath
}
func populateIndividualServiceChecks(checks []fthealth.CheckResult, pathPrefix string) ([]IndividualHealthcheckParams, int) {
indiviualServiceChecks := make([]IndividualHealthcheckParams, len(checks))
ackCount := 0
for i, individualCheck := range checks {
if individualCheck.Ack != "" {
ackCount++
}
addOrRemoveAckPath, addOrRemoveAckPathName := buildAddOrRemoveAckPath(individualCheck.Name, pathPrefix, individualCheck.Ack)
hc := IndividualHealthcheckParams{
Name: individualCheck.Name,
Status: getServiceStatusFromCheck(individualCheck),
LastUpdated: individualCheck.LastUpdated.Format(timeLayout),
MoreInfoPath: fmt.Sprintf("%s/__pods-health?service-name=%s", pathPrefix, individualCheck.Name),
AddOrRemoveAckPath: addOrRemoveAckPath,
AddOrRemoveAckPathName: addOrRemoveAckPathName,
AckMessage: individualCheck.Ack,
Output: individualCheck.CheckOutput,
}
indiviualServiceChecks[i] = hc
}
return indiviualServiceChecks, ackCount
}
func buildAddOrRemoveAckPath(serviceName string, pathPrefix string, ackMessage string) (string, string) {
if ackMessage == "" {
return fmt.Sprintf("%s/add-ack-form?service-name=%s", pathPrefix, serviceName), "Ack service"
}
return fmt.Sprintf("%s/rem-ack?service-name=%s", pathPrefix, serviceName), "Remove ack"
}
func populateIndividualPodChecks(checks []fthealth.CheckResult, pathPrefix string) ([]IndividualHealthcheckParams, int) {
indiviualServiceChecks := make([]IndividualHealthcheckParams, len(checks))
ackCount := 0
for i, check := range checks {
if check.Ack != "" {
ackCount++
}
podName := extractPodName(check.Name)
hc := IndividualHealthcheckParams{
Name: check.Name,
Status: getServiceStatusFromCheck(check),
LastUpdated: check.LastUpdated.Format(timeLayout),
MoreInfoPath: getIndividualPodHealthcheckURL("", pathPrefix, podName),
AckMessage: check.Ack,
Output: check.CheckOutput,
}
indiviualServiceChecks[i] = hc
}
return indiviualServiceChecks, ackCount
}
func getIndividualPodHealthcheckURL(clusterURL, pathPrefix, podName string) string {
return fmt.Sprintf("%s%s/__pod-individual-health?pod-name=%s", clusterURL, pathPrefix, podName)
}
func extractPodName(checkName string) string {
s := strings.Split(checkName, " ")
if len(s) >= 1 {
return s[0]
}
return ""
}
func populateAggregatePodChecks(healthResult fthealth.HealthResult, environment string, serviceName string, pathPrefix string) *AggregateHealthcheckParams {
individualChecks, ackCount := populateIndividualPodChecks(healthResult.Checks, pathPrefix)
aggregateChecks := &AggregateHealthcheckParams{
PageTitle: fmt.Sprintf("UPP %s cluster's pods of service %s", environment, serviceName),
GeneralStatus: getGeneralStatus(healthResult),
RefreshFromCachePath: getServiceHealthcheckURL("", pathPrefix, serviceName),
RefreshWithoutCachePath: fmt.Sprintf("%s/__pods-health?cache=false&service-name=%s", pathPrefix, serviceName),
IndividualHealthChecks: individualChecks,
AckCount: ackCount,
}
return aggregateChecks
}
func getServiceHealthcheckURL(hostURL, pathPrefix, serviceName string) string {
return fmt.Sprintf("%s%s/__pods-health?service-name=%s", hostURL, pathPrefix, serviceName)
}
func buildPageTitle(environment string, categories string) string {
return fmt.Sprintf("UPP %s cluster's services from categories %s", environment, categories)
}
func getServiceStatusFromCheck(check fthealth.CheckResult) string {
status := getStatusFromCheck(check)
if check.Ack != "" {
return status + " acked"
}
return status
}
func getStatusFromCheck(check fthealth.CheckResult) string {
if check.Ok {
return "ok"
}
if check.Severity == 2 {
return "warning"
}
return "critical"
}
func getGeneralStatus(healthResult fthealth.HealthResult) string {
if healthResult.Ok {
return "healthy"
}
if healthResult.Severity == 2 {
return "unhealthy"
}
return "critical"
}
func getCategoriesString(categories map[string]category) string {
formattedCategoryNames := ""
for categoryName := range categories {
formattedCategoryNames += categoryName + ","
}
l := len(formattedCategoryNames)
if l > 0 {
formattedCategoryNames = formattedCategoryNames[:l-1]
}
return formattedCategoryNames
}