1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/pkg/engine/mutation.go

81 lines
2.7 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 (
"reflect"
"github.com/golang/glog"
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1alpha1"
"github.com/nirmata/kyverno/pkg/info"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2019-05-09 22:26:22 -07:00
)
// Mutate performs mutation. Overlay first and then mutation patches
2019-08-09 12:59:37 -07:00
//TODO: check if gvk needs to be passed or can be set in resource
func Mutate(policy kyverno.Policy, resource unstructured.Unstructured) ([][]byte, []info.RuleInfo) {
2019-08-09 12:59:37 -07:00
//TODO: convert rawResource to unstructured to avoid unmarhalling all the time for get some resource information
var patches [][]byte
var ruleInfos []info.RuleInfo
2019-05-09 22:26:22 -07:00
for _, rule := range policy.Spec.Rules {
if reflect.DeepEqual(rule.Mutation, kyverno.Mutation{}) {
continue
2019-05-09 22:26:22 -07:00
}
2019-08-09 11:08:02 -07:00
// check if the resource satisfies the filter conditions defined in the rule
//TODO: this needs to be extracted, to filter the resource so that we can avoid passing resources that
// dont statisfy a policy rule resource description
ok := MatchesResourceDescription(resource, rule)
if !ok {
2019-08-09 12:59:37 -07:00
glog.V(4).Infof("resource %s/%s does not satisfy the resource description for the rule ", resource.GetNamespace(), resource.GetName())
continue
}
2019-08-09 12:59:37 -07:00
ruleInfo := info.NewRuleInfo(rule.Name, info.Mutation)
2019-08-09 11:08:02 -07:00
// Process Overlay
if rule.Mutation.Overlay != nil {
oPatches, err := processOverlay(resource, rule)
2019-07-25 16:20:22 -04:00
if err == nil {
2019-08-09 12:59:37 -07:00
if len(oPatches) == 0 {
2019-07-24 14:25:51 -04:00
// if array elements dont match then we skip(nil patch, no error)
// or if acnohor is defined and doenst match
// policy is not applicable
2019-08-09 12:59:37 -07:00
glog.V(4).Info("overlay does not match, so skipping applying rule")
2019-07-24 14:25:51 -04:00
continue
}
2019-08-09 12:59:37 -07:00
glog.V(4).Infof("overlay applied succesfully on resource %s/%s", resource.GetNamespace(), resource.GetName())
ruleInfo.Add("Overlay succesfully applied")
2019-08-09 11:08:02 -07:00
// update rule information
2019-07-25 16:20:22 -04:00
// strip slashes from string
2019-08-09 12:59:37 -07:00
patch := JoinPatches(oPatches)
ruleInfo.Changes = string(patch)
patches = append(patches, oPatches...)
2019-07-24 06:33:51 -04:00
} else {
2019-08-09 11:08:02 -07:00
glog.V(4).Infof("failed to apply overlay: %v", err)
2019-08-09 12:59:37 -07:00
ruleInfo.Fail()
ruleInfo.Addf("failed to apply overlay: %v", err)
}
}
// Process Patches
if len(rule.Mutation.Patches) != 0 {
jsonPatches, errs := processPatches(resource, rule)
if len(errs) > 0 {
2019-08-09 12:59:37 -07:00
ruleInfo.Fail()
for _, err := range errs {
2019-08-09 11:08:02 -07:00
glog.V(4).Infof("failed to apply patches: %v", err)
2019-08-09 12:59:37 -07:00
ruleInfo.Addf("patches application has failed, err %v.", err)
}
2019-06-05 16:44:53 +03:00
} else {
2019-08-09 12:59:37 -07:00
glog.V(4).Infof("patches applied succesfully on resource %s/%s", resource.GetNamespace(), resource.GetName())
ruleInfo.Addf("Patches succesfully applied.")
patches = append(patches, jsonPatches...)
}
}
2019-08-09 12:59:37 -07:00
ruleInfos = append(ruleInfos, ruleInfo)
2019-05-09 22:26:22 -07:00
}
2019-08-09 12:59:37 -07:00
return patches, ruleInfos
2019-05-09 22:26:22 -07:00
}