From a695ca108eb2c869ca593c8970d9a51f876acf49 Mon Sep 17 00:00:00 2001 From: Yash Thakkar Date: Tue, 6 Aug 2024 22:03:32 +0000 Subject: [PATCH 1/3] passing page limit to cache config --- controllers/custom/builder.go | 11 ++++++----- controllers/custom/custom_controller.go | 8 +++----- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/controllers/custom/builder.go b/controllers/custom/builder.go index 181c9bc8..21eeded5 100644 --- a/controllers/custom/builder.go +++ b/controllers/custom/builder.go @@ -113,15 +113,16 @@ func (b *Builder) Complete(reconciler Reconciler) (healthz.Checker, error) { workqueue.DefaultControllerRateLimiter(), b.options.Name) optimizedListWatch := newOptimizedListWatcher(b.ctx, b.clientSet.CoreV1().RESTClient(), - b.converter.Resource(), b.options.Namespace, b.options.PageLimit, b.converter) + b.converter.Resource(), b.options.Namespace, b.converter) // Create the config for low level controller with the custom converter // list and watch config := &cache.Config{ - Queue: cache.NewDeltaFIFO(b.converter.Indexer, b.dataStore), - ListerWatcher: optimizedListWatch, - ObjectType: b.converter.ResourceType(), - FullResyncPeriod: b.options.ResyncPeriod, + Queue: cache.NewDeltaFIFO(b.converter.Indexer, b.dataStore), + ListerWatcher: optimizedListWatch, + WatchListPageSize: int64(b.options.PageLimit), + ObjectType: b.converter.ResourceType(), + FullResyncPeriod: b.options.ResyncPeriod, Process: func(obj interface{}, _ bool) error { // from oldest to newest for _, d := range obj.(cache.Deltas) { diff --git a/controllers/custom/custom_controller.go b/controllers/custom/custom_controller.go index b3bfeee5..f1f7d257 100644 --- a/controllers/custom/custom_controller.go +++ b/controllers/custom/custom_controller.go @@ -178,23 +178,21 @@ func (c *CustomController) WaitForCacheSync(controller cache.Controller) { // newOptimizedListWatcher returns a list watcher with a custom list function that converts the // response for each page using the converter function and returns a general watcher -func newOptimizedListWatcher(ctx context.Context, restClient cache.Getter, resource string, namespace string, limit int, +func newOptimizedListWatcher(ctx context.Context, restClient cache.Getter, resource string, namespace string, converter Converter) *cache.ListWatch { listFunc := func(options metav1.ListOptions) (runtime.Object, error) { list, err := restClient.Get(). Namespace(namespace). Resource(resource). - // This needs to be done because just setting the limit using option's - // Limit is being overridden and the response is returned without pagination. VersionedParams(&metav1.ListOptions{ - Limit: int64(limit), + Limit: options.Limit, Continue: options.Continue, }, metav1.ParameterCodec). Do(ctx). Get() if err != nil { - return list, err + return nil, err } // Strip down the the list before passing the paginated response back to // the pager function From 5777979ad763e4adc53f477dcede110d18cb8e7b Mon Sep 17 00:00:00 2001 From: Yash Thakkar Date: Mon, 26 Aug 2024 23:51:04 -0700 Subject: [PATCH 2/3] adding error log to optimized list watcher --- controllers/custom/builder.go | 2 +- controllers/custom/custom_controller.go | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/controllers/custom/builder.go b/controllers/custom/builder.go index 21eeded5..232f1d42 100644 --- a/controllers/custom/builder.go +++ b/controllers/custom/builder.go @@ -113,7 +113,7 @@ func (b *Builder) Complete(reconciler Reconciler) (healthz.Checker, error) { workqueue.DefaultControllerRateLimiter(), b.options.Name) optimizedListWatch := newOptimizedListWatcher(b.ctx, b.clientSet.CoreV1().RESTClient(), - b.converter.Resource(), b.options.Namespace, b.converter) + b.converter.Resource(), b.options.Namespace, b.converter, b.log.WithName("listWatcher")) // Create the config for low level controller with the custom converter // list and watch diff --git a/controllers/custom/custom_controller.go b/controllers/custom/custom_controller.go index f1f7d257..ea39d18f 100644 --- a/controllers/custom/custom_controller.go +++ b/controllers/custom/custom_controller.go @@ -21,6 +21,7 @@ import ( "github.com/aws/amazon-vpc-resource-controller-k8s/pkg/condition" "github.com/go-logr/logr" + apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" @@ -179,7 +180,7 @@ func (c *CustomController) WaitForCacheSync(controller cache.Controller) { // newOptimizedListWatcher returns a list watcher with a custom list function that converts the // response for each page using the converter function and returns a general watcher func newOptimizedListWatcher(ctx context.Context, restClient cache.Getter, resource string, namespace string, - converter Converter) *cache.ListWatch { + converter Converter, log logr.Logger) *cache.ListWatch { listFunc := func(options metav1.ListOptions) (runtime.Object, error) { list, err := restClient.Get(). @@ -192,6 +193,11 @@ func newOptimizedListWatcher(ctx context.Context, restClient cache.Getter, resou Do(ctx). Get() if err != nil { + if statusErr, ok := err.(*apierrors.StatusError); ok { + log.Error(err, "List operation error", "code", statusErr.Status().Code) + } else { + log.Error(err, "List operation error") + } return nil, err } // Strip down the the list before passing the paginated response back to @@ -204,11 +210,20 @@ func newOptimizedListWatcher(ctx context.Context, restClient cache.Getter, resou // before storing the object in the data store. watchFunc := func(options metav1.ListOptions) (watch.Interface, error) { options.Watch = true - return restClient.Get(). + watch, err := restClient.Get(). Namespace(namespace). Resource(resource). VersionedParams(&options, metav1.ParameterCodec). Watch(ctx) + if err != nil { + if statusErr, ok := err.(*apierrors.StatusError); ok { + log.Error(err, "Watch operation error", "code", statusErr.Status().Code) + } else { + log.Error(err, "Watch operation error") + } + return nil, err + } + return watch,err } return &cache.ListWatch{ListFunc: listFunc, WatchFunc: watchFunc} } From 649244fc6968058eccf94ffe5b8f188acdc89f48 Mon Sep 17 00:00:00 2001 From: Yash Thakkar Date: Tue, 27 Aug 2024 11:08:17 -0700 Subject: [PATCH 3/3] minor change --- controllers/custom/custom_controller.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/custom/custom_controller.go b/controllers/custom/custom_controller.go index ea39d18f..df98b968 100644 --- a/controllers/custom/custom_controller.go +++ b/controllers/custom/custom_controller.go @@ -223,7 +223,7 @@ func newOptimizedListWatcher(ctx context.Context, restClient cache.Getter, resou } return nil, err } - return watch,err + return watch, err } return &cache.ListWatch{ListFunc: listFunc, WatchFunc: watchFunc} }