-
Notifications
You must be signed in to change notification settings - Fork 290
/
aggregated_runner.go
48 lines (38 loc) · 1.25 KB
/
aggregated_runner.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
package recommendation
import (
"context"
"errors"
"fmt"
"github.com/sirupsen/logrus"
"github.com/kubeshop/botkube/internal/source/kubernetes/event"
"github.com/kubeshop/botkube/pkg/multierror"
)
// AggregatedRunner contains multiple recommendations to run.
type AggregatedRunner struct {
log logrus.FieldLogger
recommendations []Recommendation
}
func newAggregatedRunner(log logrus.FieldLogger, recommendations []Recommendation) AggregatedRunner {
return AggregatedRunner{log: log, recommendations: recommendations}
}
// Do runs all recommendations within the set.
func (s AggregatedRunner) Do(ctx context.Context, event *event.Event) error {
if len(s.recommendations) == 0 {
s.log.Debug("No recommendations to run. Finishing...")
return nil
}
if event == nil {
return errors.New("event is nil")
}
errs := multierror.New()
for _, r := range s.recommendations {
s.log.Debugf("Running recommendation %q...", r.Name())
result, err := r.Do(ctx, *event)
if err != nil {
errs = multierror.Append(errs, fmt.Errorf("while running recommendation %q: %w", r.Name(), err))
}
event.Recommendations = append(event.Recommendations, result.Info...)
event.Warnings = append(event.Warnings, result.Warnings...)
}
return errs.ErrorOrNil()
}