1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-09 09:26:54 +00:00
kyverno/pkg/gencontroller/generation.go

103 lines
3.2 KiB
Go
Raw Normal View History

2019-07-03 10:25:00 -07:00
package gencontroller
import (
2019-07-08 17:51:37 -07:00
"fmt"
"strings"
2019-07-03 10:25:00 -07:00
"github.com/golang/glog"
v1alpha1 "github.com/nirmata/kyverno/pkg/apis/policy/v1alpha1"
"github.com/nirmata/kyverno/pkg/engine"
2019-07-08 16:53:34 -07:00
event "github.com/nirmata/kyverno/pkg/event"
2019-07-03 10:25:00 -07:00
"github.com/nirmata/kyverno/pkg/info"
2019-07-08 17:51:37 -07:00
violation "github.com/nirmata/kyverno/pkg/violation"
2019-07-03 10:25:00 -07:00
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
)
func (c *Controller) processNamespace(ns *corev1.Namespace) error {
//Get all policies and then verify if the namespace matches any of the defined selectors
policies, err := c.listPolicies(ns)
if err != nil {
return err
}
// process policy on namespace
for _, p := range policies {
c.processPolicy(ns, p)
}
return nil
}
func (c *Controller) listPolicies(ns *corev1.Namespace) ([]*v1alpha1.Policy, error) {
var fpolicies []*v1alpha1.Policy
policies, err := c.policyLister.List(labels.NewSelector())
if err != nil {
glog.Error("Unable to connect to policy controller. Unable to access policies not applying GENERATION rules")
return nil, err
}
for _, p := range policies {
// Check if the policy contains a generatoin rule
for _, r := range p.Spec.Rules {
if r.Generation != nil {
// Check if the resource meets the description
if namespaceMeetsRuleDescription(ns, r.ResourceDescription) {
fpolicies = append(fpolicies, p)
break
}
}
}
}
return fpolicies, nil
}
2019-07-08 16:53:34 -07:00
func (c *Controller) processPolicy(ns *corev1.Namespace, p *v1alpha1.Policy) {
var eventInfo *event.Info
2019-07-08 17:51:37 -07:00
var onViolation bool
var msg string
2019-07-08 16:53:34 -07:00
2019-07-03 10:25:00 -07:00
policyInfo := info.NewPolicyInfo(p.Name,
2019-07-08 16:53:34 -07:00
"Namespace",
2019-07-03 10:25:00 -07:00
ns.Name,
"",
p.Spec.ValidationFailureAction) // Namespace has no namespace..WOW
2019-07-03 10:25:00 -07:00
2019-07-05 11:24:18 -07:00
ruleInfos := engine.GenerateNew(c.client, p, ns)
2019-07-03 10:25:00 -07:00
policyInfo.AddRuleInfos(ruleInfos)
2019-07-08 16:53:34 -07:00
2019-07-03 10:25:00 -07:00
if !policyInfo.IsSuccessful() {
glog.Infof("Failed to apply policy %s on resource %s %s", p.Name, ns.Kind, ns.Name)
for _, r := range ruleInfos {
glog.Warning(r.Msgs)
2019-07-08 17:51:37 -07:00
if msg = strings.Join(r.Msgs, " "); strings.Contains(msg, "rule configuration not present in resource") {
onViolation = true
msg = fmt.Sprintf(`Resource creation violates generate rule '%s' of policy '%s'`, r.Name, policyInfo.Name)
}
2019-07-03 10:25:00 -07:00
}
2019-07-08 16:53:34 -07:00
2019-07-08 17:51:37 -07:00
if onViolation {
glog.Infof("Adding violation for generation rule of policy %s\n", policyInfo.Name)
2019-07-19 15:10:40 -07:00
v := violation.BuldNewViolation(policyInfo.Name, policyInfo.RKind, policyInfo.RNamespace, policyInfo.RName, event.PolicyViolation.String(), policyInfo.GetFailedRules())
2019-07-08 17:51:37 -07:00
c.violationBuilder.Add(v)
} else {
eventInfo = event.NewEvent(policyKind, "", policyInfo.Name, event.RequestBlocked,
event.FPolicyApplyBlockCreate, policyInfo.RName, policyInfo.GetRuleNames(false))
2019-07-08 16:53:34 -07:00
2019-07-08 17:51:37 -07:00
glog.V(2).Infof("Request blocked event info has prepared for %s/%s\n", policyKind, policyInfo.Name)
c.eventController.Add(eventInfo)
}
2019-07-08 16:53:34 -07:00
return
2019-07-03 10:25:00 -07:00
}
2019-07-08 16:53:34 -07:00
glog.Infof("Generation from policy %s has succesfully applied to %s/%s", p.Name, policyInfo.RKind, policyInfo.RName)
eventInfo = event.NewEvent(policyInfo.RKind, policyInfo.RNamespace, policyInfo.RName,
event.PolicyApplied, event.SRulesApply, policyInfo.GetRuleNames(true), policyInfo.Name)
2019-07-08 17:51:37 -07:00
glog.V(2).Infof("Success event info has prepared for %s/%s\n", policyInfo.RKind, policyInfo.RName)
2019-07-08 16:53:34 -07:00
c.eventController.Add(eventInfo)
2019-07-03 10:25:00 -07:00
}