1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-09 17:37:12 +00:00
kyverno/pkg/violation/builder.go

277 lines
7.5 KiB
Go
Raw Normal View History

2019-05-13 18:17:28 -07:00
package violation
2019-04-26 18:16:22 -07:00
import (
"errors"
2019-07-31 17:07:30 -07:00
"github.com/golang/glog"
2019-07-15 14:49:22 -07:00
v1alpha1 "github.com/nirmata/kyverno/pkg/apis/policy/v1alpha1"
lister "github.com/nirmata/kyverno/pkg/client/listers/policy/v1alpha1"
client "github.com/nirmata/kyverno/pkg/dclient"
2019-05-21 11:00:09 -07:00
event "github.com/nirmata/kyverno/pkg/event"
2019-07-15 14:49:22 -07:00
"github.com/nirmata/kyverno/pkg/info"
2019-05-21 11:00:09 -07:00
"github.com/nirmata/kyverno/pkg/sharedinformer"
2019-07-18 10:22:20 -07:00
"k8s.io/apimachinery/pkg/runtime"
)
2019-05-10 10:38:38 -07:00
//Generator to generate policy violation
type Generator interface {
2019-06-27 11:43:07 -07:00
Add(infos ...*Info) error
2019-07-15 14:49:22 -07:00
RemoveInactiveViolation(policy, rKind, rNs, rName string, ruleType info.RuleType) error
ResourceRemoval(policy, rKind, rNs, rName string) error
2019-04-26 18:16:22 -07:00
}
2019-05-10 10:38:38 -07:00
type builder struct {
2019-05-15 12:29:09 -07:00
client *client.Client
2019-07-15 14:49:22 -07:00
policyLister lister.PolicyLister
2019-05-15 12:29:09 -07:00
eventBuilder event.Generator
}
2019-05-10 10:38:38 -07:00
//Builder is to build policy violations
type Builder interface {
Generator
2019-06-27 11:43:07 -07:00
processViolation(info *Info) error
2019-05-10 00:05:21 -07:00
}
2019-05-10 10:38:38 -07:00
//NewPolicyViolationBuilder returns new violation builder
func NewPolicyViolationBuilder(client *client.Client,
2019-05-15 12:29:09 -07:00
sharedInfomer sharedinformer.PolicyInformer,
eventController event.Generator) Builder {
2019-05-10 10:38:38 -07:00
builder := &builder{
2019-05-15 12:29:09 -07:00
client: client,
policyLister: sharedInfomer.GetLister(),
eventBuilder: eventController,
2019-05-06 12:08:31 -07:00
}
2019-05-10 00:05:21 -07:00
return builder
}
2019-07-19 15:10:40 -07:00
//BuldNewViolation returns a new violation
func BuldNewViolation(pName string, rKind string, rNs string, rName string, reason string, frules []v1alpha1.FailedRule) *Info {
return &Info{
Policy: pName,
Violation: v1alpha1.Violation{
Kind: rKind,
Namespace: rNs,
Name: rName,
Reason: reason,
Rules: frules,
},
}
}
2019-06-27 11:43:07 -07:00
func (b *builder) Add(infos ...*Info) error {
2019-07-18 10:22:20 -07:00
if infos == nil {
return nil
}
2019-06-27 11:43:07 -07:00
for _, info := range infos {
2019-07-31 17:07:30 -07:00
err := b.processViolation(info)
if err != nil {
glog.Error(err)
}
2019-06-27 11:43:07 -07:00
}
return nil
}
2019-06-27 11:43:07 -07:00
func (b *builder) processViolation(info *Info) error {
statusMap := map[string]interface{}{}
2019-07-18 10:22:20 -07:00
violationsMap := map[string]interface{}{}
violationMap := map[string]interface{}{}
var violations interface{}
var violation interface{}
// Get Policy
obj, err := b.client.GetResource("Policy", "", info.Policy, "status")
if err != nil {
2019-04-30 17:13:44 -07:00
return err
}
2019-07-18 10:22:20 -07:00
unstr := obj.UnstructuredContent()
// get "status" subresource
status, ok := unstr["status"]
if ok {
2019-07-18 10:22:20 -07:00
// status exists
// status is already present then we append violations
if statusMap, ok = status.(map[string]interface{}); !ok {
return errors.New("Unable to parse status subresource")
}
2019-07-18 10:22:20 -07:00
// get policy violations
violations, ok = statusMap["violations"]
if !ok {
2019-07-18 10:22:20 -07:00
return nil
}
2019-07-18 10:22:20 -07:00
violationsMap, ok = violations.(map[string]interface{})
if !ok {
2019-07-18 10:22:20 -07:00
return errors.New("Unable to get status.violations subresource")
}
2019-07-18 10:22:20 -07:00
// check if the resource has a violation
violation, ok = violationsMap[info.getKey()]
if !ok {
2019-07-18 10:22:20 -07:00
// add resource violation
violationsMap[info.getKey()] = info.Violation
statusMap["violations"] = violationsMap
unstr["status"] = statusMap
} else {
violationMap, ok = violation.(map[string]interface{})
if !ok {
return errors.New("Unable to get status.violations.violation subresource")
}
// we check if the new violation updates are different from stored violation info
v := v1alpha1.Violation{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(violationMap, &v)
if err != nil {
return err
}
// compare v & info.Violation
if v.IsEqual(info.Violation) {
// no updates to violation
// do nothing
return nil
}
// update the violation
violationsMap[info.getKey()] = info.Violation
statusMap["violations"] = violationsMap
unstr["status"] = statusMap
}
2019-07-18 10:22:20 -07:00
} else {
violationsMap[info.getKey()] = info.Violation
statusMap["violations"] = violationsMap
unstr["status"] = statusMap
}
obj.SetUnstructuredContent(unstr)
// update the status sub-resource for policy
_, err = b.client.UpdateStatusResource("Policy", "", obj, false)
if err != nil {
return err
}
return nil
}
2019-07-19 15:10:40 -07:00
//RemoveInactiveViolation
2019-07-18 10:22:20 -07:00
func (b *builder) RemoveInactiveViolation(policy, rKind, rNs, rName string, ruleType info.RuleType) error {
statusMap := map[string]interface{}{}
violationsMap := map[string]interface{}{}
violationMap := map[string]interface{}{}
var violations interface{}
var violation interface{}
// Get Policy
obj, err := b.client.GetResource("Policy", "", policy, "status")
if err != nil {
return err
}
unstr := obj.UnstructuredContent()
// get "status" subresource
status, ok := unstr["status"]
if !ok {
return nil
}
// status exists
// status is already present then we append violations
if statusMap, ok = status.(map[string]interface{}); !ok {
return errors.New("Unable to parse status subresource")
}
// get policy violations
violations, ok = statusMap["violations"]
if !ok {
return nil
}
violationsMap, ok = violations.(map[string]interface{})
if !ok {
return errors.New("Unable to get status.violations subresource")
}
// check if the resource has a violation
violation, ok = violationsMap[BuildKey(rKind, rNs, rName)]
if !ok {
// no violation for this resource
return nil
}
violationMap, ok = violation.(map[string]interface{})
if !ok {
return errors.New("Unable to get status.violations.violation subresource")
}
// check remove the rules of the given type
// this is called when the policy is applied succesfully, so we can remove the previous failed rules
// if all rules are to be removed, the deleted the violation
v := v1alpha1.Violation{}
err = runtime.DefaultUnstructuredConverter.FromUnstructured(violationMap, &v)
if err != nil {
return err
}
if !v.RemoveRulesOfType(ruleType.String()) {
// no rule of given type found,
// no need to remove rule
return nil
}
// if there are no faile rules remove the violation
if len(v.Rules) == 0 {
delete(violationsMap, BuildKey(rKind, rNs, rName))
} else {
// update the rules
violationsMap[BuildKey(rKind, rNs, rName)] = v
}
2019-07-18 10:22:20 -07:00
statusMap["violations"] = violationsMap
unstr["status"] = statusMap
2019-07-18 10:22:20 -07:00
obj.SetUnstructuredContent(unstr)
// update the status sub-resource for policy
_, err = b.client.UpdateStatusResource("Policy", "", obj, false)
2019-05-10 00:05:21 -07:00
if err != nil {
return err
}
return nil
2019-04-30 17:13:44 -07:00
}
2019-07-19 15:10:40 -07:00
// ResourceRemoval on resources reoval we remove the policy violation in the policy
2019-07-18 10:22:20 -07:00
func (b *builder) ResourceRemoval(policy, rKind, rNs, rName string) error {
statusMap := map[string]interface{}{}
violationsMap := map[string]interface{}{}
var violations interface{}
// Get Policy
obj, err := b.client.GetResource("Policy", "", policy, "status")
if err != nil {
return err
}
unstr := obj.UnstructuredContent()
// get "status" subresource
status, ok := unstr["status"]
if !ok {
return nil
}
// status exists
// status is already present then we append violations
if statusMap, ok = status.(map[string]interface{}); !ok {
return errors.New("Unable to parse status subresource")
}
// get policy violations
violations, ok = statusMap["violations"]
if !ok {
return nil
}
violationsMap, ok = violations.(map[string]interface{})
if !ok {
return errors.New("Unable to get status.violations subresource")
}
// check if the resource has a violation
_, ok = violationsMap[BuildKey(rKind, rNs, rName)]
if !ok {
// no violation for this resource
return nil
}
// remove the pair from the map
delete(violationsMap, BuildKey(rKind, rNs, rName))
if len(violationsMap) == 0 {
delete(statusMap, "violations")
} else {
statusMap["violations"] = violationsMap
}
unstr["status"] = statusMap
obj.SetUnstructuredContent(unstr)
// update the status sub-resource for policy
_, err = b.client.UpdateStatusResource("Policy", "", obj, false)
if err != nil {
return err
}
return nil
}