2014-07-15 01:39:30 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2014-07-16 19:30:51 +00:00
|
|
|
package volume
|
2014-07-15 01:39:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2014-07-16 19:30:51 +00:00
|
|
|
"os"
|
2014-07-19 00:13:34 +00:00
|
|
|
"path"
|
|
|
|
|
2014-07-15 01:39:30 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
// All volume types are expected to implement this interface
|
|
|
|
type Interface interface {
|
|
|
|
// Prepares and mounts/unpacks the volume to a directory path.
|
2014-07-16 19:32:59 +00:00
|
|
|
// This procedure must be idempotent.
|
2014-07-19 00:13:34 +00:00
|
|
|
// TODO(jonesdl) SetUp should return an error if it fails.
|
2014-07-24 20:45:55 +00:00
|
|
|
SetUp() error
|
2014-07-15 01:39:30 +00:00
|
|
|
// Returns the directory path the volume is mounted to.
|
|
|
|
GetPath() string
|
|
|
|
// Unmounts the volume and removes traces of the SetUp procedure.
|
2014-07-16 19:32:59 +00:00
|
|
|
// This procedure must be idempotent.
|
2014-07-24 20:45:55 +00:00
|
|
|
TearDown() error
|
2014-07-15 01:39:30 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 19:30:51 +00:00
|
|
|
// Host Directory volumes represent a bare host directory mount.
|
|
|
|
// The directory in Path will be directly exposed to the container.
|
|
|
|
type HostDirectory struct {
|
2014-07-15 01:39:30 +00:00
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
2014-07-16 19:30:51 +00:00
|
|
|
// Host directory mounts require no setup or cleanup, but still
|
2014-07-15 01:39:30 +00:00
|
|
|
// need to fulfill the interface definitions.
|
2014-07-24 20:45:55 +00:00
|
|
|
func (hostVol *HostDirectory) SetUp() error {
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-15 01:39:30 +00:00
|
|
|
|
2014-07-24 20:45:55 +00:00
|
|
|
func (hostVol *HostDirectory) TearDown() error {
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-15 01:39:30 +00:00
|
|
|
|
2014-07-16 19:30:51 +00:00
|
|
|
func (hostVol *HostDirectory) GetPath() string {
|
2014-07-15 01:39:30 +00:00
|
|
|
return hostVol.Path
|
|
|
|
}
|
|
|
|
|
2014-07-16 19:30:51 +00:00
|
|
|
// EmptyDirectory volumes are temporary directories exposed to the pod.
|
|
|
|
// These do not persist beyond the lifetime of a pod.
|
|
|
|
type EmptyDirectory struct {
|
2014-07-19 00:13:34 +00:00
|
|
|
Name string
|
|
|
|
PodID string
|
|
|
|
RootDir string
|
2014-07-16 19:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetUp creates the new directory.
|
2014-07-24 20:45:55 +00:00
|
|
|
func (emptyDir *EmptyDirectory) SetUp() error {
|
2014-07-19 00:13:34 +00:00
|
|
|
path := emptyDir.GetPath()
|
2014-07-24 20:45:55 +00:00
|
|
|
err := os.MkdirAll(path, 0750)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-07-16 19:32:59 +00:00
|
|
|
}
|
2014-07-24 20:45:55 +00:00
|
|
|
return nil
|
2014-07-16 19:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(jonesdl) when we can properly invoke TearDown(), we should delete
|
|
|
|
// the directory created by SetUp.
|
2014-07-24 20:45:55 +00:00
|
|
|
func (emptyDir *EmptyDirectory) TearDown() error {
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-16 19:30:51 +00:00
|
|
|
|
|
|
|
func (emptyDir *EmptyDirectory) GetPath() string {
|
2014-07-19 00:13:34 +00:00
|
|
|
return path.Join(emptyDir.RootDir, emptyDir.PodID, "volumes", "empty", emptyDir.Name)
|
2014-07-16 19:30:51 +00:00
|
|
|
}
|
2014-07-16 19:32:59 +00:00
|
|
|
|
2014-07-15 01:39:30 +00:00
|
|
|
// Interprets API volume as a HostDirectory
|
2014-07-16 19:30:51 +00:00
|
|
|
func createHostDirectory(volume *api.Volume) *HostDirectory {
|
|
|
|
return &HostDirectory{volume.Source.HostDirectory.Path}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interprets API volume as an EmptyDirectory
|
2014-07-19 00:13:34 +00:00
|
|
|
func createEmptyDirectory(volume *api.Volume, podID string, rootDir string) *EmptyDirectory {
|
|
|
|
return &EmptyDirectory{volume.Name, podID, rootDir}
|
2014-07-15 01:39:30 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 19:30:51 +00:00
|
|
|
// CreateVolume returns an Interface capable of mounting a volume described by an
|
2014-07-16 19:32:59 +00:00
|
|
|
// *api.Volume and whether or not it is mounted, or an error.
|
2014-07-19 00:13:34 +00:00
|
|
|
func CreateVolume(volume *api.Volume, podID string, rootDir string) (Interface, error) {
|
2014-07-16 19:32:59 +00:00
|
|
|
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
|
2014-07-16 19:30:51 +00:00
|
|
|
// TODO(jonesdl) We should probably not check every pointer and directly
|
|
|
|
// resolve these types instead.
|
|
|
|
if source.HostDirectory != nil {
|
2014-07-16 19:32:59 +00:00
|
|
|
vol = createHostDirectory(volume)
|
2014-07-16 19:30:51 +00:00
|
|
|
} else if source.EmptyDirectory != nil {
|
2014-07-19 00:13:34 +00:00
|
|
|
vol = createEmptyDirectory(volume, podID, rootDir)
|
2014-07-15 01:39:30 +00:00
|
|
|
} else {
|
|
|
|
return nil, errors.New("Unsupported volume type.")
|
|
|
|
}
|
2014-07-16 19:32:59 +00:00
|
|
|
return vol, nil
|
2014-07-15 01:39:30 +00:00
|
|
|
}
|