Skip to content

Commit

Permalink
fix(cherry-pick kyverno#10057): add mutex to mock policy context buil…
Browse files Browse the repository at this point in the history
…der (kyverno#10059)

Signed-off-by: Vishal Choudhary <[email protected]>
  • Loading branch information
vishal-chdhry authored Apr 17, 2024
1 parent 0c0753b commit 5915865
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
7 changes: 4 additions & 3 deletions pkg/webhooks/resource/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,10 +631,10 @@ func makeKey(policy kyverno.PolicyInterface) string {
}

type mockPolicyContextBuilder struct {
sync.Mutex
configuration config.Configuration
jp jmespath.Interface
contexts []*engine.PolicyContext
count int
}

func newMockPolicyContextBuilder(
Expand All @@ -645,11 +645,13 @@ func newMockPolicyContextBuilder(
configuration: configuration,
jp: jp,
contexts: make([]*policycontext.PolicyContext, 0),
count: 0,
}
}

func (b *mockPolicyContextBuilder) Build(request admissionv1.AdmissionRequest, roles, clusterRoles []string, gvk schema.GroupVersionKind) (*engine.PolicyContext, error) {
b.Lock()
defer b.Unlock()

userRequestInfo := kyvernov1beta1.RequestInfo{
AdmissionUserInfo: *request.UserInfo.DeepCopy(),
Roles: roles,
Expand All @@ -659,7 +661,6 @@ func (b *mockPolicyContextBuilder) Build(request admissionv1.AdmissionRequest, r
if err != nil {
return nil, err
}
b.count += 1
b.contexts = append(b.contexts, pc)
return pc, err
}
14 changes: 8 additions & 6 deletions pkg/webhooks/resource/updaterequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ func (h *resourceHandlers) handleBackgroundApplies(ctx context.Context, logger l
}

func (h *resourceHandlers) handleMutateExisting(ctx context.Context, logger logr.Logger, request handlers.AdmissionRequest, policies []kyvernov1.PolicyInterface, admissionRequestTimestamp time.Time, wg *sync.WaitGroup) {
if wg != nil { // for unit testing purposes
defer wg.Done()
}

policyContext, err := h.buildPolicyContextFromAdmissionRequest(logger, request)
if err != nil {
logger.Error(err, "failed to create policy context")
return
}
if wg != nil { // for unit testing purposes
defer wg.Done()
}

if request.AdmissionRequest.Operation == admissionv1.Delete {
policyContext = policyContext.WithNewResource(policyContext.OldResource())
Expand Down Expand Up @@ -95,14 +96,15 @@ func (h *resourceHandlers) handleMutateExisting(ctx context.Context, logger logr
}

func (h *resourceHandlers) handleGenerate(ctx context.Context, logger logr.Logger, request handlers.AdmissionRequest, generatePolicies []kyvernov1.PolicyInterface, ts time.Time, wg *sync.WaitGroup) {
if wg != nil { // for unit testing purposes
defer wg.Done()
}

policyContext, err := h.buildPolicyContextFromAdmissionRequest(logger, request)
if err != nil {
logger.Error(err, "failed to create policy context")
return
}
if wg != nil { // for unit testing purposes
defer wg.Done()
}

gh := generation.NewGenerationHandler(logger, h.engine, h.client, h.kyvernoClient, h.nsLister, h.urLister, h.cpolLister, h.polLister, h.urGenerator, h.eventGen, h.metricsConfig, h.backgroundServiceAccountName)
var policies []kyvernov1.PolicyInterface
Expand Down

0 comments on commit 5915865

Please sign in to comment.