From 805f7b72a9bf4f0dd54f2194f129e4d4b1433b0c Mon Sep 17 00:00:00 2001 From: shuting Date: Thu, 30 Mar 2023 02:23:29 +0800 Subject: [PATCH] remove unused ctx (#6725) Signed-off-by: ShutingZhao --- pkg/engine/forceMutate.go | 14 +++++++------- pkg/engine/handlers/mutation/common.go | 2 +- pkg/engine/mutate/mutation.go | 11 ++++++----- pkg/engine/mutate/patch/patches.go | 5 +---- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/pkg/engine/forceMutate.go b/pkg/engine/forceMutate.go index c7f1dec125..afde293a53 100644 --- a/pkg/engine/forceMutate.go +++ b/pkg/engine/forceMutate.go @@ -46,13 +46,13 @@ func ForceMutate( } if r.Mutation.ForEachMutation != nil { - patchedResource, err = applyForEachMutate(r.Name, r.Mutation.ForEachMutation, patchedResource, ctx, logger) + patchedResource, err = applyForEachMutate(r.Name, r.Mutation.ForEachMutation, patchedResource, logger) if err != nil { return patchedResource, err } } else { m := r.Mutation - patchedResource, err = applyPatches(r.Name, m.GetPatchStrategicMerge(), m.PatchesJSON6902, patchedResource, ctx, logger) + patchedResource, err = applyPatches(r.Name, m.GetPatchStrategicMerge(), m.PatchesJSON6902, patchedResource, logger) if err != nil { return patchedResource, err } @@ -62,7 +62,7 @@ func ForceMutate( return patchedResource, nil } -func applyForEachMutate(name string, foreach []kyvernov1.ForEachMutation, resource unstructured.Unstructured, ctx context.Interface, logger logr.Logger) (patchedResource unstructured.Unstructured, err error) { +func applyForEachMutate(name string, foreach []kyvernov1.ForEachMutation, resource unstructured.Unstructured, logger logr.Logger) (patchedResource unstructured.Unstructured, err error) { patchedResource = resource for _, fe := range foreach { if fe.ForEachMutation != nil { @@ -71,10 +71,10 @@ func applyForEachMutate(name string, foreach []kyvernov1.ForEachMutation, resour return patchedResource, fmt.Errorf("failed to deserialize foreach: %w", err) } - return applyForEachMutate(name, nestedForEach, patchedResource, ctx, logger) + return applyForEachMutate(name, nestedForEach, patchedResource, logger) } - patchedResource, err = applyPatches(name, fe.GetPatchStrategicMerge(), fe.PatchesJSON6902, patchedResource, ctx, logger) + patchedResource, err = applyPatches(name, fe.GetPatchStrategicMerge(), fe.PatchesJSON6902, patchedResource, logger) if err != nil { return resource, err } @@ -83,8 +83,8 @@ func applyForEachMutate(name string, foreach []kyvernov1.ForEachMutation, resour return patchedResource, nil } -func applyPatches(name string, mergePatch apiextensions.JSON, jsonPatch string, resource unstructured.Unstructured, ctx context.Interface, logger logr.Logger) (unstructured.Unstructured, error) { - patcher := mutate.NewPatcher(name, mergePatch, jsonPatch, resource, ctx, logger) +func applyPatches(name string, mergePatch apiextensions.JSON, jsonPatch string, resource unstructured.Unstructured, logger logr.Logger) (unstructured.Unstructured, error) { + patcher := mutate.NewPatcher(name, mergePatch, jsonPatch, resource, logger) resp, mutatedResource := patcher.Patch() if resp.Status != engineapi.RuleStatusPass { return mutatedResource, fmt.Errorf("mutate status %q: %s", resp.Status, resp.Message) diff --git a/pkg/engine/handlers/mutation/common.go b/pkg/engine/handlers/mutation/common.go index 2491916ab1..c2cc22e3cb 100644 --- a/pkg/engine/handlers/mutation/common.go +++ b/pkg/engine/handlers/mutation/common.go @@ -144,7 +144,7 @@ func (f *forEachMutator) mutateElements(ctx context.Context, foreach kyvernov1.F mutateResp = m.mutateForEach(ctx) } else { - mutateResp = mutate.ForEach(f.rule.Name, foreach, policyContext.JSONContext(), patchedResource.unstructured, f.log) + mutateResp = mutate.ForEach(f.rule.Name, foreach, policyContext, patchedResource.unstructured, element, f.log) } if mutateResp.Status == engineapi.RuleStatusFail || mutateResp.Status == engineapi.RuleStatusError { diff --git a/pkg/engine/mutate/mutation.go b/pkg/engine/mutate/mutation.go index 89824bc0e8..5186cb13f2 100644 --- a/pkg/engine/mutate/mutation.go +++ b/pkg/engine/mutate/mutation.go @@ -42,7 +42,7 @@ func Mutate(rule *kyvernov1.Rule, ctx context.Interface, resource unstructured.U } m := updatedRule.Mutation - patcher := NewPatcher(updatedRule.Name, m.GetPatchStrategicMerge(), m.PatchesJSON6902, resource, ctx, logger) + patcher := NewPatcher(updatedRule.Name, m.GetPatchStrategicMerge(), m.PatchesJSON6902, resource, logger) if patcher == nil { return NewResponse(engineapi.RuleStatusError, resource, nil, "empty mutate rule") } @@ -69,13 +69,14 @@ func Mutate(rule *kyvernov1.Rule, ctx context.Interface, resource unstructured.U return NewResponse(engineapi.RuleStatusPass, patchedResource, resp.Patches, resp.Message) } -func ForEach(name string, foreach kyvernov1.ForEachMutation, ctx context.Interface, resource unstructured.Unstructured, logger logr.Logger) *Response { +func ForEach(name string, foreach kyvernov1.ForEachMutation, policyContext engineapi.PolicyContext, resource unstructured.Unstructured, element interface{}, logger logr.Logger) *Response { + ctx := policyContext.JSONContext() fe, err := substituteAllInForEach(foreach, ctx, logger) if err != nil { return NewErrorResponse("variable substitution failed", err) } - patcher := NewPatcher(name, fe.GetPatchStrategicMerge(), fe.PatchesJSON6902, resource, ctx, logger) + patcher := NewPatcher(name, fe.GetPatchStrategicMerge(), fe.PatchesJSON6902, resource, logger) if patcher == nil { return NewResponse(engineapi.RuleStatusError, unstructured.Unstructured{}, nil, "no patches found") } @@ -120,9 +121,9 @@ func substituteAllInForEach(fe kyvernov1.ForEachMutation, ctx context.Interface, return &updatedForEach, nil } -func NewPatcher(name string, strategicMergePatch apiextensions.JSON, jsonPatch string, r unstructured.Unstructured, ctx context.Interface, logger logr.Logger) patch.Patcher { +func NewPatcher(name string, strategicMergePatch apiextensions.JSON, jsonPatch string, r unstructured.Unstructured, logger logr.Logger) patch.Patcher { if strategicMergePatch != nil { - return patch.NewPatchStrategicMerge(name, strategicMergePatch, r, ctx, logger) + return patch.NewPatchStrategicMerge(name, strategicMergePatch, r, logger) } if len(jsonPatch) > 0 { diff --git a/pkg/engine/mutate/patch/patches.go b/pkg/engine/mutate/patch/patches.go index 4aafc6916a..53f0ca832e 100644 --- a/pkg/engine/mutate/patch/patches.go +++ b/pkg/engine/mutate/patch/patches.go @@ -3,7 +3,6 @@ package patch import ( "github.com/go-logr/logr" engineapi "github.com/kyverno/kyverno/pkg/engine/api" - "github.com/kyverno/kyverno/pkg/engine/context" "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" ) @@ -18,16 +17,14 @@ type patchStrategicMergeHandler struct { ruleName string patch apiextensions.JSON patchedResource unstructured.Unstructured - evalCtx context.EvalInterface logger logr.Logger } -func NewPatchStrategicMerge(ruleName string, patch apiextensions.JSON, patchedResource unstructured.Unstructured, context context.EvalInterface, logger logr.Logger) Patcher { +func NewPatchStrategicMerge(ruleName string, patch apiextensions.JSON, patchedResource unstructured.Unstructured, logger logr.Logger) Patcher { return patchStrategicMergeHandler{ ruleName: ruleName, patch: patch, patchedResource: patchedResource, - evalCtx: context, logger: logger, } }