func newOptimizedListWatcher()

in controllers/custom/custom_controller.go [182:229]


func newOptimizedListWatcher(ctx context.Context, restClient cache.Getter, resource string, namespace string,
	converter Converter, log logr.Logger) *cache.ListWatch {

	listFunc := func(options metav1.ListOptions) (runtime.Object, error) {
		list, err := restClient.Get().
			Namespace(namespace).
			Resource(resource).
			VersionedParams(&metav1.ListOptions{
				Limit:    options.Limit,
				Continue: options.Continue,
			}, metav1.ParameterCodec).
			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
		// the pager function
		convertedList, err := converter.ConvertList(list)
		return convertedList.(runtime.Object), err
	}

	// We don't need to modify the watcher, we will strip down the k8s object in the ProcessFunc
	// before storing the object in the data store.
	watchFunc := func(options metav1.ListOptions) (watch.Interface, error) {
		options.Watch = true
		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}
}