Merge pull request #216 from gman0/cachepersister-idempotency-fix

k8s metadata cache: delete shouldn't fail on NotFound errors
This commit is contained in:
Huamin Chen 2019-02-25 08:04:26 -05:00 committed by GitHub
commit 4273408f2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 9 deletions

View File

@ -110,7 +110,7 @@ func (k8scm *K8sCMCache) ForAll(pattern string, destObj interface{}, f ForAllFun
continue continue
} }
if err = json.Unmarshal([]byte(data), destObj); err != nil { if err = json.Unmarshal([]byte(data), destObj); err != nil {
return errors.Wrap(err, "k8s-cm-cache: unmarshal error") return errors.Wrapf(err, "k8s-cm-cache: JSON unmarshaling failed for configmap %s", cm.ObjectMeta.Name)
} }
if err = f(cm.ObjectMeta.Name); err != nil { if err = f(cm.ObjectMeta.Name); err != nil {
return err return err
@ -123,12 +123,12 @@ func (k8scm *K8sCMCache) ForAll(pattern string, destObj interface{}, f ForAllFun
func (k8scm *K8sCMCache) Create(identifier string, data interface{}) error { func (k8scm *K8sCMCache) Create(identifier string, data interface{}) error {
cm, err := k8scm.getMetadataCM(identifier) cm, err := k8scm.getMetadataCM(identifier)
if cm != nil && err == nil { if cm != nil && err == nil {
klog.V(4).Infof("k8s-cm-cache: configmap already exists, skipping configmap creation") klog.V(4).Infof("k8s-cm-cache: configmap %s already exists, skipping configmap creation", identifier)
return nil return nil
} }
dataJSON, err := json.Marshal(data) dataJSON, err := json.Marshal(data)
if err != nil { if err != nil {
return errors.Wrap(err, "k8s-cm-cache: marshal error") return errors.Wrapf(err, "k8s-cm-cache: JSON marshaling failed for configmap %s", identifier)
} }
cm = &v1.ConfigMap{ cm = &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
@ -145,13 +145,13 @@ func (k8scm *K8sCMCache) Create(identifier string, data interface{}) error {
_, err = k8scm.Client.CoreV1().ConfigMaps(k8scm.Namespace).Create(cm) _, err = k8scm.Client.CoreV1().ConfigMaps(k8scm.Namespace).Create(cm)
if err != nil { if err != nil {
if apierrs.IsAlreadyExists(err) { if apierrs.IsAlreadyExists(err) {
klog.V(4).Infof("k8s-cm-cache: configmap already exists") klog.V(4).Infof("k8s-cm-cache: configmap %s already exists", identifier)
return nil return nil
} }
return errors.Wrapf(err, "k8s-cm-cache: couldn't persist %s metadata as configmap", identifier) return errors.Wrapf(err, "k8s-cm-cache: couldn't persist %s metadata as configmap", identifier)
} }
klog.V(4).Infof("k8s-cm-cache: configmap %s successfully created\n", identifier) klog.V(4).Infof("k8s-cm-cache: configmap %s successfully created", identifier)
return nil return nil
} }
@ -163,7 +163,7 @@ func (k8scm *K8sCMCache) Get(identifier string, data interface{}) error {
} }
err = json.Unmarshal([]byte(cm.Data[cmDataKey]), data) err = json.Unmarshal([]byte(cm.Data[cmDataKey]), data)
if err != nil { if err != nil {
return errors.Wrap(err, "k8s-cm-cache: unmarshal error") return errors.Wrapf(err, "k8s-cm-cache: JSON unmarshaling failed for configmap %s", identifier)
} }
return nil return nil
} }
@ -172,6 +172,11 @@ func (k8scm *K8sCMCache) Get(identifier string, data interface{}) error {
func (k8scm *K8sCMCache) Delete(identifier string) error { func (k8scm *K8sCMCache) Delete(identifier string) error {
err := k8scm.Client.CoreV1().ConfigMaps(k8scm.Namespace).Delete(identifier, nil) err := k8scm.Client.CoreV1().ConfigMaps(k8scm.Namespace).Delete(identifier, nil)
if err != nil { if err != nil {
if apierrs.IsNotFound(err) {
klog.V(4).Infof("k8s-cm-cache: cannot delete missing metadata configmap %s, assuming it's already deleted", identifier)
return nil
}
return errors.Wrapf(err, "k8s-cm-cache: couldn't delete metadata configmap %s", identifier) return errors.Wrapf(err, "k8s-cm-cache: couldn't delete metadata configmap %s", identifier)
} }
klog.V(4).Infof("k8s-cm-cache: successfully deleted metadata configmap %s", identifier) klog.V(4).Infof("k8s-cm-cache: successfully deleted metadata configmap %s", identifier)

View File

@ -44,7 +44,7 @@ func (nc *NodeCache) EnsureCacheDirectory(cacheDir string) error {
if _, err := os.Stat(fullPath); os.IsNotExist(err) { if _, err := os.Stat(fullPath); os.IsNotExist(err) {
// #nosec // #nosec
if err := os.Mkdir(fullPath, 0755); err != nil { if err := os.Mkdir(fullPath, 0755); err != nil {
return errors.Wrapf(err, "node-cache: failed to create %s folder with error: %v", fullPath, err) return errors.Wrapf(err, "node-cache: failed to create %s folder", fullPath)
} }
} }
return nil return nil
@ -152,9 +152,13 @@ func (nc *NodeCache) Delete(identifier string) error {
file := path.Join(nc.BasePath, cacheDir, identifier+".json") file := path.Join(nc.BasePath, cacheDir, identifier+".json")
err := os.Remove(file) err := os.Remove(file)
if err != nil { if err != nil {
if err != os.ErrNotExist { if err == os.ErrNotExist {
return errors.Wrapf(err, "node-cache: error removing file %s", file) klog.V(4).Infof("node-cache: cannot delete missing metadata storage file %s, assuming it's already deleted", file)
return nil
} }
return errors.Wrapf(err, "node-cache: error removing file %s", file)
} }
klog.V(4).Infof("node-cache: successfully deleted metadata storage file at: %+v\n", file) klog.V(4).Infof("node-cache: successfully deleted metadata storage file at: %+v\n", file)
return nil return nil