@@ -2,21 +2,31 @@ package scenarioorchestrator
22
33import (
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+
2030func 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
0 commit comments