@@ -16,6 +16,7 @@ package trustroot
16
16
17
17
import (
18
18
"context"
19
+ "time"
19
20
20
21
"k8s.io/client-go/tools/cache"
21
22
kubeclient "knative.dev/pkg/client/injection/kube/client"
@@ -36,6 +37,8 @@ import (
36
37
// use it in tests as well.
37
38
const FinalizerName = "trustroots.policy.sigstore.dev"
38
39
40
+ type trustrootResyncPeriodKey struct {}
41
+
39
42
// NewController creates a Reconciler and returns the result of NewImpl.
40
43
func NewController (
41
44
ctx context.Context ,
@@ -63,20 +66,34 @@ func NewController(
63
66
// ConfigMap but there are no changes to the TrustRoot, it needs
64
67
// to be synced.
65
68
grCb := func (obj interface {}) {
66
- logging .FromContext (ctx ).Info ("Doing a global resync on TrustRoot due to ConfigMap changing." )
69
+ logging .FromContext (ctx ).Info ("Doing a global resync on TrustRoot due to ConfigMap changing or resync period ." )
67
70
impl .GlobalResync (trustrootInformer .Informer ())
68
71
}
69
72
// Resync on only ConfigMap changes that pertain to the one I care about.
70
73
// We could also fetch/construct the store and use CM watcher for it, but
71
74
// since we need a lister for it anyways in the reconciler, just set up
72
75
// the watch here.
73
- if _ , err := configMapInformer .Informer ().AddEventHandler (cache.FilteringResourceEventHandler {
76
+ if _ , err := configMapInformer .Informer ().AddEventHandlerWithResyncPeriod (cache.FilteringResourceEventHandler {
74
77
FilterFunc : pkgreconciler .ChainFilterFuncs (
75
78
pkgreconciler .NamespaceFilterFunc (system .Namespace ()),
76
79
pkgreconciler .NameFilterFunc (config .SigstoreKeysConfigName )),
77
80
Handler : controller .HandleAll (grCb ),
78
- }); err != nil {
79
- logging .FromContext (ctx ).Warnf ("Failed configMapInformer AddEventHandler () %v" , err )
81
+ }, FromContextOrDefaults ( ctx ) ); err != nil {
82
+ logging .FromContext (ctx ).Warnf ("Failed configMapInformer AddEventHandlerWithResyncPeriod () %v" , err )
80
83
}
81
84
return impl
82
85
}
86
+
87
+ func ToContext (ctx context.Context , duration time.Duration ) context.Context {
88
+ return context .WithValue (ctx , trustrootResyncPeriodKey {}, duration )
89
+ }
90
+
91
+ // FromContextOrDefaults returns a stored trustrootResyncPeriod if attached.
92
+ // If not found, it returns a default duration
93
+ func FromContextOrDefaults (ctx context.Context ) time.Duration {
94
+ x , ok := ctx .Value (trustrootResyncPeriodKey {}).(time.Duration )
95
+ if ok {
96
+ return x
97
+ }
98
+ return controller .DefaultResyncPeriod
99
+ }
0 commit comments