2019-05-14 01:17:28 +00:00
|
|
|
package engine
|
2019-05-10 05:26:22 +00:00
|
|
|
|
|
|
|
import (
|
2019-08-09 23:55:43 +00:00
|
|
|
"reflect"
|
2019-08-19 23:40:10 +00:00
|
|
|
"time"
|
2019-08-09 23:55:43 +00:00
|
|
|
|
2019-06-26 01:16:02 +00:00
|
|
|
"github.com/golang/glog"
|
2019-08-13 18:32:12 +00:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2019-05-10 05:26:22 +00:00
|
|
|
)
|
|
|
|
|
2019-05-13 18:27:47 +00:00
|
|
|
// Mutate performs mutation. Overlay first and then mutation patches
|
2019-11-09 02:57:27 +00:00
|
|
|
func Mutate(policyContext PolicyContext) (response EngineResponse) {
|
2019-08-24 01:34:23 +00:00
|
|
|
startTime := time.Now()
|
2019-11-09 02:57:27 +00:00
|
|
|
policy := policyContext.Policy
|
|
|
|
resource := policyContext.Resource
|
|
|
|
|
2019-08-24 01:34:23 +00:00
|
|
|
// policy information
|
|
|
|
func() {
|
|
|
|
// set policy information
|
|
|
|
response.PolicyResponse.Policy = policy.Name
|
|
|
|
// resource details
|
|
|
|
response.PolicyResponse.Resource.Name = resource.GetName()
|
|
|
|
response.PolicyResponse.Resource.Namespace = resource.GetNamespace()
|
|
|
|
response.PolicyResponse.Resource.Kind = resource.GetKind()
|
|
|
|
response.PolicyResponse.Resource.APIVersion = resource.GetAPIVersion()
|
|
|
|
}()
|
|
|
|
glog.V(4).Infof("started applying mutation rules of policy %q (%v)", policy.Name, startTime)
|
|
|
|
defer func() {
|
|
|
|
response.PolicyResponse.ProcessingTime = time.Since(startTime)
|
|
|
|
glog.V(4).Infof("finished applying mutation rules policy %v (%v)", policy.Name, response.PolicyResponse.ProcessingTime)
|
|
|
|
glog.V(4).Infof("Mutation Rules appplied succesfully count %v for policy %q", response.PolicyResponse.RulesAppliedCount, policy.Name)
|
|
|
|
}()
|
|
|
|
incrementAppliedRuleCount := func() {
|
|
|
|
// rules applied succesfully count
|
|
|
|
response.PolicyResponse.RulesAppliedCount++
|
|
|
|
}
|
|
|
|
|
|
|
|
var patchedResource unstructured.Unstructured
|
|
|
|
|
|
|
|
for _, rule := range policy.Spec.Rules {
|
|
|
|
//TODO: to be checked before calling the resources as well
|
2019-10-21 21:22:31 +00:00
|
|
|
if !rule.HasMutate() {
|
2019-08-24 01:34:23 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-11-09 02:57:27 +00:00
|
|
|
|
|
|
|
if !matchAdmissionInfo(rule, policyContext.AdmissionInfo) {
|
|
|
|
glog.Infof("rule '%s' cannot be applied on %s/%s/%s, admission permission: %v",
|
|
|
|
rule.Name, resource.GetKind(), resource.GetNamespace(), resource.GetName(), policyContext.AdmissionInfo)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-24 01:34:23 +00: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 {
|
|
|
|
glog.V(4).Infof("resource %s/%s does not satisfy the resource description for the rule ", resource.GetNamespace(), resource.GetName())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Process Overlay
|
|
|
|
if rule.Mutation.Overlay != nil {
|
|
|
|
var ruleResponse RuleResponse
|
2019-09-05 16:37:57 +00:00
|
|
|
ruleResponse, patchedResource = processOverlay(rule, resource)
|
2019-08-24 01:34:23 +00:00
|
|
|
if reflect.DeepEqual(ruleResponse, (RuleResponse{})) {
|
|
|
|
// overlay pattern does not match the resource conditions
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
response.PolicyResponse.Rules = append(response.PolicyResponse.Rules, ruleResponse)
|
|
|
|
incrementAppliedRuleCount()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process Patches
|
|
|
|
if rule.Mutation.Patches != nil {
|
|
|
|
var ruleResponse RuleResponse
|
2019-09-05 16:37:57 +00:00
|
|
|
ruleResponse, patchedResource = processPatches(rule, resource)
|
2019-08-24 01:34:23 +00:00
|
|
|
response.PolicyResponse.Rules = append(response.PolicyResponse.Rules, ruleResponse)
|
|
|
|
incrementAppliedRuleCount()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// send the patched resource
|
|
|
|
response.PatchedResource = patchedResource
|
|
|
|
return response
|
|
|
|
}
|