From f434fbf0c7579f9ef208fb554995b8b9574e76ea Mon Sep 17 00:00:00 2001 From: Richa Banker Date: Tue, 23 Jul 2024 22:19:02 -0700 Subject: [PATCH] init a common apiserver for TestAuthorizationDecisionCaching testcases Kubernetes-commit: 4acedb5132b2c3a7d61bd9e088c964af3fcfee3d --- .../plugin/policy/generic/policy_source.go | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/pkg/admission/plugin/policy/generic/policy_source.go b/pkg/admission/plugin/policy/generic/policy_source.go index 7d47c6f97..ca6cdc884 100644 --- a/pkg/admission/plugin/policy/generic/policy_source.go +++ b/pkg/admission/plugin/policy/generic/policy_source.go @@ -41,9 +41,12 @@ import ( "k8s.io/klog/v2" ) -var ( - PolicyRefreshInterval = 1 * time.Second -) +// Interval for refreshing policies. +// TODO: Consider reducing this to a shorter duration or replacing this entirely +// with checks that detect when a policy change took effect. +const policyRefreshIntervalDefault = 1 * time.Second + +var policyRefreshInterval = policyRefreshIntervalDefault type policySource[P runtime.Object, B runtime.Object, E Evaluator] struct { ctx context.Context @@ -126,6 +129,15 @@ func NewPolicySource[P runtime.Object, B runtime.Object, E Evaluator]( return res } +// SetPolicyRefreshIntervalForTests allows the refresh interval to be overridden during tests. +// This should only be called from tests. +func SetPolicyRefreshIntervalForTests(interval time.Duration) func() { + policyRefreshInterval = interval + return func() { + policyRefreshInterval = policyRefreshIntervalDefault + } +} + func (s *policySource[P, B, E]) Run(ctx context.Context) error { if s.ctx != nil { return fmt.Errorf("policy source already running") @@ -182,7 +194,7 @@ func (s *policySource[P, B, E]) Run(ctx context.Context) error { // and needs to be recompiled go func() { // Loop every 1 second until context is cancelled, refreshing policies - wait.Until(s.refreshPolicies, PolicyRefreshInterval, ctx.Done()) + wait.Until(s.refreshPolicies, policyRefreshInterval, ctx.Done()) }() <-ctx.Done()