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,15 +57,19 @@ type FileStore struct {
path string 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 { func (fstore *FileStore) Write(key string, data []byte) error {
if err := validateKey(key); err != nil { if err := validateKey(key); err != nil {
return err return err
} }
if _, err := os.Stat(fstore.path); err != nil { if err := ensurePath(fstore.path); err != nil {
// if directory already exists, proceed return err
if err = os.MkdirAll(fstore.path, 0755); err != nil && !os.IsExist(err) {
return err
}
} }
tmpfile := filepath.Join(fstore.path, fmt.Sprintf("%s%s%s", tmpPrefix, key, tmpSuffix)) tmpfile := filepath.Join(fstore.path, fmt.Sprintf("%s%s%s", tmpPrefix, key, tmpSuffix))
if err := ioutil.WriteFile(tmpfile, data, 0644); err != nil { if err := ioutil.WriteFile(tmpfile, data, 0644); err != nil {
@ -113,6 +117,15 @@ func (fstore *FileStore) getCheckpointPath(key string) string {
return filepath.Join(fstore.path, key) 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 { func validateKey(key string) error {
if len(key) <= keyMaxLength && keyRegex.MatchString(key) { if len(key) <= keyMaxLength && keyRegex.MatchString(key) {
return nil return nil

View File

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

View File

@ -84,8 +84,12 @@ type PersistentCheckpointHandler struct {
store CheckpointStore store CheckpointStore
} }
func NewPersistentCheckpointHandler() CheckpointHandler { func NewPersistentCheckpointHandler() (CheckpointHandler, error) {
return &PersistentCheckpointHandler{store: &FileStore{path: filepath.Join(dockershimRootDir, sandboxCheckpointDir)}} 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 { 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, func NewDockerService(client dockertools.DockerInterface, seccompProfileRoot string, podSandboxImage string, streamingConfig *streaming.Config,
pluginSettings *NetworkPluginSettings, cgroupsName string, kubeCgroupDriver string, execHandler dockertools.ExecHandler) (DockerService, error) { pluginSettings *NetworkPluginSettings, cgroupsName string, kubeCgroupDriver string, execHandler dockertools.ExecHandler) (DockerService, error) {
c := dockertools.NewInstrumentedDockerInterface(client) c := dockertools.NewInstrumentedDockerInterface(client)
checkpointHandler, err := NewPersistentCheckpointHandler()
if err != nil {
return nil, err
}
ds := &dockerService{ ds := &dockerService{
seccompProfileRoot: seccompProfileRoot, seccompProfileRoot: seccompProfileRoot,
client: c, client: c,
@ -157,7 +161,7 @@ func NewDockerService(client dockertools.DockerInterface, seccompProfileRoot str
execHandler: execHandler, execHandler: execHandler,
}, },
containerManager: cm.NewContainerManager(cgroupsName, client), containerManager: cm.NewContainerManager(cgroupsName, client),
checkpointHandler: NewPersistentCheckpointHandler(), checkpointHandler: checkpointHandler,
} }
if streamingConfig != nil { if streamingConfig != nil {
var err error var err error