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-04-13 18:44:43 +00:00
|
|
|
"fmt"
|
2022-01-05 01:36:33 +00:00
|
|
|
"reflect"
|
2021-10-13 18:03:25 +00:00
|
|
|
"time"
|
|
|
|
|
2020-03-17 18:05:20 +00:00
|
|
|
"github.com/go-logr/logr"
|
2021-07-14 21:50:28 +00:00
|
|
|
gojmespath "github.com/jmespath/go-jmespath"
|
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-02-06 05:49:47 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/config"
|
2023-01-30 11:41:09 +00:00
|
|
|
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
|
2022-03-25 08:40:25 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/mutate"
|
2022-10-02 19:45:03 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/logging"
|
2022-12-12 20:32:11 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/tracing"
|
2022-12-12 15:20:20 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/utils/api"
|
2022-12-12 20:32:11 +00:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2022-12-09 16:45:23 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2020-01-16 19:57:28 +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
|
2023-02-02 10:58:34 +00:00
|
|
|
func doMutate(
|
2023-01-31 14:30:40 +00:00
|
|
|
ctx context.Context,
|
2023-02-02 10:58:34 +00:00
|
|
|
contextLoader engineapi.ContextLoaderFactory,
|
2023-02-06 05:49:47 +00:00
|
|
|
selector engineapi.PolicyExceptionSelector,
|
2023-01-31 15:28:48 +00:00
|
|
|
policyContext engineapi.PolicyContext,
|
2023-02-06 05:49:47 +00:00
|
|
|
cfg config.Configuration,
|
2023-01-31 14:30:40 +00:00
|
|
|
) (resp *engineapi.EngineResponse) {
|
2019-08-24 01:34:23 +00:00
|
|
|
startTime := time.Now()
|
2023-01-31 15:28:48 +00:00
|
|
|
policy := policyContext.Policy()
|
2023-01-30 11:41:09 +00:00
|
|
|
resp = &engineapi.EngineResponse{
|
2022-03-31 12:25:54 +00:00
|
|
|
Policy: policy,
|
|
|
|
}
|
2023-01-31 15:28:48 +00:00
|
|
|
matchedResource := policyContext.NewResource()
|
|
|
|
enginectx := policyContext.JSONContext()
|
2022-01-25 09:00:18 +00:00
|
|
|
var skippedRules []string
|
2020-09-22 21:11:49 +00:00
|
|
|
|
2022-10-02 19:45:03 +00:00
|
|
|
logger := logging.WithName("EngineMutate").WithValues("policy", policy.GetName(), "kind", matchedResource.GetKind(),
|
2022-04-25 12:20:40 +00:00
|
|
|
"namespace", matchedResource.GetNamespace(), "name", matchedResource.GetName())
|
2020-07-09 18:48:34 +00:00
|
|
|
|
2022-04-19 15:35:12 +00:00
|
|
|
logger.V(4).Info("start mutate policy processing", "startTime", startTime)
|
2020-01-16 19:57:28 +00:00
|
|
|
|
2022-04-25 12:20:40 +00:00
|
|
|
startMutateResultResponse(resp, policy, matchedResource)
|
2020-12-23 23:10:07 +00:00
|
|
|
defer endMutateResultResponse(logger, resp, startTime)
|
2020-06-24 17:26:04 +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
|
|
|
|
2021-07-28 16:54:50 +00:00
|
|
|
var err error
|
2022-07-29 07:02:26 +00:00
|
|
|
applyRules := policy.GetSpec().GetApplyRules()
|
2021-07-28 16:54:50 +00:00
|
|
|
|
2022-12-21 04:35:26 +00:00
|
|
|
computeRules := autogen.ComputeRules(policy)
|
|
|
|
|
|
|
|
for i, rule := range computeRules {
|
2020-08-05 16:11:23 +00:00
|
|
|
if !rule.HasMutate() {
|
2019-08-24 01:34:23 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-12-12 20:32:11 +00:00
|
|
|
tracing.ChildSpan(
|
|
|
|
ctx,
|
|
|
|
"pkg/engine",
|
|
|
|
fmt.Sprintf("RULE %s", rule.Name),
|
|
|
|
func(ctx context.Context, span trace.Span) {
|
|
|
|
logger := logger.WithValues("rule", rule.Name)
|
|
|
|
var excludeResource []string
|
2023-02-06 05:49:47 +00:00
|
|
|
if len(cfg.GetExcludeGroupRole()) > 0 {
|
|
|
|
excludeResource = cfg.GetExcludeGroupRole()
|
2022-05-24 12:19:36 +00:00
|
|
|
}
|
2022-12-12 20:32:11 +00:00
|
|
|
|
|
|
|
kindsInPolicy := append(rule.MatchResources.GetKinds(), rule.ExcludeResources.GetKinds()...)
|
|
|
|
subresourceGVKToAPIResource := GetSubresourceGVKToAPIResourceMap(kindsInPolicy, policyContext)
|
2023-01-31 15:28:48 +00:00
|
|
|
if err = MatchesResourceDescription(subresourceGVKToAPIResource, matchedResource, rule, policyContext.AdmissionInfo(), excludeResource, policyContext.NamespaceLabels(), policyContext.Policy().GetNamespace(), policyContext.SubResource()); err != nil {
|
2022-12-12 20:32:11 +00:00
|
|
|
logger.V(4).Info("rule not matched", "reason", err.Error())
|
|
|
|
skippedRules = append(skippedRules, rule.Name)
|
|
|
|
return
|
2022-12-12 15:20:20 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 04:35:26 +00:00
|
|
|
// check if there is a corresponding policy exception
|
2023-02-06 05:49:47 +00:00
|
|
|
ruleResp := hasPolicyExceptions(logger, selector, policyContext, &computeRules[i], subresourceGVKToAPIResource, cfg)
|
2022-12-21 04:35:26 +00:00
|
|
|
if ruleResp != nil {
|
|
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, *ruleResp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-12 20:32:11 +00:00
|
|
|
logger.V(3).Info("processing mutate rule", "applyRules", applyRules)
|
2023-01-31 15:28:48 +00:00
|
|
|
resource, err := policyContext.JSONContext().Query("request.object")
|
|
|
|
policyContext.JSONContext().Reset()
|
2022-12-12 20:32:11 +00:00
|
|
|
if err == nil && resource != nil {
|
|
|
|
if err := enginectx.AddResource(resource.(map[string]interface{})); err != nil {
|
|
|
|
logger.Error(err, "unable to update resource object")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.Error(err, "failed to query resource object")
|
|
|
|
}
|
2022-04-25 12:20:40 +00:00
|
|
|
|
2023-01-31 14:30:40 +00:00
|
|
|
if err := LoadContext(ctx, contextLoader, rule.Context, policyContext, rule.Name); err != nil {
|
2022-12-12 20:32:11 +00:00
|
|
|
if _, ok := err.(gojmespath.NotFoundError); ok {
|
|
|
|
logger.V(3).Info("failed to load context", "reason", err.Error())
|
|
|
|
} else {
|
|
|
|
logger.Error(err, "failed to load context")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-04-25 12:20:40 +00:00
|
|
|
|
2022-12-12 20:32:11 +00:00
|
|
|
ruleCopy := rule.DeepCopy()
|
|
|
|
var patchedResources []resourceInfo
|
2023-01-31 15:28:48 +00:00
|
|
|
if !policyContext.AdmissionOperation() && rule.IsMutateExisting() {
|
2022-12-12 20:32:11 +00:00
|
|
|
targets, err := loadTargets(ruleCopy.Mutation.Targets, policyContext, logger)
|
|
|
|
if err != nil {
|
2023-01-30 11:41:09 +00:00
|
|
|
rr := ruleResponse(rule, engineapi.Mutation, err.Error(), engineapi.RuleStatusError)
|
2022-12-12 20:32:11 +00:00
|
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, *rr)
|
|
|
|
} else {
|
|
|
|
patchedResources = append(patchedResources, targets...)
|
|
|
|
}
|
2022-04-25 12:20:40 +00:00
|
|
|
} else {
|
2022-12-12 20:32:11 +00:00
|
|
|
var parentResourceGVR metav1.GroupVersionResource
|
2023-01-31 15:28:48 +00:00
|
|
|
if policyContext.SubResource() != "" {
|
|
|
|
parentResourceGVR = policyContext.RequestResource()
|
2022-12-12 20:32:11 +00:00
|
|
|
}
|
|
|
|
patchedResources = append(patchedResources, resourceInfo{
|
|
|
|
unstructured: matchedResource,
|
2023-01-31 15:28:48 +00:00
|
|
|
subresource: policyContext.SubResource(),
|
2022-12-12 20:32:11 +00:00
|
|
|
parentResourceGVR: parentResourceGVR,
|
|
|
|
})
|
2022-04-25 12:20:40 +00:00
|
|
|
}
|
2022-07-29 07:02:26 +00:00
|
|
|
|
2022-12-12 20:32:11 +00:00
|
|
|
for _, patchedResource := range patchedResources {
|
|
|
|
if reflect.DeepEqual(patchedResource, unstructured.Unstructured{}) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-01-31 15:28:48 +00:00
|
|
|
if !policyContext.AdmissionOperation() && rule.IsMutateExisting() {
|
2022-12-12 20:32:11 +00:00
|
|
|
policyContext := policyContext.Copy()
|
2023-01-31 15:28:48 +00:00
|
|
|
if err := policyContext.JSONContext().AddTargetResource(patchedResource.unstructured.Object); err != nil {
|
2022-12-12 20:32:11 +00:00
|
|
|
logging.Error(err, "failed to add target resource to the context")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.V(4).Info("apply rule to resource", "rule", rule.Name, "resource namespace", patchedResource.unstructured.GetNamespace(), "resource name", patchedResource.unstructured.GetName())
|
|
|
|
var mutateResp *mutate.Response
|
|
|
|
if rule.Mutation.ForEachMutation != nil {
|
|
|
|
m := &forEachMutator{
|
|
|
|
rule: ruleCopy,
|
|
|
|
foreach: rule.Mutation.ForEachMutation,
|
|
|
|
policyContext: policyContext,
|
|
|
|
resource: patchedResource,
|
|
|
|
log: logger,
|
2023-01-31 14:30:40 +00:00
|
|
|
contextLoader: contextLoader,
|
2022-12-12 20:32:11 +00:00
|
|
|
nesting: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
mutateResp = m.mutateForEach(ctx)
|
|
|
|
} else {
|
|
|
|
mutateResp = mutateResource(ruleCopy, policyContext, patchedResource.unstructured, logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
matchedResource = mutateResp.PatchedResource
|
|
|
|
ruleResponse := buildRuleResponse(ruleCopy, mutateResp, patchedResource)
|
|
|
|
|
|
|
|
if ruleResponse != nil {
|
|
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, *ruleResponse)
|
2023-01-30 11:41:09 +00:00
|
|
|
if ruleResponse.Status == engineapi.RuleStatusError {
|
2022-12-12 20:32:11 +00:00
|
|
|
incrementErrorCount(resp)
|
|
|
|
} else {
|
|
|
|
incrementAppliedCount(resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2022-07-29 07:02:26 +00:00
|
|
|
if applyRules == kyvernov1.ApplyOne && resp.PolicyResponse.RulesAppliedCount > 0 {
|
|
|
|
break
|
|
|
|
}
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
2020-01-07 23:13:57 +00:00
|
|
|
|
2021-12-28 16:37:41 +00:00
|
|
|
for _, r := range resp.PolicyResponse.Rules {
|
2022-01-25 09:00:18 +00:00
|
|
|
for _, n := range skippedRules {
|
2021-12-28 16:37:41 +00:00
|
|
|
if r.Name == n {
|
2023-01-30 11:41:09 +00:00
|
|
|
r.Status = engineapi.RuleStatusSkip
|
2022-01-25 09:00:18 +00:00
|
|
|
logger.V(4).Info("rule Status set as skip", "rule skippedRules", r.Name)
|
2021-12-28 16:37:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 12:20:40 +00:00
|
|
|
resp.PatchedResource = matchedResource
|
2021-10-07 01:31:20 +00:00
|
|
|
return resp
|
|
|
|
}
|
2021-04-13 18:44:43 +00:00
|
|
|
|
2023-01-31 15:28:48 +00:00
|
|
|
func mutateResource(rule *kyvernov1.Rule, ctx engineapi.PolicyContext, resource unstructured.Unstructured, logger logr.Logger) *mutate.Response {
|
2022-03-06 19:07:51 +00:00
|
|
|
preconditionsPassed, err := checkPreconditions(logger, ctx, rule.GetAnyAllConditions())
|
2022-01-05 01:36:33 +00:00
|
|
|
if err != nil {
|
2022-12-12 15:20:20 +00:00
|
|
|
return mutate.NewErrorResponse("failed to evaluate preconditions", err)
|
2022-01-05 01:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !preconditionsPassed {
|
2023-01-30 11:41:09 +00:00
|
|
|
return mutate.NewResponse(engineapi.RuleStatusSkip, resource, nil, "preconditions not met")
|
2022-01-05 01:36:33 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 15:20:20 +00:00
|
|
|
return mutate.Mutate(rule, ctx.JSONContext(), resource, logger)
|
2022-01-05 01:36:33 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 15:20:20 +00:00
|
|
|
type forEachMutator struct {
|
|
|
|
rule *kyvernov1.Rule
|
2023-01-31 15:28:48 +00:00
|
|
|
policyContext engineapi.PolicyContext
|
2022-12-12 15:20:20 +00:00
|
|
|
foreach []kyvernov1.ForEachMutation
|
|
|
|
resource resourceInfo
|
|
|
|
nesting int
|
2023-02-02 10:58:34 +00:00
|
|
|
contextLoader engineapi.ContextLoaderFactory
|
2022-12-12 15:20:20 +00:00
|
|
|
log logr.Logger
|
|
|
|
}
|
2021-04-16 00:33:34 +00:00
|
|
|
|
2022-12-12 15:20:20 +00:00
|
|
|
func (f *forEachMutator) mutateForEach(ctx context.Context) *mutate.Response {
|
2022-01-05 01:36:33 +00:00
|
|
|
var applyCount int
|
2021-10-07 01:31:20 +00:00
|
|
|
allPatches := make([][]byte, 0)
|
|
|
|
|
2022-12-12 15:20:20 +00:00
|
|
|
for _, foreach := range f.foreach {
|
2023-01-31 14:30:40 +00:00
|
|
|
if err := LoadContext(ctx, f.contextLoader, f.rule.Context, f.policyContext, f.rule.Name); err != nil {
|
2022-12-12 15:20:20 +00:00
|
|
|
f.log.Error(err, "failed to load context")
|
|
|
|
return mutate.NewErrorResponse("failed to load context", err)
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 15:20:20 +00:00
|
|
|
preconditionsPassed, err := checkPreconditions(f.log, f.policyContext, f.rule.GetAnyAllConditions())
|
2021-10-14 07:20:52 +00:00
|
|
|
if err != nil {
|
2022-12-12 15:20:20 +00:00
|
|
|
return mutate.NewErrorResponse("failed to evaluate preconditions", err)
|
2022-01-05 01:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !preconditionsPassed {
|
2023-01-30 11:41:09 +00:00
|
|
|
return mutate.NewResponse(engineapi.RuleStatusSkip, f.resource.unstructured, nil, "preconditions not met")
|
2019-08-24 01:34:23 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 15:20:20 +00:00
|
|
|
elements, err := evaluateList(foreach.List, f.policyContext.JSONContext())
|
2021-10-14 07:20:52 +00:00
|
|
|
if err != nil {
|
2022-12-12 15:20:20 +00:00
|
|
|
msg := fmt.Sprintf("failed to evaluate list %s: %v", foreach.List, err)
|
|
|
|
return mutate.NewErrorResponse(msg, err)
|
2021-07-22 21:37:09 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 15:20:20 +00:00
|
|
|
mutateResp := f.mutateElements(ctx, foreach, elements)
|
2023-01-30 11:41:09 +00:00
|
|
|
if mutateResp.Status == engineapi.RuleStatusError {
|
2022-12-12 15:20:20 +00:00
|
|
|
return mutate.NewErrorResponse("failed to mutate elements", err)
|
2022-01-05 01:36:33 +00:00
|
|
|
}
|
2021-10-14 07:20:52 +00:00
|
|
|
|
2023-01-30 11:41:09 +00:00
|
|
|
if mutateResp.Status != engineapi.RuleStatusSkip {
|
2021-10-14 07:20:52 +00:00
|
|
|
applyCount++
|
2022-01-05 01:36:33 +00:00
|
|
|
if len(mutateResp.Patches) > 0 {
|
2022-12-12 15:20:20 +00:00
|
|
|
f.resource.unstructured = mutateResp.PatchedResource
|
2022-01-05 01:36:33 +00:00
|
|
|
allPatches = append(allPatches, mutateResp.Patches...)
|
|
|
|
}
|
2021-10-14 07:20:52 +00:00
|
|
|
}
|
2020-06-26 00:24:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 15:20:20 +00:00
|
|
|
msg := fmt.Sprintf("%d elements processed", applyCount)
|
2021-10-07 01:31:20 +00:00
|
|
|
if applyCount == 0 {
|
2023-01-30 11:41:09 +00:00
|
|
|
return mutate.NewResponse(engineapi.RuleStatusSkip, f.resource.unstructured, allPatches, msg)
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 11:41:09 +00:00
|
|
|
return mutate.NewResponse(engineapi.RuleStatusPass, f.resource.unstructured, allPatches, msg)
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 15:20:20 +00:00
|
|
|
func (f *forEachMutator) mutateElements(ctx context.Context, foreach kyvernov1.ForEachMutation, elements []interface{}) *mutate.Response {
|
|
|
|
f.policyContext.JSONContext().Checkpoint()
|
|
|
|
defer f.policyContext.JSONContext().Restore()
|
2020-04-04 17:09:21 +00:00
|
|
|
|
2022-12-12 15:20:20 +00:00
|
|
|
patchedResource := f.resource
|
2022-01-05 01:36:33 +00:00
|
|
|
var allPatches [][]byte
|
2022-03-25 15:15:31 +00:00
|
|
|
if foreach.RawPatchStrategicMerge != nil {
|
|
|
|
invertedElement(elements)
|
|
|
|
}
|
2021-10-14 07:20:52 +00:00
|
|
|
|
2023-01-30 15:30:47 +00:00
|
|
|
for index, element := range elements {
|
|
|
|
if element == nil {
|
2022-11-04 09:35:25 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-12-12 15:20:20 +00:00
|
|
|
|
|
|
|
f.policyContext.JSONContext().Reset()
|
|
|
|
policyContext := f.policyContext.Copy()
|
|
|
|
|
2022-05-02 16:39:37 +00:00
|
|
|
falseVar := false
|
2023-01-30 15:30:47 +00:00
|
|
|
if err := addElementToContext(policyContext, element, index, f.nesting, &falseVar); err != nil {
|
|
|
|
return mutate.NewErrorResponse(fmt.Sprintf("failed to add element to mutate.foreach[%d].context", index), err)
|
2022-01-05 01:36:33 +00:00
|
|
|
}
|
2021-10-07 01:31:20 +00:00
|
|
|
|
2023-01-31 14:30:40 +00:00
|
|
|
if err := LoadContext(ctx, f.contextLoader, foreach.Context, policyContext, f.rule.Name); err != nil {
|
2023-01-30 15:30:47 +00:00
|
|
|
return mutate.NewErrorResponse(fmt.Sprintf("failed to load to mutate.foreach[%d].context", index), err)
|
2022-01-05 01:36:33 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 15:20:20 +00:00
|
|
|
preconditionsPassed, err := checkPreconditions(f.log, policyContext, foreach.AnyAllConditions)
|
2022-01-05 01:36:33 +00:00
|
|
|
if err != nil {
|
2023-01-30 15:30:47 +00:00
|
|
|
return mutate.NewErrorResponse(fmt.Sprintf("failed to evaluate mutate.foreach[%d].preconditions", index), err)
|
2022-01-05 01:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !preconditionsPassed {
|
2023-01-30 15:30:47 +00:00
|
|
|
f.log.Info("mutate.foreach.preconditions not met", "elementIndex", index)
|
2022-01-05 01:36:33 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-12-12 15:20:20 +00:00
|
|
|
var mutateResp *mutate.Response
|
|
|
|
if foreach.ForEachMutation != nil {
|
2022-12-12 19:24:13 +00:00
|
|
|
nestedForEach, err := api.DeserializeJSONArray[kyvernov1.ForEachMutation](foreach.ForEachMutation)
|
2022-12-12 15:20:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return mutate.NewErrorResponse("failed to deserialize foreach", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &forEachMutator{
|
|
|
|
rule: f.rule,
|
|
|
|
policyContext: f.policyContext,
|
|
|
|
resource: patchedResource,
|
|
|
|
log: f.log,
|
2022-12-12 19:24:13 +00:00
|
|
|
foreach: nestedForEach,
|
2022-12-12 15:20:20 +00:00
|
|
|
nesting: f.nesting + 1,
|
2023-01-31 14:30:40 +00:00
|
|
|
contextLoader: f.contextLoader,
|
2022-12-12 15:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mutateResp = m.mutateForEach(ctx)
|
|
|
|
} else {
|
|
|
|
mutateResp = mutate.ForEach(f.rule.Name, foreach, policyContext.JSONContext(), patchedResource.unstructured, f.log)
|
|
|
|
}
|
|
|
|
|
2023-01-30 11:41:09 +00:00
|
|
|
if mutateResp.Status == engineapi.RuleStatusFail || mutateResp.Status == engineapi.RuleStatusError {
|
2022-01-05 01:36:33 +00:00
|
|
|
return mutateResp
|
|
|
|
}
|
2021-10-07 01:31:20 +00:00
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
if len(mutateResp.Patches) > 0 {
|
2022-12-12 15:20:20 +00:00
|
|
|
patchedResource.unstructured = mutateResp.PatchedResource
|
2022-01-05 01:36:33 +00:00
|
|
|
allPatches = append(allPatches, mutateResp.Patches...)
|
|
|
|
}
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 11:41:09 +00:00
|
|
|
return mutate.NewResponse(engineapi.RuleStatusPass, patchedResource.unstructured, allPatches, "")
|
2022-01-05 01:36:33 +00:00
|
|
|
}
|
2021-10-14 07:20:52 +00:00
|
|
|
|
2023-01-30 11:41:09 +00:00
|
|
|
func buildRuleResponse(rule *kyvernov1.Rule, mutateResp *mutate.Response, info resourceInfo) *engineapi.RuleResponse {
|
|
|
|
resp := ruleResponse(*rule, engineapi.Mutation, mutateResp.Message, mutateResp.Status)
|
|
|
|
if resp.Status == engineapi.RuleStatusPass {
|
2022-01-05 01:36:33 +00:00
|
|
|
resp.Patches = mutateResp.Patches
|
|
|
|
resp.Message = buildSuccessMessage(mutateResp.PatchedResource)
|
|
|
|
}
|
|
|
|
|
2022-12-12 15:20:20 +00:00
|
|
|
if len(rule.Mutation.Targets) != 0 {
|
|
|
|
resp.PatchedTarget = &mutateResp.PatchedResource
|
|
|
|
resp.PatchedTargetSubresourceName = info.subresource
|
|
|
|
resp.PatchedTargetParentResourceGVR = info.parentResourceGVR
|
|
|
|
}
|
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
return resp
|
|
|
|
}
|
2021-10-07 01:31:20 +00:00
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
func buildSuccessMessage(r unstructured.Unstructured) string {
|
|
|
|
if reflect.DeepEqual(unstructured.Unstructured{}, r) {
|
|
|
|
return "mutated resource"
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 18:55:13 +00:00
|
|
|
if r.GetNamespace() == "" {
|
2022-01-05 01:36:33 +00:00
|
|
|
return fmt.Sprintf("mutated %s/%s", r.GetKind(), r.GetName())
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
return fmt.Sprintf("mutated %s/%s in namespace %s", r.GetKind(), r.GetName(), r.GetNamespace())
|
2020-02-14 19:59:28 +00:00
|
|
|
}
|
2019-12-26 23:34:19 +00:00
|
|
|
|
2023-01-30 11:41:09 +00:00
|
|
|
func startMutateResultResponse(resp *engineapi.EngineResponse, policy kyvernov1.PolicyInterface, resource unstructured.Unstructured) {
|
2020-12-23 23:10:07 +00:00
|
|
|
if resp == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-29 21:43:11 +00:00
|
|
|
resp.PolicyResponse.Policy.Name = policy.GetName()
|
|
|
|
resp.PolicyResponse.Policy.Namespace = policy.GetNamespace()
|
2020-01-16 19:57:28 +00:00
|
|
|
resp.PolicyResponse.Resource.Name = resource.GetName()
|
|
|
|
resp.PolicyResponse.Resource.Namespace = resource.GetNamespace()
|
|
|
|
resp.PolicyResponse.Resource.Kind = resource.GetKind()
|
|
|
|
resp.PolicyResponse.Resource.APIVersion = resource.GetAPIVersion()
|
|
|
|
}
|
|
|
|
|
2023-01-30 11:41:09 +00:00
|
|
|
func endMutateResultResponse(logger logr.Logger, resp *engineapi.EngineResponse, startTime time.Time) {
|
2020-12-23 23:10:07 +00:00
|
|
|
if resp == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-16 19:57:28 +00:00
|
|
|
resp.PolicyResponse.ProcessingTime = time.Since(startTime)
|
2023-01-30 14:49:44 +00:00
|
|
|
resp.PolicyResponse.Timestamp = startTime.Unix()
|
2021-01-07 19:24:38 +00:00
|
|
|
logger.V(5).Info("finished processing policy", "processingTime", resp.PolicyResponse.ProcessingTime.String(), "mutationRulesApplied", resp.PolicyResponse.RulesAppliedCount)
|
2019-12-26 23:34:19 +00:00
|
|
|
}
|