@@ -22,6 +22,8 @@ import (
22
22
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23
23
"k8s.io/apimachinery/pkg/watch"
24
24
"k8s.io/client-go/kubernetes"
25
+ "k8s.io/client-go/tools/cache"
26
+ watchtools "k8s.io/client-go/tools/watch"
25
27
)
26
28
27
29
type Watcher struct {
@@ -136,35 +138,40 @@ func (w *Watcher) watch(ctx context.Context, env string, client kubernetes.Inter
136
138
w .handlersCounter .Add (ctx , 1 , metric .WithAttributes (attribute .String ("environment" , env )))
137
139
defer w .handlersCounter .Add (ctx , - 1 , metric .WithAttributes (attribute .String ("environment" , env )))
138
140
139
- // Events we want to watch for
140
- // SuccessfulRescale - Check for successful rescale events
141
- // Killing - Check for liveness failures
141
+ newWatcher := func (selector string ) (* watchtools.RetryWatcher , error ) {
142
+ list , err := client .EventsV1 ().Events ("" ).List (ctx , metav1.ListOptions {
143
+ FieldSelector : selector ,
144
+ Limit : 10 ,
145
+ })
146
+ if err != nil {
147
+ return nil , err
148
+ }
142
149
143
- list , err := client .EventsV1 ().Events ("" ).List (ctx , metav1.ListOptions {})
144
- if err != nil {
145
- return fmt .Errorf ("failed to list events: %w" , err )
150
+ return watchtools .NewRetryWatcher (list .ResourceVersion , & cache.ListWatch {
151
+ WatchFunc : func (options metav1.ListOptions ) (watch.Interface , error ) {
152
+ options .FieldSelector = selector
153
+ return client .EventsV1 ().Events ("" ).Watch (context .Background (), options )
154
+ },
155
+ })
146
156
}
147
157
148
- w .log .WithField ("len" , len (list .Items )).Debug ("listed events" )
149
-
158
+ // Events we want to watch for
159
+ // SuccessfulRescale - Check for successful rescale events
160
+ // Killing - Check for liveness failures
150
161
closeAndDrain := func (w watch.Interface ) {
151
162
w .Stop ()
152
163
for range w .ResultChan () {
153
164
// Drain the channel
154
165
}
155
166
}
156
167
157
- rescale , err := client .EventsV1 ().Events ("" ).Watch (ctx , metav1.ListOptions {
158
- FieldSelector : "reason=SuccessfulRescale,metadata.namespace!=nais-system" ,
159
- })
168
+ rescale , err := newWatcher ("reason=SuccessfulRescale,metadata.namespace!=nais-system" )
160
169
if err != nil {
161
170
return fmt .Errorf ("failed to watch for rescale events: %w" , err )
162
171
}
163
172
defer closeAndDrain (rescale )
164
173
165
- killing , err := client .EventsV1 ().Events ("" ).Watch (ctx , metav1.ListOptions {
166
- FieldSelector : "reason=Killing,metadata.namespace!=nais-system" ,
167
- })
174
+ killing , err := newWatcher ("reason=Killing,metadata.namespace!=nais-system" )
168
175
if err != nil {
169
176
return fmt .Errorf ("failed to watch for killing events: %w" , err )
170
177
}
0 commit comments