2019-05-14 01:17:28 +00:00
|
|
|
package engine
|
2019-05-10 05:26:22 +00:00
|
|
|
|
|
|
|
import (
|
2022-12-09 13:45:11 +00:00
|
|
|
"context"
|
2021-10-13 18:03:25 +00:00
|
|
|
"time"
|
|
|
|
|
2020-03-17 18:05:20 +00:00
|
|
|
"github.com/go-logr/logr"
|
2022-05-17 11:12:43 +00:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2022-03-28 14:01:27 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/autogen"
|
2023-01-30 11:41:09 +00:00
|
|
|
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
|
2023-03-31 06:41:48 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/handlers"
|
2023-02-07 04:30:15 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/internal"
|
2020-01-16 19:57:28 +00:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2019-05-10 05:26:22 +00:00
|
|
|
)
|
|
|
|
|
2023-03-30 11:59:32 +00:00
|
|
|
// mutate performs mutation. Overlay first and then mutation patches
|
2023-02-06 12:49:04 +00:00
|
|
|
func (e *engine) mutate(
|
2023-01-31 14:30:40 +00:00
|
|
|
ctx context.Context,
|
2023-02-09 15:15:51 +00:00
|
|
|
logger logr.Logger,
|
2023-01-31 15:28:48 +00:00
|
|
|
policyContext engineapi.PolicyContext,
|
2023-03-30 11:59:32 +00:00
|
|
|
) (engineapi.PolicyResponse, unstructured.Unstructured) {
|
|
|
|
resp := engineapi.NewPolicyResponse()
|
2023-03-27 08:09:46 +00:00
|
|
|
policy := policyContext.Policy()
|
2023-01-31 15:28:48 +00:00
|
|
|
matchedResource := policyContext.NewResource()
|
2023-04-03 04:57:48 +00:00
|
|
|
applyRules := policy.GetSpec().GetApplyRules()
|
2020-01-16 19:57:28 +00:00
|
|
|
|
2023-01-31 15:28:48 +00:00
|
|
|
policyContext.JSONContext().Checkpoint()
|
|
|
|
defer policyContext.JSONContext().Restore()
|
2021-02-02 07:22:19 +00:00
|
|
|
|
2023-03-27 08:09:46 +00:00
|
|
|
for _, rule := range autogen.ComputeRules(policy) {
|
2023-03-30 11:59:32 +00:00
|
|
|
startTime := time.Now()
|
2023-03-27 11:22:54 +00:00
|
|
|
logger := internal.LoggerWithRule(logger, rule)
|
2020-08-05 16:11:23 +00:00
|
|
|
if !rule.HasMutate() {
|
2019-08-24 01:34:23 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-04-03 04:57:48 +00:00
|
|
|
var handler handlers.Handler
|
|
|
|
handler = e.mutateResourceHandler
|
2023-03-27 11:22:54 +00:00
|
|
|
if !policyContext.AdmissionOperation() && rule.IsMutateExisting() {
|
2023-04-03 04:57:48 +00:00
|
|
|
handler = e.mutateExistingHandler
|
2023-03-27 11:22:54 +00:00
|
|
|
}
|
2023-03-31 06:41:48 +00:00
|
|
|
resource, ruleResp := e.invokeRuleHandler(
|
|
|
|
ctx,
|
|
|
|
logger,
|
2023-04-03 04:57:48 +00:00
|
|
|
handler,
|
2023-03-31 06:41:48 +00:00
|
|
|
policyContext,
|
|
|
|
matchedResource,
|
|
|
|
rule,
|
|
|
|
engineapi.Mutation,
|
|
|
|
)
|
2023-03-27 08:09:46 +00:00
|
|
|
matchedResource = resource
|
|
|
|
for _, ruleResp := range ruleResp {
|
|
|
|
ruleResp := ruleResp
|
2023-03-30 11:59:32 +00:00
|
|
|
internal.AddRuleResponse(&resp, &ruleResp, startTime)
|
2023-03-27 08:09:46 +00:00
|
|
|
logger.V(4).Info("finished processing rule", "processingTime", ruleResp.Stats.ProcessingTime.String())
|
|
|
|
}
|
2023-03-30 11:59:32 +00:00
|
|
|
if applyRules == kyvernov1.ApplyOne && resp.Stats.RulesAppliedCount > 0 {
|
2022-07-29 07:02:26 +00:00
|
|
|
break
|
|
|
|
}
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
2023-03-30 11:59:32 +00:00
|
|
|
return resp, matchedResource
|
2019-12-26 23:34:19 +00:00
|
|
|
}
|