Skip to content

Commit 4acedb5

Browse files
committed
init a common apiserver for TestAuthorizationDecisionCaching testcases
1 parent 342ecab commit 4acedb5

File tree

2 files changed

+209
-175
lines changed

2 files changed

+209
-175
lines changed

staging/src/k8s.io/apiserver/pkg/admission/plugin/policy/generic/policy_source.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ import (
4141
"k8s.io/klog/v2"
4242
)
4343

44-
var (
45-
PolicyRefreshInterval = 1 * time.Second
46-
)
44+
// Interval for refreshing policies.
45+
// TODO: Consider reducing this to a shorter duration or replacing this entirely
46+
// with checks that detect when a policy change took effect.
47+
const policyRefreshIntervalDefault = 1 * time.Second
48+
49+
var policyRefreshInterval = policyRefreshIntervalDefault
4750

4851
type policySource[P runtime.Object, B runtime.Object, E Evaluator] struct {
4952
ctx context.Context
@@ -126,6 +129,15 @@ func NewPolicySource[P runtime.Object, B runtime.Object, E Evaluator](
126129
return res
127130
}
128131

132+
// SetPolicyRefreshIntervalForTests allows the refresh interval to be overridden during tests.
133+
// This should only be called from tests.
134+
func SetPolicyRefreshIntervalForTests(interval time.Duration) func() {
135+
policyRefreshInterval = interval
136+
return func() {
137+
policyRefreshInterval = policyRefreshIntervalDefault
138+
}
139+
}
140+
129141
func (s *policySource[P, B, E]) Run(ctx context.Context) error {
130142
if s.ctx != nil {
131143
return fmt.Errorf("policy source already running")
@@ -182,7 +194,7 @@ func (s *policySource[P, B, E]) Run(ctx context.Context) error {
182194
// and needs to be recompiled
183195
go func() {
184196
// Loop every 1 second until context is cancelled, refreshing policies
185-
wait.Until(s.refreshPolicies, PolicyRefreshInterval, ctx.Done())
197+
wait.Until(s.refreshPolicies, policyRefreshInterval, ctx.Done())
186198
}()
187199

188200
<-ctx.Done()

0 commit comments

Comments
 (0)