util/cachepersister: check and return CacheEntryNotFound error in Get()

This commit is contained in:
gman 2019-02-25 18:05:20 +01:00
parent e627a6166c
commit e5dbea15d3
3 changed files with 16 additions and 2 deletions

View File

@ -27,9 +27,15 @@ const (
PluginFolder = "/var/lib/kubelet/plugins" PluginFolder = "/var/lib/kubelet/plugins"
) )
// ForAllFunc stores metadata with identifier // ForAllFunc is a unary predicate for visiting all cache entries
// matching the `pattern' in CachePersister's ForAll function.
type ForAllFunc func(identifier string) error type ForAllFunc func(identifier string) error
// CacheEntryNotFound is an error type for "Not Found" cache errors
type CacheEntryNotFound struct {
error
}
// CachePersister interface implemented for store // CachePersister interface implemented for store
type CachePersister interface { type CachePersister interface {
Create(identifier string, data interface{}) error Create(identifier string, data interface{}) error

View File

@ -159,6 +159,10 @@ func (k8scm *K8sCMCache) Create(identifier string, data interface{}) error {
func (k8scm *K8sCMCache) Get(identifier string, data interface{}) error { func (k8scm *K8sCMCache) Get(identifier string, data interface{}) error {
cm, err := k8scm.getMetadataCM(identifier) cm, err := k8scm.getMetadataCM(identifier)
if err != nil { if err != nil {
if apierrs.IsNotFound(err) {
return &CacheEntryNotFound{err}
}
return err return err
} }
err = json.Unmarshal([]byte(cm.Data[cmDataKey]), data) err = json.Unmarshal([]byte(cm.Data[cmDataKey]), data)

View File

@ -130,6 +130,10 @@ func (nc *NodeCache) Get(identifier string, data interface{}) error {
// #nosec // #nosec
fp, err := os.Open(file) fp, err := os.Open(file)
if err != nil { if err != nil {
if os.IsNotExist(errors.Cause(err)) {
return &CacheEntryNotFound{err}
}
return errors.Wrapf(err, "node-cache: open error for %s", file) return errors.Wrapf(err, "node-cache: open error for %s", file)
} }