-
Notifications
You must be signed in to change notification settings - Fork 291
/
resource_events.go
53 lines (43 loc) · 1.57 KB
/
resource_events.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
package recommendation
import (
"github.com/kubeshop/botkube/internal/source/kubernetes/config"
"github.com/kubeshop/botkube/internal/source/kubernetes/event"
"github.com/kubeshop/botkube/pkg/ptr"
)
const (
podsResourceType = "v1/pods"
ingressResourceType = "networking.k8s.io/v1/ingresses"
)
// ResourceEventsForConfig returns the resource event map for a given source recommendations config.
func ResourceEventsForConfig(recCfg *config.Recommendations) map[string]config.EventType {
resTypes := make(map[string]config.EventType)
if recCfg == nil {
return resTypes
}
if ptr.ToValue(recCfg.Ingress.TLSSecretValid) || ptr.ToValue(recCfg.Ingress.BackendServiceValid) {
resTypes[ingressResourceType] = config.CreateEvent
}
if ptr.ToValue(recCfg.Pod.NoLatestImageTag) || ptr.ToValue(recCfg.Pod.LabelsSet) {
resTypes[podsResourceType] = config.CreateEvent
}
return resTypes
}
// ShouldIgnoreEvent returns true if user doesn't listen to events for a given resource, apart from enabled recommendations.
func ShouldIgnoreEvent(recCfg *config.Recommendations, event event.Event) bool {
if event.HasRecommendationsOrWarnings() {
// shouldn't be skipped
return false
}
res := ResourceEventsForConfig(recCfg)
recommEventType, ok := res[event.Resource]
if !ok {
// this event doesn't relate to recommendations, finish early
return false
}
if event.Type != recommEventType {
// this event doesn't relate to recommendations, finish early
return false
}
// The event is related to recommendations informers. No recommendations there, so it should be skipped.
return true
}