@@ -41,9 +41,12 @@ import (
41
41
"k8s.io/klog/v2"
42
42
)
43
43
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
47
50
48
51
type policySource [P runtime.Object , B runtime.Object , E Evaluator ] struct {
49
52
ctx context.Context
@@ -126,6 +129,15 @@ func NewPolicySource[P runtime.Object, B runtime.Object, E Evaluator](
126
129
return res
127
130
}
128
131
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
+
129
141
func (s * policySource [P , B , E ]) Run (ctx context.Context ) error {
130
142
if s .ctx != nil {
131
143
return fmt .Errorf ("policy source already running" )
@@ -182,7 +194,7 @@ func (s *policySource[P, B, E]) Run(ctx context.Context) error {
182
194
// and needs to be recompiled
183
195
go func () {
184
196
// 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 ())
186
198
}()
187
199
188
200
<- ctx .Done ()
0 commit comments