1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-01-20 18:52:16 +00:00
kyverno/pkg/namespace/report.go

64 lines
2.3 KiB
Go
Raw Normal View History

2019-08-14 14:56:53 -07:00
package namespace
import (
"fmt"
"github.com/golang/glog"
"github.com/nirmata/kyverno/pkg/engine/response"
2019-08-14 14:56:53 -07:00
"github.com/nirmata/kyverno/pkg/event"
"github.com/nirmata/kyverno/pkg/policyviolation"
2019-08-14 14:56:53 -07:00
)
func (nsc *NamespaceController) report(engineResponses []response.EngineResponse) {
2019-08-14 14:56:53 -07:00
// generate events
2019-11-12 14:41:29 -08:00
eventInfos := generateEvents(engineResponses)
nsc.eventGen.Add(eventInfos...)
2019-08-14 14:56:53 -07:00
// generate policy violations
pvInfos := policyviolation.GeneratePVsFromEngineResponse(engineResponses)
2019-11-12 14:41:29 -08:00
nsc.pvGenerator.Add(pvInfos...)
}
func generateEvents(ers []response.EngineResponse) []event.Info {
2019-11-12 14:41:29 -08:00
var eventInfos []event.Info
for _, er := range ers {
if er.IsSuccesful() {
continue
}
eventInfos = append(eventInfos, generateEventsPerEr(er)...)
}
return eventInfos
}
func generateEventsPerEr(er response.EngineResponse) []event.Info {
2019-11-12 14:41:29 -08:00
var eventInfos []event.Info
glog.V(4).Infof("reporting results for policy '%s' application on resource '%s/%s/%s'", er.PolicyResponse.Policy, er.PolicyResponse.Resource.Kind, er.PolicyResponse.Resource.Namespace, er.PolicyResponse.Resource.Name)
for _, rule := range er.PolicyResponse.Rules {
if rule.Success {
continue
2019-08-14 14:56:53 -07:00
}
// generate event on resource for each failed rule
2019-11-12 14:41:29 -08:00
glog.V(4).Infof("generation event on resource '%s/%s' for policy '%s'", er.PolicyResponse.Resource.Kind, er.PolicyResponse.Resource.Name, er.PolicyResponse.Policy)
2019-08-26 13:34:42 -07:00
e := event.Info{}
2019-11-12 14:41:29 -08:00
e.Kind = er.PolicyResponse.Resource.Kind
e.Namespace = "" // event generate on namespace resource
2019-11-12 14:41:29 -08:00
e.Name = er.PolicyResponse.Resource.Name
2019-08-14 14:56:53 -07:00
e.Reason = "Failure"
e.Source = event.GeneratePolicyController
2019-11-18 17:13:48 -08:00
e.Message = fmt.Sprintf("policy '%s' (%s) rule '%s' not satisfied. %v", er.PolicyResponse.Policy, rule.Type, rule.Name, rule.Message)
2019-11-12 14:41:29 -08:00
eventInfos = append(eventInfos, e)
}
if er.IsSuccesful() {
return eventInfos
2019-08-14 14:56:53 -07:00
}
// generate a event on policy for all failed rules
2019-11-12 14:41:29 -08:00
glog.V(4).Infof("generation event on policy '%s'", er.PolicyResponse.Policy)
2019-08-26 13:34:42 -07:00
e := event.Info{}
e.Kind = "ClusterPolicy"
2019-08-14 14:56:53 -07:00
e.Namespace = ""
2019-11-12 14:41:29 -08:00
e.Name = er.PolicyResponse.Policy
2019-08-14 14:56:53 -07:00
e.Reason = "Failure"
e.Source = event.GeneratePolicyController
2019-11-18 17:13:48 -08:00
e.Message = fmt.Sprintf("policy '%s' rules '%v' on resource '%s/%s/%s' not stasified", er.PolicyResponse.Policy, er.GetFailedRules(), er.PolicyResponse.Resource.Kind, er.PolicyResponse.Resource.Namespace, er.PolicyResponse.Resource.Name)
2019-11-12 14:41:29 -08:00
return eventInfos
2019-08-14 14:56:53 -07:00
}