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"
|
2024-07-26 10:49:51 +00:00
|
|
|
"fmt"
|
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"
|
2024-10-16 13:24:37 +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-04-03 19:58:58 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/handlers/mutation"
|
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
|
|
|
|
2024-10-16 13:24:37 +00:00
|
|
|
for _, rule := range autogen.Default.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)
|
2023-04-03 19:58:58 +00:00
|
|
|
handlerFactory := func() (handlers.Handler, error) {
|
|
|
|
if !rule.HasMutate() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2023-12-22 13:07:17 +00:00
|
|
|
if !policyContext.AdmissionOperation() && rule.HasMutateExisting() {
|
2024-07-26 10:49:51 +00:00
|
|
|
if e.client == nil {
|
|
|
|
return nil, fmt.Errorf("Handler factory requires a client but a nil client was passed, likely due to a bug or unsupported operation.")
|
|
|
|
}
|
2023-04-03 19:58:58 +00:00
|
|
|
return mutation.NewMutateExistingHandler(e.client)
|
|
|
|
}
|
|
|
|
return mutation.NewMutateResourceHandler()
|
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 19:58:58 +00:00
|
|
|
handlerFactory,
|
2023-03-31 06:41:48 +00:00
|
|
|
policyContext,
|
|
|
|
matchedResource,
|
|
|
|
rule,
|
|
|
|
engineapi.Mutation,
|
|
|
|
)
|
2023-03-27 08:09:46 +00:00
|
|
|
matchedResource = resource
|
2023-04-05 22:55:42 +00:00
|
|
|
resp.Add(engineapi.NewExecutionStats(startTime, time.Now()), ruleResp...)
|
2023-04-12 16:20:42 +00:00
|
|
|
if applyRules == kyvernov1.ApplyOne && resp.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
|
|
|
}
|