@@ -21,8 +21,10 @@ import (
21
21
"errors"
22
22
"fmt"
23
23
"sync"
24
+ "time"
24
25
25
26
"k8s.io/apimachinery/pkg/api/meta"
27
+ "k8s.io/apimachinery/pkg/util/wait"
26
28
"k8s.io/client-go/util/workqueue"
27
29
"sigs.k8s.io/controller-runtime/pkg/client"
28
30
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -119,17 +121,34 @@ func (ks *Kind) Start(ctx context.Context, handler handler.EventHandler, queue w
119
121
ctx , ks .startCancel = context .WithCancel (ctx )
120
122
ks .started = make (chan error )
121
123
go func () {
122
- // Lookup the Informer from the Cache and add an EventHandler which populates the Queue
123
- i , err := ks .cache .GetInformer (ctx , ks .Type )
124
- if err != nil {
125
- kindMatchErr := & meta.NoKindMatchError {}
126
- if errors .As (err , & kindMatchErr ) {
127
- log .Error (err , "if kind is a CRD, it should be installed before calling Start" ,
128
- "kind" , kindMatchErr .GroupKind )
124
+ var (
125
+ i cache.Informer
126
+ lastErr error
127
+ )
128
+
129
+ // Tries to get an informer until it returns true,
130
+ // an error or the specified context is cancelled or expired.
131
+ if err := wait .PollImmediateUntilWithContext (ctx , 10 * time .Second , func (ctx context.Context ) (bool , error ) {
132
+ // Lookup the Informer from the Cache and add an EventHandler which populates the Queue
133
+ i , lastErr = ks .cache .GetInformer (ctx , ks .Type )
134
+ if lastErr != nil {
135
+ kindMatchErr := & meta.NoKindMatchError {}
136
+ if errors .As (lastErr , & kindMatchErr ) {
137
+ log .Error (lastErr , "if kind is a CRD, it should be installed before calling Start" ,
138
+ "kind" , kindMatchErr .GroupKind )
139
+ }
140
+ return false , nil // Retry.
141
+ }
142
+ return true , nil
143
+ }); err != nil {
144
+ if lastErr != nil {
145
+ ks .started <- fmt .Errorf ("failed to get informer from cache: %w" , lastErr )
146
+ return
129
147
}
130
148
ks .started <- err
131
149
return
132
150
}
151
+
133
152
i .AddEventHandler (internal.EventHandler {Queue : queue , EventHandler : handler , Predicates : prct })
134
153
if ! ks .cache .WaitForCacheSync (ctx ) {
135
154
// Would be great to return something more informative here
0 commit comments