Skip to content

Commit

Permalink
NETOBSERV-1404 mitigate narrowcache weakness on missed events
Browse files Browse the repository at this point in the history
narrowcache might contain outdated data in case of missed events, add a
mechanism to invalidate entries on errors
  • Loading branch information
jotak committed Nov 30, 2023
1 parent 654de83 commit f9a35d8
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions pkg/narrowcache/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/workqueue"
Expand Down Expand Up @@ -230,3 +231,56 @@ func (c *Client) GetSource(ctx context.Context, obj client.Object) (source.Sourc
},
}, nil
}

func (c *Client) clearEntry(obj client.Object) {
c.wmut.Lock()
defer c.wmut.Unlock()

key := types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}
gvk, _ := c.GroupVersionKindFor(obj)
strGVK := gvk.String()
if _, managed := c.watchedGVKs[strGVK]; managed {
strGVK := gvk.String()
objKey := strGVK + "|" + key.String()
delete(c.watchedObjects, objKey)
}
}

func (c *Client) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
if err := c.Client.Create(ctx, obj, opts...); err != nil {
// might be due to an outdated cache, clear the corresponding entry
log.FromContext(ctx).
WithName("narrowcache").
WithValues("name", obj.GetName(), "namespace", obj.GetNamespace()).
Info("Invalidating cache entry")
c.clearEntry(obj)
return err
}
return nil
}

func (c *Client) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
if err := c.Client.Delete(ctx, obj, opts...); err != nil {
// might be due to an outdated cache, clear the corresponding entry
log.FromContext(ctx).
WithName("narrowcache").
WithValues("name", obj.GetName(), "namespace", obj.GetNamespace()).
Info("Invalidating cache entry")
c.clearEntry(obj)
return err
}
return nil
}

func (c *Client) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
if err := c.Client.Update(ctx, obj, opts...); err != nil {
// might be due to an outdated cache, clear the corresponding entry
log.FromContext(ctx).
WithName("narrowcache").
WithValues("name", obj.GetName(), "namespace", obj.GetNamespace()).
Info("Invalidating cache entry")
c.clearEntry(obj)
return err
}
return nil
}

0 comments on commit f9a35d8

Please sign in to comment.