1
0
mirror of https://github.com/rancher/steve.git synced 2025-09-26 15:25:33 +00:00

support unwatchables in vai (#458)

* Create and use a synthetic watcher for non-watchable resources.

* Write unit tests for the synthetic watcher.

* Make the refresh interval for synthetic watchers configurable.

The default is to call `client.List(...)` every 5 seconds for each unwatchable GVK.

There are currently only 3 such GVKs right now so this will be informative
enough but not really noticeable.

* Pass the context into the synthetic watch func.

* Restore changes lost in rebasing.

---------

Co-authored-by: Tom Lebreux <tom.lebreux@suse.com>
This commit is contained in:
Eric Promislow
2025-02-20 12:45:58 -08:00
committed by GitHub
parent 6559fa9ad7
commit c906c36bc3
6 changed files with 364 additions and 19 deletions

View File

@@ -20,6 +20,8 @@ import (
"k8s.io/client-go/tools/cache"
)
var defaultRefreshTime = 5 * time.Second
// Informer is a SQLite-backed cache.SharedIndexInformer that can execute queries on listprocessor structs
type Informer struct {
cache.SharedIndexInformer
@@ -35,15 +37,22 @@ var newInformer = cache.NewSharedIndexInformer
// NewInformer returns a new SQLite-backed Informer for the type specified by schema in unstructured.Unstructured form
// using the specified client
func NewInformer(ctx context.Context, client dynamic.ResourceInterface, fields [][]string, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt bool, namespaced bool) (*Informer, error) {
func NewInformer(ctx context.Context, client dynamic.ResourceInterface, fields [][]string, transform cache.TransformFunc, gvk schema.GroupVersionKind, db db.Client, shouldEncrypt bool, namespaced bool, watchable bool) (*Informer, error) {
watchFunc := func(options metav1.ListOptions) (watch.Interface, error) {
return client.Watch(ctx, options)
}
if !watchable {
watchFunc = func(options metav1.ListOptions) (watch.Interface, error) {
ctx, cancel := context.WithCancel(ctx)
return newSyntheticWatcher(ctx, cancel).watch(client, options, defaultRefreshTime)
}
}
listWatcher := &cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
a, err := client.List(ctx, options)
return a, err
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return client.Watch(ctx, options)
},
WatchFunc: watchFunc,
}
example := &unstructured.Unstructured{}
@@ -97,6 +106,11 @@ func (i *Informer) ListByOptions(ctx context.Context, lo ListOptions, partitions
return i.ByOptionsLister.ListByOptions(ctx, lo, partitions, namespace)
}
// SetSyntheticWatchableInterval - call this function to override the default interval time of 5 seconds
func SetSyntheticWatchableInterval(interval time.Duration) {
defaultRefreshTime = interval
}
func informerNameFromGVK(gvk schema.GroupVersionKind) string {
return gvk.Group + "_" + gvk.Version + "_" + gvk.Kind
}