-
Notifications
You must be signed in to change notification settings - Fork 876
/
report.go
90 lines (80 loc) · 2.36 KB
/
report.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package webhooks
import (
"strings"
"github.com/go-logr/logr"
kyvernov1alpha2 "github.com/kyverno/kyverno/api/kyverno/v1alpha2"
"github.com/kyverno/kyverno/pkg/engine/response"
"github.com/kyverno/kyverno/pkg/event"
)
//generateEvents generates event info for the engine responses
func generateEvents(engineResponses []*response.EngineResponse, blocked, onUpdate bool, log logr.Logger) []event.Info {
var events []event.Info
// - Admission-Response is SUCCESS
// - Some/All policies failed (policy violations generated)
// - report failure event on policy
// - report failure event on resource
// - Some/All policies succeeded
// - report success event on policy
// - report success event on resource
for _, er := range engineResponses {
if !er.IsSuccessful() {
// Rules that failed
failedRules := er.GetFailedRules()
failedRulesStr := strings.Join(failedRules, ";")
// Event on the policy
kind := "ClusterPolicy"
if er.PolicyResponse.Policy.Namespace != "" {
kind = "Policy"
}
pe := event.NewEvent(
log,
kind,
kyvernov1alpha2.SchemeGroupVersion.String(),
er.PolicyResponse.Policy.Namespace,
er.PolicyResponse.Policy.Name,
event.PolicyViolation.String(),
event.AdmissionController,
event.FPolicyApply,
failedRulesStr,
er.PolicyResponse.Resource.GetKey(),
)
// Event on the resource
re := event.NewEvent(
log,
er.PolicyResponse.Resource.Kind,
er.PolicyResponse.Resource.APIVersion,
er.PolicyResponse.Resource.Namespace,
er.PolicyResponse.Resource.Name,
event.PolicyViolation.String(),
event.AdmissionController,
event.FResourcePolicyApply,
failedRulesStr,
er.PolicyResponse.Policy.Name,
)
events = append(events, pe, re)
}
if !er.IsFailed() {
successRules := er.GetSuccessRules()
successRulesStr := strings.Join(successRules, ";")
// Event on the policy
kind := "ClusterPolicy"
if er.PolicyResponse.Policy.Namespace != "" {
kind = "Policy"
}
e := event.NewEvent(
log,
kind,
kyvernov1alpha2.SchemeGroupVersion.String(),
er.PolicyResponse.Policy.Namespace,
er.PolicyResponse.Policy.Name,
event.PolicyApplied.String(),
event.AdmissionController,
event.SPolicyApply,
successRulesStr,
er.PolicyResponse.Resource.GetKey(),
)
events = append(events, e)
}
}
return events
}