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

@@ -14,6 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
// Package volumes includes internal representations of external volume types
// Package volume includes internal representations of external volume types
// as well as utility methods required to mount/unmount volumes to kubelets.
package volume

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
}

View File

@@ -26,12 +26,21 @@ func TestCreateVolumes(t *testing.T) {
volumes := []api.Volume{
{
Name: "host-dir",
HostDirectory: &api.HostDirectory{"/dir/path"},
Source: &api.VolumeSource{
HostDirectory: &api.HostDirectory{"/dir/path"},
},
},
{
Name: "empty-dir",
Source: &api.VolumeSource{
EmptyDirectory: &api.EmptyDirectory{},
},
},
}
expectedPaths := []string{"/dir/path"}
fakePodID := "my-id"
expectedPaths := []string{"/dir/path", "/exports/my-id/empty-dir"}
for i, volume := range volumes {
extVolume, _ := CreateVolume(&volume)
extVolume, _ := CreateVolume(&volume, fakePodID)
expectedPath := expectedPaths[i]
path := extVolume.GetPath()
if expectedPath != path {