generated from cisco-ospo/oss-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
589 lines (521 loc) · 17.5 KB
/
main.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
582
583
584
585
586
587
588
589
// Copyright 2024 Cisco Systems, Inc. and its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/cisco-open/synthetic-heart/common"
"github.com/cisco-open/synthetic-heart/common/proto"
"github.com/cisco-open/synthetic-heart/common/storage"
gmux "github.com/gorilla/mux"
"github.com/hashicorp/go-hclog"
"github.com/pkg/errors"
"github.com/redis/go-redis/v9"
"github.com/rs/cors"
"io/ioutil"
"log"
"net/http"
_ "net/http/pprof"
"os"
"strconv"
"strings"
"sync"
"time"
)
const PingRefreshFrequency = 15 * time.Second
type RestApi struct {
config RestApiConfig
srv *http.Server
store storage.RedisSynHeartStore
PingResponse PingApiResponse
pingRespMutex *sync.Mutex
logger hclog.Logger
}
type PingApiResponse struct {
Message string `json:"message"`
LastUpdated string `json:"lastUpdated"`
Details string `json:"details"`
Status int `json:"status"`
FailedTests map[string]FailedTestInfo `json:"failedTests"`
}
type FailedTestInfo struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
TestConfigId string `json:"testId"`
DisplayName string `json:"displayName"`
Status int `json:"status"`
}
type RestApiConfig struct {
Address string `yaml:"address"`
StorageAddress string `yaml:"storageAddress"`
UIAddress string `yaml:"uiAddress"`
DebugMode bool `yaml:"debugMode"`
}
func NewRestApi(configPath string) (*RestApi, error) {
r := RestApi{}
pluginConfig := RestApiConfig{}
r.pingRespMutex = &sync.Mutex{}
r.logger = hclog.New(&hclog.LoggerOptions{
Name: "restapi",
Level: hclog.LevelFromString(os.Getenv("LOG_LEVEL")),
})
b, err := ioutil.ReadFile(configPath)
if err != nil {
return &RestApi{}, errors.Wrap(err, "error reading file")
}
err = common.ParseYMLConfig(string(b), &pluginConfig)
if err != nil {
return &RestApi{}, errors.Wrap(err, "error parsing config")
}
r.config = pluginConfig
router := gmux.NewRouter()
// Setup HTTP response
router.HandleFunc("/ui", r.RedirectToUi)
router.HandleFunc("/api/v1/ping", r.GetPing)
router.HandleFunc("/api/v1/agents", r.GetAllAgents)
router.HandleFunc("/api/v1/testconfigs/summary", r.GetAllTests)
router.HandleFunc("/api/v1/testconfig/{id:[a-zA-z0-9-]+\\/[a-zA-z0-9-]+}", r.GetTestConfig)
router.HandleFunc("/api/v1/plugins/status", r.GetAllPluginStatus)
router.HandleFunc("/api/v1/plugin/{id:[a-zA-z0-9-]+\\/[a-zA-z0-9-]+\\/[a-zA-z0-9-]+\\/[a-zA-z0-9-]+}/health", r.GetPluginHealth)
router.HandleFunc("/api/v1/plugin/{id:[a-zA-z0-9-]+\\/[a-zA-z0-9-]+\\/[a-zA-z0-9-]+\\/[a-zA-z0-9-]+}/lastUnhealthy", r.GetPluginHealth)
router.HandleFunc("/api/v1/testruns/status", r.GetAllTestStatus)
router.HandleFunc("/api/v1/testrun/{id:[a-zA-z0-9-]+\\/[a-zA-z0-9-]+\\/[a-zA-z0-9-]+\\/[a-zA-z0-9-]+}/latest", r.GetTestRun)
router.HandleFunc("/api/v1/testrun/{id:[a-zA-z0-9-]+\\/[a-zA-z0-9-]+\\/[a-zA-z0-9-]+\\/[a-zA-z0-9-]+}/lastFailed", r.GetTestRun)
router.HandleFunc("/api/v1/testrun/{id:[a-zA-z0-9-]+\\/[a-zA-z0-9-]+\\/[a-zA-z0-9-]+\\/[a-zA-z0-9-]+}/latest/logs", r.GetTestLogs)
router.HandleFunc("/api/v1/testrun/{id:[a-zA-z0-9-]+\\/[a-zA-z0-9-]+\\/[a-zA-z0-9-]+\\/[a-zA-z0-9-]+}/lastFailed/logs", r.GetTestLogs)
if pluginConfig.DebugMode {
router.PathPrefix("/debug/").Handler(http.DefaultServeMux)
}
handler := cors.Default().Handler(router)
srv := &http.Server{Addr: r.config.Address, Handler: handler}
r.srv = srv
extStore := storage.NewRedisSynHeartStore(storage.SynHeartStoreConfig{
Type: "redis",
BufferSize: 1000,
Address: r.config.StorageAddress,
}, r.logger)
r.store = extStore
return &r, nil
}
func (r *RestApi) Finish() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := r.srv.Shutdown(ctx)
if err != nil {
return err
}
return nil
}
func (r *RestApi) RedirectToUi(w http.ResponseWriter, req *http.Request) {
r.PrintIPAndUserAgent(req)
redirectUrl := r.config.UIAddress
if redirectUrl == "" {
r.logger.Warn("No UI address configured, redirecting to base url")
redirectUrl = req.URL.Path // set it back to root path, if no ui address set
}
if req.URL.RawQuery != "" {
redirectUrl = fmt.Sprintf("%s&%s", redirectUrl, req.URL.RawQuery)
} else {
redirectUrl = fmt.Sprintf("%s", redirectUrl)
}
http.Redirect(w, req, redirectUrl, http.StatusSeeOther)
return
}
func (r *RestApi) GetAllAgents(w http.ResponseWriter, req *http.Request) {
r.PrintIPAndUserAgent(req)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
agents, err := r.store.FetchAllAgentStatus(ctx)
if err != nil {
r.logger.Error("error fetching agent statuses", "err", err)
http.Error(w, "unable to fetch all tests", http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(agents)
if err != nil {
r.logger.Error("error encoding json", "err", err)
}
}
func (r *RestApi) GetAllTests(w http.ResponseWriter, req *http.Request) {
r.PrintIPAndUserAgent(req)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
syntests, err := r.store.FetchAllTestConfigSummary(ctx)
if err != nil {
r.logger.Error("error fetching syntests", "err", err)
http.Error(w, "unable to fetch all tests", http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(syntests)
if err != nil {
r.logger.Error("error encoding json", "err", err)
}
}
func (r *RestApi) GetAllTestStatus(w http.ResponseWriter, req *http.Request) {
r.PrintIPAndUserAgent(req)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
w.Header().Set("Content-Type", "application/json")
status, err := r.store.FetchAllTestRunStatus(ctx)
if err != nil {
r.logger.Error("error fetching status from extStore", "err", err)
http.Error(w, "error fetching status from extStore", http.StatusInternalServerError)
return
}
err = json.NewEncoder(w).Encode(status)
if err != nil {
r.logger.Error("error encoding json", "err", err)
}
}
func (r *RestApi) GetAllPluginStatus(w http.ResponseWriter, req *http.Request) {
r.PrintIPAndUserAgent(req)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
w.Header().Set("Content-Type", "application/json")
status, err := r.store.FetchAllPluginStatus(ctx)
if err != nil {
r.logger.Error("error fetching plugin status from extStore", "err", err)
http.Error(w, "error fetching plugin status from extStore", http.StatusInternalServerError)
return
}
err = json.NewEncoder(w).Encode(status)
if err != nil {
r.logger.Error("error encoding json", "err", err)
}
}
func (r *RestApi) GetPluginHealth(w http.ResponseWriter, req *http.Request) {
r.PrintIPAndUserAgent(req)
id, ok := gmux.Vars(req)["id"]
if !ok {
http.Error(w, "no test id provided", http.StatusUnprocessableEntity)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
redisKey := ""
if strings.HasSuffix(req.URL.String(), "/lastUnhealthy") {
redisKey = fmt.Sprintf(storage.PluginLastUnhealthyFmt, id)
} else {
redisKey = fmt.Sprintf(storage.PluginLatestHealthFmt, id)
}
// Get Health
health, err := r.store.GetR(ctx, redisKey)
if err != nil {
if errors.Is(err, redis.Nil) {
http.Error(w, "no plugin health found", http.StatusNotFound)
return
}
r.logger.Error("error getting health for syntest", "id", id, "err", err)
http.Error(w, "unable to fetch test run", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(health))
}
func (r *RestApi) GetTestConfig(w http.ResponseWriter, req *http.Request) {
r.PrintIPAndUserAgent(req)
configId, ok := gmux.Vars(req)["id"]
if !ok {
http.Error(w, "no test id provided", http.StatusUnprocessableEntity)
return
}
w.Header().Set("Content-Type", "application/json")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// Using GetR to obtain the json directly from redis instead using a function that parses the json
// Get Config
testConfig, err := r.store.GetR(ctx, fmt.Sprintf(storage.ConfigSynTestJsonFmt, configId))
if err != nil {
if errors.Is(err, redis.Nil) {
http.Error(w, "no test config found", http.StatusNotFound)
return
} else {
r.logger.Error("error getting config for syntest", "id", configId, "err", err)
http.Error(w, "unable to fetch config", http.StatusInternalServerError)
return
}
}
// Get raw config (i.e. crd) for the test plugin
raw, err := r.store.GetR(ctx, fmt.Sprintf(storage.ConfigSynTestRawFmt, configId))
if err != nil {
if errors.Is(err, redis.Nil) {
http.Error(w, "no test config found", http.StatusNotFound)
return
} else {
r.logger.Error("error getting raw config for syntest", "id", configId, "err", err)
http.Error(w, "unable to fetch config", http.StatusInternalServerError)
return
}
}
// Get the status of the test config (i.e. CRD status)
status, err := r.store.GetR(ctx, fmt.Sprintf(storage.ConfigSynTestStatusFmt, configId))
if err != nil {
if errors.Is(err, redis.Nil) {
http.Error(w, "no test config found", http.StatusNotFound)
return
} else {
r.logger.Error("error getting config status for syntest", "id", configId, "err", err)
http.Error(w, "unable to fetch config", http.StatusInternalServerError)
return
}
}
type Response struct {
TestConfig json.RawMessage `json:"testConfig"`
ConfigStatus json.RawMessage `json:"configStatus"`
RawConfig string `json:"rawConfig"`
}
err = json.NewEncoder(w).Encode(Response{
TestConfig: json.RawMessage(testConfig),
ConfigStatus: json.RawMessage(status),
RawConfig: raw,
})
if err != nil {
r.logger.Error("error encoding json", "err", err)
http.Error(w, "unable to fetch test run", http.StatusInternalServerError)
return
}
}
func (r *RestApi) GetTestRun(w http.ResponseWriter, req *http.Request) {
r.PrintIPAndUserAgent(req)
id, ok := gmux.Vars(req)["id"]
if !ok {
http.Error(w, "no test id provided", http.StatusUnprocessableEntity)
return
}
redisKey := ""
if strings.HasSuffix(req.URL.String(), "/lastFailed") {
redisKey = fmt.Sprintf(storage.TestRunLastFailedFmt, id)
} else {
redisKey = fmt.Sprintf(storage.TestRunLatestFmt, id)
}
w.Header().Set("Content-Type", "application/json")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// Using GetR to obtain the json directly from redis instead using a function that parses the json
// Get Test
test, err := r.store.GetR(ctx, redisKey)
if err != nil {
if errors.Is(err, redis.Nil) {
http.Error(w, "no testrun found", http.StatusNotFound)
return
} else {
r.logger.Error("error getting latest test run for syntest", "id", id, "err", err)
http.Error(w, "unable to fetch test run", http.StatusInternalServerError)
return
}
}
w.Write([]byte(test))
}
func (r *RestApi) GetTestLogs(w http.ResponseWriter, req *http.Request) {
r.PrintIPAndUserAgent(req)
id, ok := gmux.Vars(req)["id"]
if !ok {
http.Error(w, "no test id provided", http.StatusUnprocessableEntity)
return
}
w.Header().Set("Content-Type", "text/plain")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
var testRun proto.TestRun
var err error
if strings.HasSuffix(req.URL.String(), "/lastFailed/logs") {
testRun, err = r.store.FetchLastFailedTestRun(ctx, id)
} else {
testRun, err = r.store.FetchLatestTestRun(ctx, id)
}
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
http.Error(w, "no testrun found", http.StatusNotFound)
return
} else {
r.logger.Error("error getting latest test run for syntest", "id", id, "err", err)
http.Error(w, "unable to fetch test run", http.StatusInternalServerError)
return
}
}
logs := testRun.Details[common.LogKey]
w.Write([]byte(logs))
}
func (r *RestApi) GetPing(w http.ResponseWriter, req *http.Request) {
r.PrintIPAndUserAgent(req)
w.Header().Set("Content-Type", "application/json")
r.pingRespMutex.Lock()
defer r.pingRespMutex.Unlock()
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(r.PingResponse)
if err != nil {
r.logger.Error("error encoding json", "err", err)
}
}
func (r *RestApi) UpdatePingResponse(ctx context.Context, redisAddress string) {
logger := r.logger.Named("ping-loop")
storageClient := storage.NewRedisSynHeartStore(storage.SynHeartStoreConfig{
Type: "redis",
BufferSize: 1000,
Address: redisAddress,
}, logger)
defer storageClient.Close()
allStatus, err := storageClient.FetchAllTestRunStatus(ctx)
if err != nil {
logger.Error("error fetching status from extStore", "err", err)
return
}
configSummaries, err := storageClient.FetchAllTestConfigSummary(ctx)
if err != nil {
logger.Error("error fetching status displayNames", "err", err)
return
}
resp := PingApiResponse{
Message: "",
LastUpdated: "",
Details: "",
FailedTests: map[string]FailedTestInfo{},
Status: 0,
}
maxFailedTestNames := 3
failedTestNames := map[string]bool{}
failedTests := map[string]FailedTestInfo{} // Used to construct the details string later
overallStatus := 3
for pluginId, passRatioStr := range allStatus {
passRatio, err := strconv.ParseFloat(passRatioStr, 64)
if err != nil {
logger.Error("error converting status to int", "err", err, "status", passRatioStr)
continue
}
testName, testNs, _, _, err := common.GetPluginIdComponents(pluginId)
if err != nil {
logger.Error("error decomposing plugin id", "err", err)
continue
}
configId := common.ComputeSynTestConfigId(testName, testNs)
legacyStatus := GetLegacyStatus(passRatio) // this is a status of the test run based on the pass ratio, its legacy, to maintain backwards compatibility
if passRatio < 1 {
if failedTestInfo, ok := failedTests[testName]; ok {
failedTests[testName] = failedTestInfo
} else {
fti := FailedTestInfo{
Name: testName,
Namespace: testNs,
TestConfigId: configId,
DisplayName: configSummaries[configId].DisplayName,
Status: GetLegacyStatus(passRatio),
}
failedTests[testName] = fti
}
if len(failedTestNames) < 3 {
// add the name to list, so we can concatenate it and
if failedTests[testName].DisplayName != "" {
failedTestNames[failedTests[testName].DisplayName] = true
} else {
failedTestNames[testName] = true
}
}
// set the overall status
if legacyStatus < overallStatus {
if legacyStatus != 0 {
overallStatus = legacyStatus
} else {
overallStatus = 2 // if the test status is unknown we set the overall status to warning
}
}
}
}
failedTestNamesString := ""
i := 0
for testName, _ := range failedTestNames {
if i == maxFailedTestNames || i == (len(failedTestNames)-1) { // Dont include comma in the last one
failedTestNamesString += testName
} else {
failedTestNamesString += testName + ", "
}
i++
}
if len(failedTests) > 3 {
resp.Details = fmt.Sprintf("%d failed tests: %s and %d more", len(failedTests), failedTestNamesString, len(failedTests)-3)
} else if len(failedTests) == 0 {
resp.Details = fmt.Sprintf("No failed tests")
} else if len(failedTests) == 1 {
resp.Details = fmt.Sprintf("%d failed test: %s", len(failedTestNames), failedTestNamesString)
} else {
resp.Details = fmt.Sprintf("%d failed tests: %s", len(failedTestNames), failedTestNamesString)
}
resp.Status = overallStatus
resp.LastUpdated = time.Now().Format(common.TimeFormat)
if resp.Status == 3 {
resp.Message = "Healthy"
} else {
resp.Message = "UnHealthy"
}
resp.FailedTests = failedTests
r.pingRespMutex.Lock()
r.PingResponse = resp
r.pingRespMutex.Unlock()
}
func GetLegacyStatus(passRatio float64) int {
if passRatio == 1 {
return 3
}
if passRatio == 0 {
return 1
}
if passRatio < 1 && passRatio > 0.5 {
return 2
}
return 0
}
func (r *RestApi) PrintIPAndUserAgent(req *http.Request) {
ip := req.Header.Get("X-Real-Ip")
if ip == "" {
ip = req.Header.Get("X-Forwarded-For")
}
if ip == "" {
ip = req.RemoteAddr
}
userAgent := req.UserAgent()
r.logger.Info("request", "path", req.URL.Path, "ip", ip, "user", userAgent)
}
func main() {
configFilePath := ""
if len(os.Args) > 1 { // Override if a path has been provided - used for debugging
configFilePath = os.Args[1]
} else {
log.Fatal("no config file path provided")
}
restApi, err := NewRestApi(configFilePath)
if err != nil {
log.Fatal(err)
}
// Start the Ping Api polling/updating
go func() {
ticker := time.NewTicker(PingRefreshFrequency)
for {
select {
case <-ticker.C:
ctx, cancel := context.WithTimeout(context.Background(), PingRefreshFrequency)
restApi.UpdatePingResponse(ctx, restApi.config.StorageAddress)
cancel()
}
}
}()
// Start the Server
log.Println("running server at: " + restApi.config.Address)
err = restApi.srv.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Println("error running server: ", err)
os.Exit(1)
}
}