Skip to content

Commit f518093

Browse files
committed
decouple queueNotification from EnableAnalysis
1 parent 43ebf04 commit f518093

3 files changed

Lines changed: 49 additions & 40 deletions

File tree

pkg/common/config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
// channel id for #hcm-cicd-notifications
17-
const DefaultNotificationsChannel = "C06HQR8HN0L"
17+
const defaultNotificationsChannel = "C06HQR8HN0L"
1818

1919
type Secret struct {
2020
FileLocation string
@@ -963,7 +963,7 @@ func InitOSDe2eViper() {
963963
_ = viper.BindEnv(LogAnalysis.SlackWebhook, "LOG_ANALYSIS_SLACK_WEBHOOK")
964964
RegisterSecret(LogAnalysis.SlackWebhook, "notifier_slack_webhook")
965965

966-
viper.SetDefault(LogAnalysis.SlackChannel, DefaultNotificationsChannel)
966+
viper.SetDefault(LogAnalysis.SlackChannel, defaultNotificationsChannel)
967967
_ = viper.BindEnv(LogAnalysis.SlackChannel, "LOG_ANALYSIS_SLACK_CHANNEL")
968968

969969
// ----- KrknAI Configuration -----

pkg/e2e/adhoctestimages/adhoctestimages.go

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,43 @@ var _ = ginkgo.Describe("Ad Hoc Test Images", ginkgo.Ordered, ginkgo.ContinueOnF
129129
}
130130
}
131131

132-
if len(allFailures) > 0 && viper.GetBool(config.LogAnalysis.EnableAnalysis) {
132+
if len(allFailures) > 0 {
133133
combinedErr := fmt.Errorf("failures in %s: %s", testImage, strings.Join(allFailures, "; "))
134-
runLogAnalysisForAdHocTestImage(ctx, logger, testSuite, combinedErr, exeConfig.OutputDir)
134+
var analysisContent string
135+
if viper.GetBool(config.LogAnalysis.EnableAnalysis) {
136+
analysisContent = runLogAnalysisForAdHocTestImage(ctx, logger, testSuite, combinedErr, exeConfig.OutputDir)
137+
}
138+
queueNotification(testSuite, analysisContent)
135139
}
136140
},
137141
testImageEntries)
138142
})
139143

