2019-05-13 18:17:28 -07:00
|
|
|
package engine
|
2019-05-09 22:26:22 -07:00
|
|
|
|
|
|
|
import (
|
2022-12-09 14:45:11 +01:00
|
|
|
"context"
|
2024-07-26 13:49:51 +03:00
|
|
|
"fmt"
|
2021-10-13 11:03:25 -07:00
|
|
|
"time"
|
|
|
|
|
2020-03-17 11:05:20 -07:00
|
|
|
"github.com/go-logr/logr"
|
2022-05-17 13:12:43 +02:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2024-10-16 15:24:37 +02:00
|
|
|
"github.com/kyverno/kyverno/pkg/autogen"
|
2023-01-30 12:41:09 +01:00
|
|
|
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
|
2023-03-31 08:41:48 +02:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/handlers"
|
2023-04-03 21:58:58 +02:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/handlers/mutation"
|
2023-02-07 05:30:15 +01:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/internal"
|
2020-01-16 11:57:28 -08:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2019-05-09 22:26:22 -07:00
|
|
|
)
|
|
|
|
|
2023-03-30 13:59:32 +02:00
|
|
|
// mutate performs mutation. Overlay first and then mutation patches
|
2023-02-06 13:49:04 +01:00
|
|
|
func (e *engine) mutate(
|
2023-01-31 15:30:40 +01:00
|
|
|
ctx context.Context,
|
2023-02-09 16:15:51 +01:00
|
|
|
logger logr.Logger,
|
2023-01-31 16:28:48 +01:00
|
|
|
policyContext engineapi.PolicyContext,
|
2023-03-30 13:59:32 +02:00
|
|
|
) (engineapi.PolicyResponse, unstructured.Unstructured) {
|
|
|
|
resp := engineapi.NewPolicyResponse()
|
2023-03-27 10:09:46 +02:00
|
|
|
policy := policyContext.Policy()
|
2023-01-31 16:28:48 +01:00
|
|
|
matchedResource := policyContext.NewResource()
|
2023-04-03 06:57:48 +02:00
|
|
|
applyRules := policy.GetSpec().GetApplyRules()
|
2020-01-16 11:57:28 -08:00
|
|
|
|
2023-01-31 16:28:48 +01:00
|
|
|
policyContext.JSONContext().Checkpoint()
|
|
|
|
defer policyContext.JSONContext().Restore()
|
2021-02-01 23:22:19 -08:00
|
|
|
|
2024-10-16 15:24:37 +02:00
|
|
|
for _, rule := range autogen.Default.ComputeRules(policy, "") {
|
2023-03-30 13:59:32 +02:00
|
|
|
startTime := time.Now()
|
2023-03-27 13:22:54 +02:00
|
|
|
logger := internal.LoggerWithRule(logger, rule)
|
2023-04-03 21:58:58 +02:00
|
|
|
handlerFactory := func() (handlers.Handler, error) {
|
|
|
|
if !rule.HasMutate() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2023-12-22 21:07:17 +08:00
|
|
|
if !policyContext.AdmissionOperation() && rule.HasMutateExisting() {
|
2024-07-26 13:49:51 +03: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 21:58:58 +02:00
|
|
|
return mutation.NewMutateExistingHandler(e.client)
|
|
|
|
}
|
|
|
|
return mutation.NewMutateResourceHandler()
|
2023-03-27 13:22:54 +02:00
|
|
|
}
|
2023-03-31 08:41:48 +02:00
|
|
|
resource, ruleResp := e.invokeRuleHandler(
|
|
|
|
ctx,
|
|
|
|
logger,
|
2023-04-03 21:58:58 +02:00
|
|
|
handlerFactory,
|
2023-03-31 08:41:48 +02:00
|
|
|
policyContext,
|
|
|
|
matchedResource,
|
|
|
|
rule,
|
|
|
|
engineapi.Mutation,
|
|
|
|
)
|
2023-03-27 10:09:46 +02:00
|
|
|
matchedResource = resource
|
2023-04-06 00:55:42 +02:00
|
|
|
resp.Add(engineapi.NewExecutionStats(startTime, time.Now()), ruleResp...)
|
2023-04-12 18:20:42 +02:00
|
|
|
if applyRules == kyvernov1.ApplyOne && resp.RulesAppliedCount() > 0 {
|
2022-07-29 00:02:26 -07:00
|
|
|
break
|
|
|
|
}
|
2021-10-06 18:31:20 -07:00
|
|
|
}
|
2023-03-30 13:59:32 +02:00
|
|
|
return resp, matchedResource
|
2019-12-26 15:34:19 -08:00
|
|
|
}
|