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-04-25 16:36:31 +00:00
|
|
|
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/utils/store"
|
2022-03-28 14:01:27 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/autogen"
|
2022-03-25 08:40:25 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/mutate"
|
2020-10-07 18:12:31 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/response"
|
2022-10-02 19:45:03 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/logging"
|
2022-12-07 15:08:37 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/registryclient"
|
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
|
2022-12-09 13:45:11 +00:00
|
|
|
func Mutate(ctx context.Context, rclient registryclient.Client, policyContext *PolicyContext) (resp *response.EngineResponse) {
|
2019-08-24 01:34:23 +00:00
|
|
|
startTime := time.Now()
|
2022-12-02 08:14:23 +00:00
|
|
|
policy := policyContext.policy
|
2022-03-31 12:25:54 +00:00
|
|
|
resp = &response.EngineResponse{
|
|
|
|
Policy: policy,
|
|
|
|
}
|
2022-12-02 08:14:23 +00:00
|
|
|
matchedResource := policyContext.newResource
|
2022-12-09 13:45:11 +00:00
|
|
|
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
|
|
|
|
2022-12-02 08:14:23 +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-03-30 13:04:30 +00:00
|
|
|
for _, rule := range autogen.ComputeRules(policy) {
|
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
|
|
|
|
if len(policyContext.excludeGroupRole) > 0 {
|
|
|
|
excludeResource = policyContext.excludeGroupRole
|
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)
|
|
|
|
if err = MatchesResourceDescription(subresourceGVKToAPIResource, matchedResource, rule, policyContext.admissionInfo, excludeResource, policyContext.namespaceLabels, policyContext.policy.GetNamespace(), policyContext.subresource); err != nil {
|
|
|
|
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-12 20:32:11 +00:00
|
|
|
logger.V(3).Info("processing mutate rule", "applyRules", applyRules)
|
|
|
|
resource, err := policyContext.jsonContext.Query("request.object")
|
|
|
|
policyContext.jsonContext.Reset()
|
|
|
|
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
|
|
|
|
2022-12-12 20:32:11 +00:00
|
|
|
if err := LoadContext(ctx, logger, rclient, rule.Context, policyContext, rule.Name); err != nil {
|
|
|
|
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
|
|
|
|
if !policyContext.admissionOperation && rule.IsMutateExisting() {
|
|
|
|
targets, err := loadTargets(ruleCopy.Mutation.Targets, policyContext, logger)
|
|
|
|
if err != nil {
|
|
|
|
rr := ruleResponse(rule, response.Mutation, err.Error(), response.RuleStatusError)
|
|
|
|
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
|
|
|
|
if policyContext.subresource != "" {
|
|
|
|
parentResourceGVR = policyContext.requestResource
|
|
|
|
}
|
|
|
|
patchedResources = append(patchedResources, resourceInfo{
|
|
|
|
unstructured: matchedResource,
|
|
|
|
subresource: policyContext.subresource,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if !policyContext.admissionOperation && rule.IsMutateExisting() {
|
|
|
|
policyContext := policyContext.Copy()
|
|
|
|
if err := policyContext.jsonContext.AddTargetResource(patchedResource.unstructured.Object); err != nil {
|
|
|
|
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,
|
|
|
|
rclient: rclient,
|
|
|
|
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)
|
|
|
|
if ruleResponse.Status == response.RuleStatusError {
|
|
|
|
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 {
|
|
|
|
r.Status = response.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
|
|
|
|
2022-12-12 15:20:20 +00:00
|
|
|
func mutateResource(rule *kyvernov1.Rule, ctx *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 {
|
2022-12-12 15:20:20 +00:00
|
|
|
return mutate.NewResponse(response.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
|
|
|
|
policyContext *PolicyContext
|
|
|
|
foreach []kyvernov1.ForEachMutation
|
|
|
|
resource resourceInfo
|
|
|
|
nesting int
|
|
|
|
rclient registryclient.Client
|
|
|
|
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 {
|
|
|
|
if err := LoadContext(ctx, f.log, f.rclient, f.rule.Context, f.policyContext, f.rule.Name); err != nil {
|
|
|
|
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 {
|
2022-12-12 15:20:20 +00:00
|
|
|
return mutate.NewResponse(response.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)
|
2022-01-05 01:36:33 +00:00
|
|
|
if mutateResp.Status == response.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
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
if mutateResp.Status != response.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 {
|
2022-12-12 15:20:20 +00:00
|
|
|
return mutate.NewResponse(response.RuleStatusSkip, f.resource.unstructured, allPatches, msg)
|
2021-10-07 01:31:20 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 15:20:20 +00:00
|
|
|
return mutate.NewResponse(response.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
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
for i, e := range elements {
|
2022-11-04 09:35:25 +00:00
|
|
|
if e == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-12-12 15:20:20 +00:00
|
|
|
|
|
|
|
f.policyContext.JSONContext().Reset()
|
|
|
|
policyContext := f.policyContext.Copy()
|
|
|
|
|
|
|
|
// TODO - this needs to be refactored. The engine should not have a dependency to the CLI code
|
2022-12-12 19:24:13 +00:00
|
|
|
store.SetForEachElement(i)
|
2022-12-12 15:20:20 +00:00
|
|
|
|
2022-05-02 16:39:37 +00:00
|
|
|
falseVar := false
|
2022-12-12 15:20:20 +00:00
|
|
|
if err := addElementToContext(policyContext, e, i, f.nesting, &falseVar); err != nil {
|
|
|
|
return mutate.NewErrorResponse(fmt.Sprintf("failed to add element to mutate.foreach[%d].context", i), err)
|
2022-01-05 01:36:33 +00:00
|
|
|
}
|
2021-10-07 01:31:20 +00:00
|
|
|
|
2022-12-12 15:20:20 +00:00
|
|
|
if err := LoadContext(ctx, f.log, f.rclient, foreach.Context, policyContext, f.rule.Name); err != nil {
|
|
|
|
return mutate.NewErrorResponse(fmt.Sprintf("failed to load to mutate.foreach[%d].context", i), 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 {
|
2022-12-12 15:20:20 +00:00
|
|
|
return mutate.NewErrorResponse(fmt.Sprintf("failed to evaluate mutate.foreach[%d].preconditions", i), err)
|
2022-01-05 01:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !preconditionsPassed {
|
2022-12-12 15:20:20 +00:00
|
|
|
f.log.Info("mutate.foreach.preconditions not met", "elementIndex", i)
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
mutateResp = m.mutateForEach(ctx)
|
|
|
|
} else {
|
|
|
|
mutateResp = mutate.ForEach(f.rule.Name, foreach, policyContext.JSONContext(), patchedResource.unstructured, f.log)
|
|
|
|
}
|
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
if mutateResp.Status == response.RuleStatusFail || mutateResp.Status == response.RuleStatusError {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-12-12 15:20:20 +00:00
|
|
|
return mutate.NewResponse(response.RuleStatusPass, patchedResource.unstructured, allPatches, "")
|
2022-01-05 01:36:33 +00:00
|
|
|
}
|
2021-10-14 07:20:52 +00:00
|
|
|
|
2022-12-12 15:20:20 +00:00
|
|
|
func buildRuleResponse(rule *kyvernov1.Rule, mutateResp *mutate.Response, info resourceInfo) *response.RuleResponse {
|
|
|
|
resp := ruleResponse(*rule, response.Mutation, mutateResp.Message, mutateResp.Status)
|
2021-10-07 01:31:20 +00:00
|
|
|
if resp.Status == response.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
|
|
|
|
2022-05-17 11:12:43 +00:00
|
|
|
func startMutateResultResponse(resp *response.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()
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:05:20 +00:00
|
|
|
func endMutateResultResponse(logger logr.Logger, resp *response.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)
|
2021-05-15 13:45:04 +00:00
|
|
|
resp.PolicyResponse.PolicyExecutionTimestamp = 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
|
|
|
}
|