mirror of
https://github.com/kyverno/kyverno.git
synced 2024-12-14 11:57:48 +00:00
fix: watch error in resource controller (#4751)
Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
This commit is contained in:
parent
5a3532da91
commit
7fa796e24a
1 changed files with 38 additions and 29 deletions
|
@ -139,37 +139,46 @@ func (c *controller) updateDynamicWatchers() error {
|
||||||
delete(c.dynamicWatchers, gvr)
|
delete(c.dynamicWatchers, gvr)
|
||||||
} else {
|
} else {
|
||||||
logger.Info("start watcher ...", "gvr", gvr)
|
logger.Info("start watcher ...", "gvr", gvr)
|
||||||
watchInterface, _ := c.client.GetDynamicInterface().Resource(gvr).Watch(context.TODO(), metav1.ListOptions{})
|
watchInterface, err := c.client.GetDynamicInterface().Resource(gvr).Watch(context.TODO(), metav1.ListOptions{})
|
||||||
w := &watcher{
|
if err != nil {
|
||||||
watcher: watchInterface,
|
logger.Error(err, "failed to create watcher", "gvr", gvr)
|
||||||
gvk: gvr.GroupVersion().WithKind(kind),
|
} else {
|
||||||
hashes: map[types.UID]Resource{},
|
w := &watcher{
|
||||||
}
|
watcher: watchInterface,
|
||||||
go func() {
|
gvk: gvr.GroupVersion().WithKind(kind),
|
||||||
gvr := gvr
|
hashes: map[types.UID]Resource{},
|
||||||
defer logger.Info("watcher stopped")
|
}
|
||||||
for event := range watchInterface.ResultChan() {
|
go func() {
|
||||||
switch event.Type {
|
gvr := gvr
|
||||||
case watch.Added:
|
defer logger.Info("watcher stopped")
|
||||||
c.updateHash(event.Object.(*unstructured.Unstructured), gvr)
|
for event := range watchInterface.ResultChan() {
|
||||||
case watch.Modified:
|
switch event.Type {
|
||||||
c.updateHash(event.Object.(*unstructured.Unstructured), gvr)
|
case watch.Added:
|
||||||
case watch.Deleted:
|
c.updateHash(event.Object.(*unstructured.Unstructured), gvr)
|
||||||
c.deleteHash(event.Object.(*unstructured.Unstructured), gvr)
|
case watch.Modified:
|
||||||
|
c.updateHash(event.Object.(*unstructured.Unstructured), gvr)
|
||||||
|
case watch.Deleted:
|
||||||
|
c.deleteHash(event.Object.(*unstructured.Unstructured), gvr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
|
objs, err := c.client.GetDynamicInterface().Resource(gvr).List(context.TODO(), 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
|
|
||||||
objs, _ := c.client.GetDynamicInterface().Resource(gvr).List(context.TODO(), metav1.ListOptions{})
|
|
||||||
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])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue