@@ -21,6 +21,7 @@ import (
21
21
22
22
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/condition"
23
23
"github.com/go-logr/logr"
24
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
24
25
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25
26
"k8s.io/apimachinery/pkg/runtime"
26
27
"k8s.io/apimachinery/pkg/types"
@@ -178,23 +179,26 @@ func (c *CustomController) WaitForCacheSync(controller cache.Controller) {
178
179
179
180
// newOptimizedListWatcher returns a list watcher with a custom list function that converts the
180
181
// response for each page using the converter function and returns a general watcher
181
- func newOptimizedListWatcher (ctx context.Context , restClient cache.Getter , resource string , namespace string , limit int ,
182
- converter Converter ) * cache.ListWatch {
182
+ func newOptimizedListWatcher (ctx context.Context , restClient cache.Getter , resource string , namespace string ,
183
+ converter Converter , log logr. Logger ) * cache.ListWatch {
183
184
184
185
listFunc := func (options metav1.ListOptions ) (runtime.Object , error ) {
185
186
list , err := restClient .Get ().
186
187
Namespace (namespace ).
187
188
Resource (resource ).
188
- // This needs to be done because just setting the limit using option's
189
- // Limit is being overridden and the response is returned without pagination.
190
189
VersionedParams (& metav1.ListOptions {
191
- Limit : int64 ( limit ) ,
190
+ Limit : options . Limit ,
192
191
Continue : options .Continue ,
193
192
}, metav1 .ParameterCodec ).
194
193
Do (ctx ).
195
194
Get ()
196
195
if err != nil {
197
- return list , err
196
+ if statusErr , ok := err .(* apierrors.StatusError ); ok {
197
+ log .Error (err , "List operation error" , "code" , statusErr .Status ().Code )
198
+ } else {
199
+ log .Error (err , "List operation error" )
200
+ }
201
+ return nil , err
198
202
}
199
203
// Strip down the the list before passing the paginated response back to
200
204
// the pager function
@@ -206,11 +210,20 @@ func newOptimizedListWatcher(ctx context.Context, restClient cache.Getter, resou
206
210
// before storing the object in the data store.
207
211
watchFunc := func (options metav1.ListOptions ) (watch.Interface , error ) {
208
212
options .Watch = true
209
- return restClient .Get ().
213
+ watch , err := restClient .Get ().
210
214
Namespace (namespace ).
211
215
Resource (resource ).
212
216
VersionedParams (& options , metav1 .ParameterCodec ).
213
217
Watch (ctx )
218
+ if err != nil {
219
+ if statusErr , ok := err .(* apierrors.StatusError ); ok {
220
+ log .Error (err , "Watch operation error" , "code" , statusErr .Status ().Code )
221
+ } else {
222
+ log .Error (err , "Watch operation error" )
223
+ }
224
+ return nil , err
225
+ }
226
+ return watch , err
214
227
}
215
228
return & cache.ListWatch {ListFunc : listFunc , WatchFunc : watchFunc }
216
229
}
0 commit comments