2019-07-15 16:07:56 -07:00
|
|
|
package webhooks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
"github.com/nirmata/kyverno/pkg/event"
|
|
|
|
"github.com/nirmata/kyverno/pkg/info"
|
|
|
|
)
|
|
|
|
|
2019-07-16 15:53:14 -07:00
|
|
|
//TODO: change validation from bool -> enum(validation, mutation)
|
2019-08-14 10:01:47 -07:00
|
|
|
func newEventInfoFromPolicyInfo(policyInfoList []info.PolicyInfo, onUpdate bool, ruleType info.RuleType) []*event.Info {
|
2019-07-15 16:07:56 -07:00
|
|
|
var eventsInfo []*event.Info
|
|
|
|
ok, msg := isAdmSuccesful(policyInfoList)
|
|
|
|
// Some policies failed to apply succesfully
|
|
|
|
if !ok {
|
|
|
|
for _, pi := range policyInfoList {
|
2019-07-16 15:53:14 -07:00
|
|
|
if pi.IsSuccessful() {
|
|
|
|
continue
|
|
|
|
}
|
2019-07-15 16:07:56 -07:00
|
|
|
rules := pi.FailedRules()
|
|
|
|
ruleNames := strings.Join(rules, ";")
|
|
|
|
if !onUpdate {
|
|
|
|
// CREATE
|
|
|
|
eventsInfo = append(eventsInfo,
|
2019-08-06 11:30:44 -07:00
|
|
|
event.NewEvent(policyKind, "", pi.Name, event.RequestBlocked, event.FPolicyApplyBlockCreate, pi.RNamespace+"/"+pi.RName, ruleNames))
|
2019-07-15 16:07:56 -07:00
|
|
|
|
|
|
|
glog.V(3).Infof("Rule(s) %s of policy %s blocked resource creation, error: %s\n", ruleNames, pi.Name, msg)
|
|
|
|
} else {
|
|
|
|
// UPDATE
|
|
|
|
eventsInfo = append(eventsInfo,
|
|
|
|
event.NewEvent(pi.RKind, pi.RNamespace, pi.RName, event.RequestBlocked, event.FPolicyApplyBlockUpdate, ruleNames, pi.Name))
|
|
|
|
eventsInfo = append(eventsInfo,
|
2019-08-06 11:30:44 -07:00
|
|
|
event.NewEvent(policyKind, "", pi.Name, event.RequestBlocked, event.FPolicyBlockResourceUpdate, pi.RNamespace+"/"+pi.RName, ruleNames))
|
2019-07-15 16:07:56 -07:00
|
|
|
glog.V(3).Infof("Request blocked events info has prepared for %s/%s and %s/%s\n", policyKind, pi.Name, pi.RKind, pi.RName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !onUpdate {
|
|
|
|
// All policies were applied succesfully
|
|
|
|
// CREATE
|
|
|
|
for _, pi := range policyInfoList {
|
|
|
|
rules := pi.SuccessfulRules()
|
|
|
|
ruleNames := strings.Join(rules, ";")
|
|
|
|
eventsInfo = append(eventsInfo,
|
|
|
|
event.NewEvent(pi.RKind, pi.RNamespace, pi.RName, event.PolicyApplied, event.SRulesApply, ruleNames, pi.Name))
|
|
|
|
|
|
|
|
glog.V(3).Infof("Success event info has prepared for %s/%s\n", pi.RKind, pi.RName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-14 10:01:47 -07:00
|
|
|
return eventsInfo
|
2019-07-17 17:53:13 -07:00
|
|
|
}
|