-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
109 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package controllers | ||
|
||
import ( | ||
lru "github.com/hashicorp/golang-lru" | ||
|
||
"k8s.io/kubernetes/pkg/client/cache" | ||
"k8s.io/kubernetes/pkg/controller" | ||
"k8s.io/kubernetes/pkg/runtime" | ||
"k8s.io/kubernetes/pkg/storage/etcd" | ||
utilruntime "k8s.io/kubernetes/pkg/util/runtime" | ||
) | ||
|
||
// MutationCache is able to take the result of update operations and stores them in an LRU | ||
// that can be used to provide a more current view of a requested object. It requires interpretting | ||
// resourceVersions for comparisons. | ||
// Implementations must be thread-safe. | ||
type MutationCache interface { | ||
GetByKey(key string) (interface{}, bool, error) | ||
Mutation(interface{}) | ||
} | ||
|
||
type ResourceVersionComparator interface { | ||
CompareResourceVersion(lhs, rhs runtime.Object) int | ||
} | ||
|
||
// NewEtcdMutationCache gives back a MutationCache that understands how to deal with etcd backed objects | ||
func NewEtcdMutationCache(backingCache cache.Store) MutationCache { | ||
lru, err := lru.New(100) | ||
if err != nil { | ||
// errors only happen on invalid sizes, this would be programmer error | ||
panic(err) | ||
} | ||
|
||
return &mutationCache{ | ||
backingCache: backingCache, | ||
mutationCache: lru, | ||
comparator: etcd.APIObjectVersioner{}, | ||
} | ||
} | ||
|
||
// mutationCache doesn't guarantee that it returns values added via Mutation since they can page out and | ||
// since you can't distinguish between, "didn't observe create" and "was deleted after create", | ||
// if the key is missing from the backing cache, we always return it as missing | ||
type mutationCache struct { | ||
backingCache cache.Store | ||
mutationCache *lru.Cache | ||
|
||
comparator ResourceVersionComparator | ||
} | ||
|
||
func (c *mutationCache) GetByKey(key string) (interface{}, bool, error) { | ||
obj, exists, err := c.backingCache.GetByKey(key) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
if !exists { | ||
// we can't distinguish between, "didn't observe create" and "was deleted after create", so | ||
// if the key is missing, we always return it as missing | ||
return nil, false, nil | ||
} | ||
objRuntime, ok := obj.(runtime.Object) | ||
if !ok { | ||
return obj, true, nil | ||
} | ||
|
||
mutatedObj, exists := c.mutationCache.Get(key) | ||
if !exists { | ||
return obj, true, nil | ||
} | ||
mutatedObjRuntime, ok := mutatedObj.(runtime.Object) | ||
if !ok { | ||
return obj, true, nil | ||
} | ||
|
||
if c.comparator.CompareResourceVersion(objRuntime, mutatedObjRuntime) >= 0 { | ||
c.mutationCache.Remove(key) | ||
return obj, true, nil | ||
} | ||
|
||
return mutatedObj, true, nil | ||
} | ||
|
||
func (c *mutationCache) Mutation(obj interface{}) { | ||
key, err := controller.KeyFunc(obj) | ||
if err != nil { | ||
// this is a "nice to have", so failures shouldn't do anything weird | ||
utilruntime.HandleError(err) | ||
return | ||
} | ||
|
||
c.mutationCache.Add(key, obj) | ||
} |