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

105 lines
3.3 KiB
Go
Raw Normal View History

2019-05-13 18:17:28 -07:00
package engine
2019-05-09 22:26:22 -07:00
import (
2019-06-25 22:53:18 -07:00
jsonpatch "github.com/evanphx/json-patch"
"github.com/golang/glog"
2019-05-21 11:00:09 -07:00
types "github.com/nirmata/kyverno/pkg/apis/policy/v1alpha1"
2019-06-25 22:53:18 -07:00
client "github.com/nirmata/kyverno/pkg/dclient"
"github.com/nirmata/kyverno/pkg/info"
2019-07-31 17:43:46 -07:00
"github.com/nirmata/kyverno/pkg/utils"
2019-06-25 22:53:18 -07:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2019-05-09 22:26:22 -07:00
)
2019-07-23 23:34:03 -04:00
// ProcessExisting checks for mutation and validation violations of existing resources
2019-07-31 17:43:46 -07:00
func ProcessExisting(client *client.Client, policy *types.Policy, filterK8Resources []utils.K8Resource) []*info.PolicyInfo {
2019-07-23 23:34:03 -04:00
glog.Infof("Applying policy %s on existing resources", policy.Name)
// key uid
2019-07-31 17:43:46 -07:00
resourceMap := ListResourcesThatApplyToPolicy(client, policy, filterK8Resources)
2019-06-25 22:53:18 -07:00
policyInfos := []*info.PolicyInfo{}
// for the filtered resource apply policy
2019-07-17 23:13:28 -07:00
for _, v := range resourceMap {
2019-06-25 22:53:18 -07:00
2019-07-17 23:13:28 -07:00
policyInfo, err := applyPolicy(client, policy, v)
2019-06-25 22:53:18 -07:00
if err != nil {
2019-07-23 23:34:03 -04:00
glog.Errorf("unable to apply policy %s on resource %s/%s", policy.Name, v.Resource.GetName(), v.Resource.GetNamespace())
2019-06-25 22:53:18 -07:00
glog.Error(err)
continue
}
policyInfos = append(policyInfos, policyInfo)
}
return policyInfos
}
func applyPolicy(client *client.Client, policy *types.Policy, res resourceInfo) (*info.PolicyInfo, error) {
2019-07-23 23:34:03 -04:00
policyInfo := info.NewPolicyInfo(policy.Name, res.Gvk.Kind, res.Resource.GetName(), res.Resource.GetNamespace(), policy.Spec.ValidationFailureAction)
2019-06-25 22:53:18 -07:00
glog.Infof("Applying policy %s with %d rules\n", policy.ObjectMeta.Name, len(policy.Spec.Rules))
2019-07-23 23:34:03 -04:00
rawResource, err := res.Resource.MarshalJSON()
2019-06-25 22:53:18 -07:00
if err != nil {
return nil, err
}
// Mutate
2019-07-23 23:34:03 -04:00
mruleInfos, err := mutation(policy, rawResource, res.Gvk)
2019-06-25 22:53:18 -07:00
policyInfo.AddRuleInfos(mruleInfos)
if err != nil {
return nil, err
}
// Validation
2019-07-23 23:34:03 -04:00
vruleInfos, err := Validate(*policy, rawResource, *res.Gvk)
2019-06-25 22:53:18 -07:00
policyInfo.AddRuleInfos(vruleInfos)
if err != nil {
return nil, err
}
2019-07-23 23:34:03 -04:00
if res.Gvk.Kind == "Namespace" {
// Generation
2019-07-26 08:11:20 -04:00
gruleInfos := Generate(client, policy, res.Resource)
policyInfo.AddRuleInfos(gruleInfos)
}
2019-06-25 22:53:18 -07:00
return policyInfo, nil
}
func mutation(p *types.Policy, rawResource []byte, gvk *metav1.GroupVersionKind) ([]*info.RuleInfo, error) {
patches, ruleInfos := Mutate(*p, rawResource, *gvk)
2019-07-18 10:22:20 -07:00
if len(ruleInfos) == 0 {
// no rules were processed
return nil, nil
}
2019-07-23 00:10:18 -04:00
// if there are any errors return
for _, r := range ruleInfos {
if !r.IsSuccessful() {
return ruleInfos, nil
}
}
// if there are no patches // for overlay
if len(patches) == 0 {
return ruleInfos, nil
}
2019-06-25 22:53:18 -07:00
// option 2: (original Resource + patch) compare with (original resource)
mergePatches := JoinPatches(patches)
// merge the patches
patch, err := jsonpatch.DecodePatch(mergePatches)
if err != nil {
return nil, err
}
// apply the patches returned by mutate to the original resource
patchedResource, err := patch.Apply(rawResource)
if err != nil {
return nil, err
}
// compare (original Resource + patch) vs (original resource)
// to verify if they are equal
2019-06-26 15:31:18 -07:00
ruleInfo := info.NewRuleInfo("over-all mutation", info.Mutation)
2019-06-25 22:53:18 -07:00
if !jsonpatch.Equal(patchedResource, rawResource) {
//resource does not match so there was a mutation rule violated
// TODO : check the rule name "mutation rules"
ruleInfo.Fail()
ruleInfo.Add("resource does not satisfy mutation rules")
} else {
ruleInfo.Add("resource satisfys the mutation rule")
}
ruleInfos = append(ruleInfos, ruleInfo)
return ruleInfos, nil
2019-05-14 18:20:41 -07:00
}