Skip to content

Commit ac5c0f3

Browse files
committed
feat: receive resiliency report from scenario container logs and change Scenario struct to recieve weight and config
Signed-off-by: Abhinav Sharma <abhinavs1920bpl@gmail.com>
1 parent 3f0b9e7 commit ac5c0f3

2 files changed

Lines changed: 60 additions & 5 deletions

File tree

pkg/scenarioorchestrator/common_functions.go

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,31 @@ package scenarioorchestrator
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
6-
"github.com/fatih/color"
7-
"github.com/krkn-chaos/krknctl/pkg/config"
8-
providermodels "github.com/krkn-chaos/krknctl/pkg/provider/models"
9-
"github.com/krkn-chaos/krknctl/pkg/scenarioorchestrator/models"
10-
"github.com/krkn-chaos/krknctl/pkg/scenarioorchestrator/utils"
117
"io"
128
"os"
139
"os/signal"
1410
"path"
11+
"regexp"
1512
"sort"
13+
"strings"
1614
"sync"
1715
"syscall"
16+
"time"
17+
18+
"github.com/fatih/color"
19+
"github.com/krkn-chaos/krknctl/pkg/config"
20+
providermodels "github.com/krkn-chaos/krknctl/pkg/provider/models"
21+
"github.com/krkn-chaos/krknctl/pkg/scenarioorchestrator/models"
22+
"github.com/krkn-chaos/krknctl/pkg/scenarioorchestrator/utils"
1823
)
1924

25+
// ResiliencyReport represents resiliency report extracted from scenario logs
26+
type ResiliencyReport struct {
27+
Score float64 `json:"score"`
28+
}
29+
2030
func CommonRunGraph(
2131
scenarios models.ScenarioSet,
2232
resolvedGraph models.ResolvedGraph,
@@ -29,6 +39,14 @@ func CommonRunGraph(
2939
registry *providermodels.RegistryV2,
3040
userID *int,
3141
) {
42+
// collectors initialization
43+
var allResiliencyReports []ResiliencyReport
44+
var resiliencyScores []float64
45+
var resiliencyWeights []float64
46+
runStartTime := time.Now().UTC()
47+
48+
reportRegex := regexp.MustCompile(`^KRKN_RESILIENCY_REPORT_JSON:(.*)$`)
49+
3250
for step, s := range resolvedGraph {
3351
var wg sync.WaitGroup
3452
for _, scID := range s {
@@ -64,6 +82,13 @@ func CommonRunGraph(
6482
volumes[k] = v
6583
}
6684

85+
// inject resiliency config as env variable if provided (Use Case: Resiliency Config is provided via env variable)
86+
if scenario.ResiliencyConfigPath != "" {
87+
if content, err := os.ReadFile(scenario.ResiliencyConfigPath); err == nil {
88+
env["RESILIENCY_CONFIG"] = string(content)
89+
}
90+
}
91+
6792
containerName := utils.GenerateContainerName(config, scenario.Name, &scID)
6893
filename := fmt.Sprintf("%s.log", containerName)
6994
file, err := os.Create(path.Clean(filename))
@@ -86,7 +111,35 @@ func CommonRunGraph(
86111

87112
}
88113
wg.Wait()
114+
115+
// Parse resiliency reports
116+
for _, scIDIter := range s {
117+
scenarioIter := scenarios[scIDIter]
118+
containerNameIter := utils.GenerateContainerName(config, scenarioIter.Name, &scIDIter)
119+
logFilePath := fmt.Sprintf("%s.log", containerNameIter)
120+
if data, err := os.ReadFile(logFilePath); err == nil {
121+
lines := strings.Split(string(data), "\n")
122+
for _, line := range lines {
123+
if matches := reportRegex.FindStringSubmatch(line); len(matches) > 1 {
124+
var report ResiliencyReport
125+
if err := json.Unmarshal([]byte(strings.TrimSpace(matches[1])), &report); err == nil {
126+
allResiliencyReports = append(allResiliencyReports, report)
127+
resiliencyScores = append(resiliencyScores, report.Score)
128+
resiliencyWeights = append(resiliencyWeights, scenarioIter.ResiliencyWeight)
129+
}
130+
break
131+
}
132+
}
133+
}
134+
}
89135
}
136+
runEndTime := time.Now().UTC()
137+
_ = runStartTime
138+
_ = runEndTime
139+
_ = allResiliencyReports
140+
_ = resiliencyScores
141+
_ = resiliencyWeights
142+
90143
commChannel <- nil
91144
}
92145

pkg/scenarioorchestrator/models/models.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ type Scenario struct {
4141
Env map[string]string `json:"env,omitempty"`
4242
Volumes map[string]string `json:"volumes,omitempty"`
4343
ResiliencyConfig string `json:"resiliencyConfig,omitempty"`
44+
ResiliencyConfigPath string `json:"resiliencyConfigPath,omitempty"`
45+
ResiliencyWeight float64 `json:"resiliencyWeight,omitempty"`
4446
}
4547

4648
type ScenarioContainer struct {

0 commit comments

Comments
 (0)