Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initialize directory while creating checkpoint file store #41665

Merged
merged 1 commit into from
Feb 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 18 additions & 5 deletions pkg/kubelet/dockershim/checkpoint_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,19 @@ 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) {
return 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 {
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/kubelet/dockershim/checkpoint_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions pkg/kubelet/dockershim/docker_checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion pkg/kubelet/dockershim/docker_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down