API modified to use source; now supports EmptyDirectory

API is now modified to use a Source struct to handle multiple volumes.

Two volume types are supported now, HostDirectory and EmptyDirectory.
This commit is contained in:
Danny Jones
2014-07-16 12:32:59 -07:00
parent f1a7850454
commit bb2843498d
8 changed files with 104 additions and 41 deletions

View File

@@ -21,16 +21,19 @@ import (
"fmt"
"os"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/golang/glog"
)
// All volume types are expected to implement this interface
type Interface interface {
// Prepares and mounts/unpacks the volume to a directory path.
// This procedure must be idempotent.
SetUp()
// Returns the directory path the volume is mounted to.
GetPath() string
// Unmounts the volume and removes traces of the SetUp procedure.
// This procedure must be idempotent.
TearDown()
}
@@ -52,7 +55,6 @@ func (hostVol *HostDirectory) GetPath() string {
// EmptyDirectory volumes are temporary directories exposed to the pod.
// These do not persist beyond the lifetime of a pod.
type EmptyDirectory struct {
Name string
PodID string
@@ -60,7 +62,11 @@ type EmptyDirectory struct {
// SetUp creates the new directory.
func (emptyDir *EmptyDirectory) SetUp() {
os.MkdirAll(emptyDir.GetPath(), 0750)
if _, err := os.Stat(emptyDir.GetPath()); os.IsNotExist(err) {
os.MkdirAll(emptyDir.GetPath(), 0750)
} else {
glog.Warningf("Directory already exists: (%v)", emptyDir.GetPath())
}
}
// TODO(jonesdl) when we can properly invoke TearDown(), we should delete
@@ -72,6 +78,7 @@ func (emptyDir *EmptyDirectory) GetPath() string {
// directory for kubelet to write to. For now this will just be /exports
return fmt.Sprintf("/exports/%v/%v", emptyDir.PodID, emptyDir.Name)
}
// Interprets API volume as a HostDirectory
func createHostDirectory(volume *api.Volume) *HostDirectory {
return &HostDirectory{volume.Source.HostDirectory.Path}
@@ -83,16 +90,23 @@ func createEmptyDirectory(volume *api.Volume, podID string) *EmptyDirectory {
}
// CreateVolume returns an Interface capable of mounting a volume described by an
// *api.Volume, or an error.
// *api.Volume and whether or not it is mounted, or an error.
func CreateVolume(volume *api.Volume, podID string) (Interface, error) {
source := volume.Source
// TODO(jonesdl) We will want to throw an error here when we no longer
// support the default behavior.
if source == nil {
return nil, nil
}
var vol Interface
// TODO(jonesdl) We should probably not check every pointer and directly
// resolve these types instead.
source := volume.Source
if source.HostDirectory != nil {
return createHostDirectory(volume), nil
vol = createHostDirectory(volume)
} else if source.EmptyDirectory != nil {
return createEmptyDirectory(volume, podID), nil
vol = createEmptyDirectory(volume, podID)
} else {
return nil, errors.New("Unsupported volume type.")
}
return vol, nil
}