mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 05:27:21 +00:00
Make sure we remove old object from index before adding new
This commit is contained in:
parent
1cf69bdefc
commit
53f1efa484
21
pkg/client/cache/store.go
vendored
21
pkg/client/cache/store.go
vendored
@ -78,22 +78,28 @@ func (c *cache) Add(obj interface{}) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("couldn't create key for object: %v", err)
|
return fmt.Errorf("couldn't create key for object: %v", err)
|
||||||
}
|
}
|
||||||
|
// keep a pointer to whatever could have been there previously
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
|
oldObject := c.items[key]
|
||||||
c.items[key] = obj
|
c.items[key] = obj
|
||||||
c.updateIndices(obj)
|
c.updateIndices(oldObject, obj)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateIndices modifies the objects location in the managed indexes
|
// updateIndices modifies the objects location in the managed indexes, if this is an update, you must provide an oldObj
|
||||||
// updateIndices must be called from a function that already has a lock on the cache
|
// updateIndices must be called from a function that already has a lock on the cache
|
||||||
func (c *cache) updateIndices(obj interface{}) error {
|
func (c *cache) updateIndices(oldObj interface{}, newObj interface{}) error {
|
||||||
key, err := c.keyFunc(obj)
|
// if we got an old object, we need to remove it before we add it again
|
||||||
|
if oldObj != nil {
|
||||||
|
c.deleteFromIndices(oldObj)
|
||||||
|
}
|
||||||
|
key, err := c.keyFunc(newObj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for name, indexFunc := range c.indexers {
|
for name, indexFunc := range c.indexers {
|
||||||
indexValue, err := indexFunc(obj)
|
indexValue, err := indexFunc(newObj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -143,8 +149,9 @@ func (c *cache) Update(obj interface{}) error {
|
|||||||
}
|
}
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
|
oldObject := c.items[key]
|
||||||
c.items[key] = obj
|
c.items[key] = obj
|
||||||
c.updateIndices(obj)
|
c.updateIndices(oldObject, obj)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,7 +243,7 @@ func (c *cache) Replace(list []interface{}) error {
|
|||||||
// rebuild any index
|
// rebuild any index
|
||||||
c.indices = Indices{}
|
c.indices = Indices{}
|
||||||
for _, item := range c.items {
|
for _, item := range c.items {
|
||||||
c.updateIndices(item)
|
c.updateIndices(nil, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user