140-
// runLogAnalysisForAdHocTestImage runs AI analysis and queues the result for
141-
// deferred Slack delivery. The engine runs without sending notifications because
142-
// S3 artifacts have not been uploaded yet at this point. The queued result is
143-
// later sent by Report after S3 upload populates presigned URLs.
144-
func runLogAnalysisForAdHocTestImage(ctx context.Context, logger logr.Logger, testSuite config.TestSuite, err error, artifactsDir string) {
144+
// queueNotification adds a PendingNotification for deferred Slack delivery.
145+
// Called directly when log analysis is disabled so notifications are still sent.
146+
func queueNotification(testSuite config.TestSuite, analysisContent string) {
147+
clusterInfo := &analysisengine.ClusterInfo{
148+
ID: viper.GetString(config.Cluster.ID),
149+
Name: viper.GetString(config.Cluster.Name),
150+
Provider: viper.GetString(config.Provider),
151+
Region: viper.GetString(config.CloudProvider.Region),
152+
CloudProvider: viper.GetString(config.CloudProvider.CloudProviderID),
153+
Version: viper.GetString(config.Cluster.Version),
154+
}
155+
pendingMu.Lock()
156+
pendingNotifications = append(pendingNotifications, PendingNotification{
157+
AnalysisContent: analysisContent,
158+
TestSuite: testSuite,
159+
ClusterInfo: clusterInfo,
160+
Env: viper.GetString(ocmprovider.Env),
161+
})
162+
pendingMu.Unlock()
163+
}
164+
165+
// runLogAnalysisForAdHocTestImage runs AI analysis and returns the result content.
166+
// Returns an empty string if analysis fails. The caller is responsible for
167+
// queuing the notification via queueNotification.
168+
func runLogAnalysisForAdHocTestImage(ctx context.Context, logger logr.Logger, testSuite config.TestSuite, err error, artifactsDir string) string {
145169
logger.Info("Running Log analysis for test image", "image", testSuite.Image, "slackChannel", testSuite.SlackChannel)
146170

147171
clusterInfo := &analysisengine.ClusterInfo{
@@ -166,24 +190,17 @@ func runLogAnalysisForAdHocTestImage(ctx context.Context, logger logr.Logger, te
166190
engine, err := analysisengine.New(ctx, engineConfig)
167191
if err != nil {
168192
logger.Error(err, "Unable to create analysis engine for image", "image", testSuite.Image)
169-
return
193+
return ""
170194
}
171195

172196
result, runErr := engine.Run(ctx)
173197
if runErr != nil {
174198
logger.Error(runErr, "Log analysis failed for image", "image", testSuite.Image)
175-
return
199+
return ""
176200
}
177201

178202
logger.Info("Log analysis completed successfully", "image", testSuite.Image, "resultsDir", fmt.Sprintf("%s/%s/", artifactsDir, analysisengine.AnalysisDirName))
179203
log.Printf("=== Log Analysis Result for %s ===\n%s", testSuite.Image, result.Content)
180204

181-
pendingMu.Lock()
182-
pendingNotifications = append(pendingNotifications, PendingNotification{
183-
AnalysisContent: result.Content,
184-
TestSuite: testSuite,
185-
ClusterInfo: clusterInfo,
186-
Env: viper.GetString(ocmprovider.Env),
187-
})
188-
pendingMu.Unlock()
205+
return result.Content
189206
}

pkg/e2e/e2e.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,12 @@ func (o *E2EOrchestrator) Report(ctx context.Context) error {
276276
}
277277
}
278278

279-
// Send deferred per-suite notifications first; fall back to global notification
280-
// when no per-suite notifications were sent
281-
deferredSent := o.sendDeferredNotifications(ctx)
282-
if !deferredSent && o.result.ExitCode != config.Success && viper.GetBool(config.Tests.EnableSlackNotify) {
279+
// Drain per-suite pending notifications. If any were queued (suites ran),
280+
// send them; otherwise fall back to global failure notification.
281+
pending := adhoctestimages.DrainPendingNotifications()
282+
if len(pending) > 0 {
283+
o.sendDeferredNotifications(ctx, pending)
284+
} else if o.result.ExitCode != config.Success && viper.GetBool(config.Tests.EnableSlackNotify) {
283285
o.sendFailureNotification(ctx)
284286
}
285287

@@ -294,8 +296,8 @@ func (o *E2EOrchestrator) Report(ctx context.Context) error {
294296
func (o *E2EOrchestrator) sendFailureNotification(ctx context.Context) {
295297
reportDir := viper.GetString(config.ReportDir)
296298
channel := viper.GetString(config.Tests.SlackChannel)
297-
if ch := viper.GetString(config.LogAnalysis.SlackChannel); ch != "" && ch != config.DefaultNotificationsChannel {
298-
channel = ch
299+
if channel == "" {
300+
channel = viper.GetString(config.LogAnalysis.SlackChannel)
299301
}
300302
notificationConfig := slack.BuildNotificationConfig(
301303
viper.GetString(config.LogAnalysis.SlackWebhook),
@@ -345,22 +347,15 @@ func (o *E2EOrchestrator) sendFailureNotification(ctx context.Context) {
345347
}
346348
}
347349

348-
// sendDeferredNotifications delivers Slack notifications that were queued by
349-
// adhoctestimages during test execution. Called by Report after S3 upload so
350-
// that presigned URLs are available for inclusion in the message.
351-
// Returns true if at least one notification was sent.
352-
func (o *E2EOrchestrator) sendDeferredNotifications(ctx context.Context) bool {
353-
pending := adhoctestimages.DrainPendingNotifications()
354-
if len(pending) == 0 {
355-
return false
356-
}
357-
350+
// sendDeferredNotifications delivers the given Slack notifications that were
351+
// queued by adhoctestimages during test execution. Called by Report after S3
352+
// upload so that presigned URLs are available for inclusion in the message.
353+
func (o *E2EOrchestrator) sendDeferredNotifications(ctx context.Context, pending []adhoctestimages.PendingNotification) {
358354
webhook := viper.GetString(config.LogAnalysis.SlackWebhook)
359355
if webhook == "" || !viper.GetBool(config.Tests.EnableSlackNotify) {
360-
return false
356+
return
361357
}
362358

363-
sent := false
364359
artifactLinks := s3ResultsToArtifactLinks(o.s3Results)
365360
slackReporter := slack.NewSlackReporter()
366361

@@ -390,11 +385,8 @@ func (o *E2EOrchestrator) sendDeferredNotifications(ctx context.Context) bool {
390385

391386
if err := slackReporter.Report(ctx, result, &cfg); err != nil {
392387
log.Printf("Failed to send deferred notification for %s: %v", p.TestSuite.Image, err)
393-
} else {
394-
sent = true
395388
}
396389
}
397-
return sent
398390
}
399391

400392
// uploadToS3 uploads the report directory contents to S3 and caches results.

0 commit comments

Comments
 (0)