Merge pull request #41665 from freehan/cri-checkpoint-fix

Automatic merge from submit-queue (batch tested with PRs 41812, 41665, 40007, 41281, 41771)

initialize directory while creating checkpoint file store

fixes: #41616 
ref: https://github.com/kubernetes/kubernetes/issues/41225
This commit is contained in:
Kubernetes Submit Queue 2017-02-23 00:11:35 -08:00 committed by GitHub
commit 0d5a638d24
4 changed files with 31 additions and 9 deletions

View File

@ -57,16 +57,20 @@ type FileStore struct {
path string
}
func NewFileStore(path string) (CheckpointStore, error) {
if err := ensurePath(path); err != nil {
return nil, err
}
return &FileStore{path: path}, nil
}
func (fstore *FileStore) Write(key string, data []byte) error {
if err := validateKey(key); err != nil {
return err
}
if _, err := os.Stat(fstore.path); err != nil {
// if directory already exists, proceed
if err = os.MkdirAll(fstore.path, 0755); err != nil && !os.IsExist(err) {
if err := ensurePath(fstore.path); err != nil {
return err
}
}
tmpfile := filepath.Join(fstore.path, fmt.Sprintf("%s%s%s", tmpPrefix, key, tmpSuffix))
if err := ioutil.WriteFile(tmpfile, data, 0644); err != nil {
return err
@ -113,6 +117,15 @@ func (fstore *FileStore) getCheckpointPath(key string) string {
return filepath.Join(fstore.path, key)
}
// ensurePath creates input directory if it does not exist
func ensurePath(path string) error {
if _, err := os.Stat(path); err != nil {
// MkdirAll returns nil if directory already exists
return os.MkdirAll(path, 0755)
}
return nil
}
func validateKey(key string) error {
if len(key) <= keyMaxLength && keyRegex.MatchString(key) {
return nil

View File

@ -30,7 +30,8 @@ func TestFileStore(t *testing.T) {
path, err := ioutil.TempDir("", "FileStore")
assert.NoError(t, err)
defer cleanUpTestPath(t, path)
store := &FileStore{path: path}
store, err := NewFileStore(path)
assert.NoError(t, err)
Checkpoints := []struct {
key string

View File

@ -84,8 +84,12 @@ type PersistentCheckpointHandler struct {
store CheckpointStore
}
func NewPersistentCheckpointHandler() CheckpointHandler {
return &PersistentCheckpointHandler{store: &FileStore{path: filepath.Join(dockershimRootDir, sandboxCheckpointDir)}}
func NewPersistentCheckpointHandler() (CheckpointHandler, error) {
fstore, err := NewFileStore(filepath.Join(dockershimRootDir, sandboxCheckpointDir))
if err != nil {
return nil, err
}
return &PersistentCheckpointHandler{store: fstore}, nil
}
func (handler *PersistentCheckpointHandler) CreateCheckpoint(podSandboxID string, checkpoint *PodSandboxCheckpoint) error {

View File

@ -147,6 +147,10 @@ var internalLabelKeys []string = []string{containerTypeLabelKey, containerLogPat
func NewDockerService(client dockertools.DockerInterface, seccompProfileRoot string, podSandboxImage string, streamingConfig *streaming.Config,
pluginSettings *NetworkPluginSettings, cgroupsName string, kubeCgroupDriver string, execHandler dockertools.ExecHandler) (DockerService, error) {
c := dockertools.NewInstrumentedDockerInterface(client)
checkpointHandler, err := NewPersistentCheckpointHandler()
if err != nil {
return nil, err
}
ds := &dockerService{
seccompProfileRoot: seccompProfileRoot,
client: c,
@ -157,7 +161,7 @@ func NewDockerService(client dockertools.DockerInterface, seccompProfileRoot str
execHandler: execHandler,
},
containerManager: cm.NewContainerManager(cgroupsName, client),
checkpointHandler: NewPersistentCheckpointHandler(),
checkpointHandler: checkpointHandler,
}
if streamingConfig != nil {
var err error