mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
fix closed watchers in the resource-report-controller (#5350)
* fix: fix closed watchers in the resource-report-controller Signed-off-by: Rodrigo Fior Kuntzer <rodrigo@miro.com> * fix: using the resourceVersion from the list operation result Signed-off-by: Rodrigo Fior Kuntzer <rodrigo@miro.com> Signed-off-by: Rodrigo Fior Kuntzer <rodrigo@miro.com> Co-authored-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
This commit is contained in:
parent
449cd4356c
commit
bb90bb25ba
1 changed files with 40 additions and 33 deletions
|
@ -22,6 +22,8 @@ import (
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/apimachinery/pkg/types"
|
"k8s.io/apimachinery/pkg/types"
|
||||||
"k8s.io/apimachinery/pkg/watch"
|
"k8s.io/apimachinery/pkg/watch"
|
||||||
|
"k8s.io/client-go/tools/cache"
|
||||||
|
watchTools "k8s.io/client-go/tools/watch"
|
||||||
"k8s.io/client-go/util/workqueue"
|
"k8s.io/client-go/util/workqueue"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -153,15 +155,35 @@ func (c *controller) updateDynamicWatchers(ctx context.Context) error {
|
||||||
dynamicWatchers[gvr] = c.dynamicWatchers[gvr]
|
dynamicWatchers[gvr] = c.dynamicWatchers[gvr]
|
||||||
delete(c.dynamicWatchers, gvr)
|
delete(c.dynamicWatchers, gvr)
|
||||||
} else {
|
} else {
|
||||||
logger.Info("start watcher ...", "gvr", gvr)
|
hashes := map[types.UID]Resource{}
|
||||||
watchInterface, err := c.client.GetDynamicInterface().Resource(gvr).Watch(ctx, metav1.ListOptions{})
|
objs, err := c.client.GetDynamicInterface().Resource(gvr).List(ctx, metav1.ListOptions{})
|
||||||
|
if err != nil {
|
||||||
|
logger.Error(err, "failed to list resources", "gvr", gvr)
|
||||||
|
} else {
|
||||||
|
resourceVersion := objs.GetResourceVersion()
|
||||||
|
for _, obj := range objs.Items {
|
||||||
|
uid := obj.GetUID()
|
||||||
|
hash := reportutils.CalculateResourceHash(obj)
|
||||||
|
hashes[uid] = Resource{
|
||||||
|
Hash: hash,
|
||||||
|
Namespace: obj.GetNamespace(),
|
||||||
|
Name: obj.GetName(),
|
||||||
|
}
|
||||||
|
c.notify(uid, gvk, hashes[uid])
|
||||||
|
}
|
||||||
|
logger.Info("start watcher ...", "gvr", gvr, "resourceVersion", resourceVersion)
|
||||||
|
|
||||||
|
watchFunc := func(options metav1.ListOptions) (watch.Interface, error) {
|
||||||
|
return c.client.GetDynamicInterface().Resource(gvr).Watch(ctx, options)
|
||||||
|
}
|
||||||
|
watchInterface, err := watchTools.NewRetryWatcher(resourceVersion, &cache.ListWatch{WatchFunc: watchFunc})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(err, "failed to create watcher", "gvr", gvr)
|
logger.Error(err, "failed to create watcher", "gvr", gvr)
|
||||||
} else {
|
} else {
|
||||||
w := &watcher{
|
w := &watcher{
|
||||||
watcher: watchInterface,
|
watcher: watchInterface,
|
||||||
gvk: gvk,
|
gvk: gvk,
|
||||||
hashes: map[types.UID]Resource{},
|
hashes: hashes,
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
gvr := gvr
|
gvr := gvr
|
||||||
|
@ -177,21 +199,6 @@ func (c *controller) updateDynamicWatchers(ctx context.Context) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
objs, err := c.client.GetDynamicInterface().Resource(gvr).List(ctx, metav1.ListOptions{})
|
|
||||||
if err != nil {
|
|
||||||
logger.Error(err, "failed to list resources", "gvr", gvr)
|
|
||||||
watchInterface.Stop()
|
|
||||||
} else {
|
|
||||||
for _, obj := range objs.Items {
|
|
||||||
uid := obj.GetUID()
|
|
||||||
hash := reportutils.CalculateResourceHash(obj)
|
|
||||||
w.hashes[uid] = Resource{
|
|
||||||
Hash: hash,
|
|
||||||
Namespace: obj.GetNamespace(),
|
|
||||||
Name: obj.GetName(),
|
|
||||||
}
|
|
||||||
c.notify(uid, w.gvk, w.hashes[uid])
|
|
||||||
}
|
|
||||||
dynamicWatchers[gvr] = w
|
dynamicWatchers[gvr] = w
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue