@@ -41,6 +41,13 @@ import (
41
41
"k8s.io/klog/v2"
42
42
)
43
43
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
50
+
44
51
type policySource [P runtime.Object , B runtime.Object , E Evaluator ] struct {
45
52
ctx context.Context
46
53
policyInformer generic.Informer [P ]
@@ -122,6 +129,15 @@ func NewPolicySource[P runtime.Object, B runtime.Object, E Evaluator](
122
129
return res
123
130
}
124
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
+
125
141
func (s * policySource [P , B , E ]) Run (ctx context.Context ) error {
126
142
if s .ctx != nil {
127
143
return fmt .Errorf ("policy source already running" )
@@ -178,7 +194,7 @@ func (s *policySource[P, B, E]) Run(ctx context.Context) error {
178
194
// and needs to be recompiled
179
195
go func () {
180
196
// Loop every 1 second until context is cancelled, refreshing policies
181
- wait .Until (s .refreshPolicies , 1 * time . Second , ctx .Done ())
197
+ wait .Until (s .refreshPolicies , policyRefreshInterval , ctx .Done ())
182
198
}()
183
199
184
200
<- ctx .Done ()
0 commit comments