2019-05-14 01:17:28 +00:00
|
|
|
package engine
|
2019-05-10 05:26:22 +00:00
|
|
|
|
|
|
|
import (
|
2019-12-27 23:57:43 +00:00
|
|
|
"reflect"
|
2019-12-26 23:34:19 +00:00
|
|
|
"strings"
|
2019-08-19 23:40:10 +00:00
|
|
|
"time"
|
2019-08-09 23:55:43 +00:00
|
|
|
|
2020-03-17 18:05:20 +00:00
|
|
|
"github.com/go-logr/logr"
|
2019-12-26 23:34:19 +00:00
|
|
|
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
|
2020-01-08 01:06:17 +00:00
|
|
|
"github.com/nirmata/kyverno/pkg/engine/mutate"
|
2019-12-12 23:02:59 +00:00
|
|
|
"github.com/nirmata/kyverno/pkg/engine/response"
|
2020-01-07 23:13:57 +00:00
|
|
|
"github.com/nirmata/kyverno/pkg/engine/variables"
|
2020-01-16 19:57:28 +00:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2020-03-17 23:25:34 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
2019-05-10 05:26:22 +00:00
|
|
|
)
|
|
|
|
|
2019-12-26 23:34:19 +00:00
|
|
|
const (
|
2020-01-24 20:05:53 +00:00
|
|
|
//PodControllers stores the list of Pod-controllers in csv string
|
|
|
|
PodControllers = "DaemonSet,Deployment,Job,StatefulSet"
|
|
|
|
//PodControllersAnnotation defines the annotation key for Pod-Controllers
|
2019-12-26 23:34:19 +00:00
|
|
|
PodControllersAnnotation = "pod-policies.kyverno.io/autogen-controllers"
|
2020-01-24 20:05:53 +00:00
|
|
|
//PodTemplateAnnotation defines the annotation key for Pod-Template
|
|
|
|
PodTemplateAnnotation = "pod-policies.kyverno.io/autogen-applied"
|
2019-12-26 23:34:19 +00:00
|
|
|
)
|
|
|
|
|
2019-05-13 18:27:47 +00:00
|
|
|
// Mutate performs mutation. Overlay first and then mutation patches
|
2019-12-12 23:02:59 +00:00
|
|
|
func Mutate(policyContext PolicyContext) (resp response.EngineResponse) {
|
2019-08-24 01:34:23 +00:00
|
|
|
startTime := time.Now()
|
2019-11-09 02:57:27 +00:00
|
|
|
policy := policyContext.Policy
|
2019-11-13 21:13:07 +00:00
|
|
|
resource := policyContext.NewResource
|
2019-12-13 02:25:54 +00:00
|
|
|
ctx := policyContext.Context
|
2020-03-17 18:05:20 +00:00
|
|
|
logger := log.Log.WithName("Mutate").WithValues("policy", policy.Name, "kind", resource.GetKind(), "namespace", resource.GetNamespace(), "name", resource.GetName())
|
|
|
|
logger.V(4).Info("start processing", "startTime", startTime)
|
2020-01-16 19:57:28 +00:00
|
|
|
startMutateResultResponse(&resp, policy, resource)
|
2020-03-17 18:05:20 +00:00
|
|
|
defer endMutateResultResponse(logger, &resp, startTime)
|
2020-01-16 19:57:28 +00:00
|
|
|
|
2019-11-14 01:56:56 +00:00
|
|
|
patchedResource := policyContext.NewResource
|
2019-08-24 01:34:23 +00:00
|
|
|
for _, rule := range policy.Spec.Rules {
|
2020-02-14 19:59:28 +00:00
|
|
|
var ruleResponse response.RuleResponse
|
2020-03-17 18:05:20 +00:00
|
|
|
logger := logger.WithValues("rule", rule.Name)
|
2019-08-24 01:34:23 +00:00
|
|
|
//TODO: to be checked before calling the resources as well
|
2019-12-26 23:34:19 +00:00
|
|
|
if !rule.HasMutate() && !strings.Contains(PodControllers, resource.GetKind()) {
|
2019-08-24 01:34:23 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-11-09 02:57:27 +00:00
|
|
|
|
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
|
2020-02-07 09:15:43 +00:00
|
|
|
if err := MatchesResourceDescription(resource, rule, policyContext.AdmissionInfo); err != nil {
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.V(4).Info("resource fails the match description")
|
2019-08-24 01:34:23 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-01-07 23:13:57 +00:00
|
|
|
|
2020-02-04 20:13:41 +00:00
|
|
|
// operate on the copy of the conditions, as we perform variable substitution
|
|
|
|
copyConditions := copyConditions(rule.Conditions)
|
2020-01-07 23:13:57 +00:00
|
|
|
// evaluate pre-conditions
|
2020-02-14 19:59:28 +00:00
|
|
|
// - handle variable subsitutions
|
2020-03-17 18:05:20 +00:00
|
|
|
if !variables.EvaluateConditions(logger, ctx, copyConditions) {
|
|
|
|
logger.V(4).Info("resource fails the preconditions")
|
2020-01-07 23:13:57 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-14 19:59:28 +00:00
|
|
|
mutation := rule.Mutation.DeepCopy()
|
2019-08-24 01:34:23 +00:00
|
|
|
// Process Overlay
|
2020-02-14 19:59:28 +00:00
|
|
|
if mutation.Overlay != nil {
|
|
|
|
overlay := mutation.Overlay
|
|
|
|
// subsiitue the variables
|
|
|
|
var err error
|
2020-03-17 18:05:20 +00:00
|
|
|
if overlay, err = variables.SubstituteVars(logger, ctx, overlay); err != nil {
|
2020-02-14 19:59:28 +00:00
|
|
|
// variable subsitution failed
|
|
|
|
ruleResponse.Success = false
|
|
|
|
ruleResponse.Message = err.Error()
|
|
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, ruleResponse)
|
|
|
|
continue
|
|
|
|
}
|
2020-01-09 20:24:37 +00:00
|
|
|
|
2020-03-17 23:25:34 +00:00
|
|
|
ruleResponse, patchedResource = mutate.ProcessOverlay(logger, rule.Name, overlay, patchedResource)
|
2020-02-14 19:59:28 +00:00
|
|
|
if ruleResponse.Success {
|
2020-01-09 20:24:37 +00:00
|
|
|
// - overlay pattern does not match the resource conditions
|
|
|
|
if ruleResponse.Patches == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.V(4).Info("overlay applied succesfully")
|
2019-08-24 01:34:23 +00:00
|
|
|
}
|
2019-11-14 01:56:56 +00:00
|
|
|
|
2019-12-12 23:02:59 +00:00
|
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, ruleResponse)
|
2020-02-14 19:59:28 +00:00
|
|
|
incrementAppliedRuleCount(&resp)
|
2019-08-24 01:34:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process Patches
|
|
|
|
if rule.Mutation.Patches != nil {
|
2019-12-12 23:02:59 +00:00
|
|
|
var ruleResponse response.RuleResponse
|
2020-03-17 23:25:34 +00:00
|
|
|
ruleResponse, patchedResource = mutate.ProcessPatches(logger, rule, patchedResource)
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.V(4).Info("patches applied successfully")
|
2019-12-12 23:02:59 +00:00
|
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, ruleResponse)
|
2020-02-14 19:59:28 +00:00
|
|
|
incrementAppliedRuleCount(&resp)
|
2019-08-24 01:34:23 +00:00
|
|
|
}
|
2019-12-26 23:34:19 +00:00
|
|
|
|
|
|
|
// insert annotation to podtemplate if resource is pod controller
|
2019-12-27 23:57:43 +00:00
|
|
|
// skip inserting on existing resource
|
2020-01-08 18:44:41 +00:00
|
|
|
if reflect.DeepEqual(policyContext.AdmissionInfo, kyverno.RequestInfo{}) {
|
2019-12-27 23:57:43 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-26 23:34:19 +00:00
|
|
|
if strings.Contains(PodControllers, resource.GetKind()) {
|
|
|
|
var ruleResponse response.RuleResponse
|
2020-03-17 23:25:34 +00:00
|
|
|
ruleResponse, patchedResource = mutate.ProcessOverlay(logger, rule.Name, podTemplateRule, patchedResource)
|
2019-12-26 23:34:19 +00:00
|
|
|
if !ruleResponse.Success {
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.Info("failed to insert annotation for podTemplate", "error", ruleResponse.Message)
|
2019-12-26 23:34:19 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-27 23:57:43 +00:00
|
|
|
if ruleResponse.Success && ruleResponse.Patches != nil {
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.V(2).Info("inserted annotation for podTemplate")
|
2019-12-27 02:41:14 +00:00
|
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, ruleResponse)
|
|
|
|
}
|
2019-12-26 23:34:19 +00:00
|
|
|
}
|
2019-08-24 01:34:23 +00:00
|
|
|
}
|
|
|
|
// send the patched resource
|
2019-12-12 23:02:59 +00:00
|
|
|
resp.PatchedResource = patchedResource
|
|
|
|
return resp
|
2019-08-24 01:34:23 +00:00
|
|
|
}
|
2020-02-14 19:59:28 +00:00
|
|
|
func incrementAppliedRuleCount(resp *response.EngineResponse) {
|
|
|
|
resp.PolicyResponse.RulesAppliedCount++
|
|
|
|
}
|
2019-12-26 23:34:19 +00:00
|
|
|
|
2020-01-16 19:57:28 +00:00
|
|
|
func startMutateResultResponse(resp *response.EngineResponse, policy kyverno.ClusterPolicy, resource unstructured.Unstructured) {
|
|
|
|
// set policy information
|
|
|
|
resp.PolicyResponse.Policy = policy.Name
|
|
|
|
// resource details
|
|
|
|
resp.PolicyResponse.Resource.Name = resource.GetName()
|
|
|
|
resp.PolicyResponse.Resource.Namespace = resource.GetNamespace()
|
|
|
|
resp.PolicyResponse.Resource.Kind = resource.GetKind()
|
|
|
|
resp.PolicyResponse.Resource.APIVersion = resource.GetAPIVersion()
|
2020-01-16 22:29:44 +00:00
|
|
|
// TODO(shuting): set response with mutationFailureAction
|
2020-01-16 19:57:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-17 18:05:20 +00:00
|
|
|
func endMutateResultResponse(logger logr.Logger, resp *response.EngineResponse, startTime time.Time) {
|
2020-01-16 19:57:28 +00:00
|
|
|
resp.PolicyResponse.ProcessingTime = time.Since(startTime)
|
2020-03-17 18:05:20 +00:00
|
|
|
logger.V(4).Info("finshed processing", "processingTime", resp.PolicyResponse.ProcessingTime, "mutationRulesApplied", resp.PolicyResponse.RulesAppliedCount)
|
2020-01-16 19:57:28 +00:00
|
|
|
}
|
|
|
|
|
2019-12-26 23:34:19 +00:00
|
|
|
// podTemplateRule mutate pod template with annotation
|
|
|
|
// pod-policies.kyverno.io/autogen-applied=true
|
|
|
|
var podTemplateRule = kyverno.Rule{
|
2019-12-27 02:41:14 +00:00
|
|
|
Name: "autogen-annotate-podtemplate",
|
2019-12-26 23:34:19 +00:00
|
|
|
Mutation: kyverno.Mutation{
|
2019-12-27 02:41:14 +00:00
|
|
|
Overlay: map[string]interface{}{
|
|
|
|
"spec": map[string]interface{}{
|
|
|
|
"template": map[string]interface{}{
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
"annotations": map[string]interface{}{
|
2020-01-16 02:15:48 +00:00
|
|
|
"+(pod-policies.kyverno.io/autogen-applied)": "true",
|
2019-12-27 02:41:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-12-26 23:34:19 +00:00
|
|
|
},
|
|
|
|
}
|