mirror of
https://github.com/ceph/ceph-csi.git
synced 2024-11-10 08:20:23 +00:00
b318964af5
issue #217 Goal we try to solve when csi exit unexpect, the pod use cephfs pv can not auto recovery because lost mount relation until pod be killed and reschedule to other node. i think this is may be a problem. may be csi plugin can do more thing to remount the old path so when pod may be auto recovery when pod exit and restart, the old mount path can use. NoGoal Pod should exit and restart when csi plugin pod exit and mount point lost. if pod not exit will get error of **transport endpoint is not connected**. implment logic csi-plugin start: 1. load all MountCachEntry from node local dir 2. check if volID exist in cluster, if no we ignore this entry, if yes continue 3. check if stagingPath exist, if yes we mount the path 4. check if all targetPath exist, if yes we binmount to staging path NodeServer: 1. NodeStageVolume: add MountCachEntry on local dir include readonly attr and ceph secret 2. NodeStagePublishVolume: add pod bind mount path to MountCachEntry and persist local dir 3. NodeStageunPublishVolume: remove pod bind mount path From MountCachEntry and persist local dir 4. NodeStageunStageVolume: remove MountCachEntry from local dir
183 lines
5.2 KiB
Go
183 lines
5.2 KiB
Go
/*
|
|
Copyright 2018 The Kubernetes Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package util
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/pkg/errors"
|
|
"k8s.io/klog"
|
|
)
|
|
|
|
// NodeCache to store metadata
|
|
type NodeCache struct {
|
|
BasePath string
|
|
CacheDir string
|
|
}
|
|
|
|
var errDec = errors.New("file not found")
|
|
|
|
// EnsureCacheDirectory creates cache directory if not present
|
|
func (nc *NodeCache) EnsureCacheDirectory(cacheDir string) error {
|
|
fullPath := path.Join(nc.BasePath, cacheDir)
|
|
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
|
|
// #nosec
|
|
if err := os.Mkdir(fullPath, 0755); err != nil {
|
|
return errors.Wrapf(err, "node-cache: failed to create %s folder", fullPath)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//ForAll list the metadata in Nodecache and filters outs based on the pattern
|
|
func (nc *NodeCache) ForAll(pattern string, destObj interface{}, f ForAllFunc) error {
|
|
err := nc.EnsureCacheDirectory(nc.CacheDir)
|
|
if err != nil {
|
|
return errors.Wrap(err, "node-cache: couldn't ensure cache directory exists")
|
|
}
|
|
files, err := ioutil.ReadDir(path.Join(nc.BasePath, nc.CacheDir))
|
|
if err != nil {
|
|
return errors.Wrapf(err, "node-cache: failed to read %s folder", nc.BasePath)
|
|
}
|
|
path := path.Join(nc.BasePath, nc.CacheDir)
|
|
for _, file := range files {
|
|
err = decodeObj(path, pattern, file, destObj)
|
|
if err == errDec {
|
|
continue
|
|
} else if err == nil {
|
|
if err = f(strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return err
|
|
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func decodeObj(filepath, pattern string, file os.FileInfo, destObj interface{}) error {
|
|
match, err := regexp.MatchString(pattern, file.Name())
|
|
if err != nil || !match {
|
|
return errDec
|
|
}
|
|
if !strings.HasSuffix(file.Name(), ".json") {
|
|
return errDec
|
|
}
|
|
// #nosec
|
|
fp, err := os.Open(path.Join(filepath, file.Name()))
|
|
if err != nil {
|
|
klog.Infof("node-cache: open file: %s err %v", file.Name(), err)
|
|
return errDec
|
|
}
|
|
decoder := json.NewDecoder(fp)
|
|
if err = decoder.Decode(destObj); err != nil {
|
|
if err = fp.Close(); err != nil {
|
|
return errors.Wrapf(err, "failed to close file %s", file.Name())
|
|
|
|
}
|
|
return errors.Wrapf(err, "node-cache: couldn't decode file %s", file.Name())
|
|
}
|
|
return nil
|
|
|
|
}
|
|
|
|
func (nc *NodeCache) Update(identifier string, data interface{}) error {
|
|
file := path.Join(nc.BasePath, nc.CacheDir, identifier+".json")
|
|
identifierTmp := identifier + ".creating"
|
|
fileTmp := path.Join(nc.BasePath, nc.CacheDir, identifierTmp+".json")
|
|
os.Remove(fileTmp)
|
|
if err := nc.Create(identifierTmp, data); err != nil {
|
|
return errors.Wrapf(err, "node-cache: failed to create metadata storage file %s\n", file)
|
|
}
|
|
if err := os.Rename(fileTmp, file); err != nil {
|
|
return errors.Wrapf(err, "node-cache: couldn't rename %s as %s", fileTmp, file)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Create creates the metadata file in cache directory with identifier name
|
|
func (nc *NodeCache) Create(identifier string, data interface{}) error {
|
|
file := path.Join(nc.BasePath, nc.CacheDir, identifier+".json")
|
|
fp, err := os.Create(file)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "node-cache: failed to create metadata storage file %s\n", file)
|
|
}
|
|
|
|
defer func() {
|
|
if err = fp.Close(); err != nil {
|
|
klog.Warningf("failed to close file:%s %v", fp.Name(), err)
|
|
}
|
|
}()
|
|
|
|
encoder := json.NewEncoder(fp)
|
|
if err = encoder.Encode(data); err != nil {
|
|
return errors.Wrapf(err, "node-cache: failed to encode metadata for file: %s\n", file)
|
|
}
|
|
klog.V(4).Infof("node-cache: successfully saved metadata into file: %s\n", file)
|
|
return nil
|
|
}
|
|
|
|
// Get retrieves the metadata from cache directory with identifier name
|
|
func (nc *NodeCache) Get(identifier string, data interface{}) error {
|
|
file := path.Join(nc.BasePath, nc.CacheDir, identifier+".json")
|
|
// #nosec
|
|
fp, err := os.Open(file)
|
|
if err != nil {
|
|
if os.IsNotExist(errors.Cause(err)) {
|
|
return &CacheEntryNotFound{err}
|
|
}
|
|
|
|
return errors.Wrapf(err, "node-cache: open error for %s", file)
|
|
}
|
|
|
|
defer func() {
|
|
if err = fp.Close(); err != nil {
|
|
klog.Warningf("failed to close file:%s %v", fp.Name(), err)
|
|
}
|
|
}()
|
|
|
|
decoder := json.NewDecoder(fp)
|
|
if err = decoder.Decode(data); err != nil {
|
|
return errors.Wrap(err, "rbd: decode error")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Delete deletes the metadata file from cache directory with identifier name
|
|
func (nc *NodeCache) Delete(identifier string) error {
|
|
file := path.Join(nc.BasePath, nc.CacheDir, identifier+".json")
|
|
err := os.Remove(file)
|
|
if err != nil {
|
|
if err == os.ErrNotExist {
|
|
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)
|
|
return nil
|
|
}
|