2014-11-23 15:47:25 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-11-23 15:47:25 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package empty_dir
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-07-07 16:40:55 +00:00
|
|
|
"path"
|
2014-11-23 15:47:25 +00:00
|
|
|
|
2015-08-05 22:05:17 +00:00
|
|
|
"github.com/golang/glog"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/types"
|
|
|
|
"k8s.io/kubernetes/pkg/util"
|
|
|
|
"k8s.io/kubernetes/pkg/util/mount"
|
|
|
|
"k8s.io/kubernetes/pkg/volume"
|
|
|
|
volumeutil "k8s.io/kubernetes/pkg/volume/util"
|
2014-11-23 15:47:25 +00:00
|
|
|
)
|
|
|
|
|
2015-07-07 16:40:55 +00:00
|
|
|
// TODO: in the near future, this will be changed to be more restrictive
|
|
|
|
// and the group will be set to allow containers to use emptyDir volumes
|
|
|
|
// from the group attribute.
|
|
|
|
//
|
|
|
|
// https://github.com/GoogleCloudPlatform/kubernetes/issues/2630
|
|
|
|
const perm os.FileMode = 0777
|
|
|
|
|
2014-11-23 15:47:25 +00:00
|
|
|
// This is the primary entrypoint for volume plugins.
|
2015-03-19 05:18:31 +00:00
|
|
|
func ProbeVolumePlugins() []volume.VolumePlugin {
|
|
|
|
return []volume.VolumePlugin{
|
2015-07-22 18:50:11 +00:00
|
|
|
&emptyDirPlugin{nil},
|
2015-03-07 21:38:50 +00:00
|
|
|
}
|
2014-11-23 15:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type emptyDirPlugin struct {
|
2015-07-22 18:50:11 +00:00
|
|
|
host volume.VolumeHost
|
2014-11-23 15:47:25 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 05:18:31 +00:00
|
|
|
var _ volume.VolumePlugin = &emptyDirPlugin{}
|
2014-11-23 15:47:25 +00:00
|
|
|
|
|
|
|
const (
|
2015-07-22 18:50:11 +00:00
|
|
|
emptyDirPluginName = "kubernetes.io/empty-dir"
|
2014-11-23 15:47:25 +00:00
|
|
|
)
|
|
|
|
|
2015-03-19 05:18:31 +00:00
|
|
|
func (plugin *emptyDirPlugin) Init(host volume.VolumeHost) {
|
2014-11-23 15:47:25 +00:00
|
|
|
plugin.host = host
|
|
|
|
}
|
|
|
|
|
|
|
|
func (plugin *emptyDirPlugin) Name() string {
|
|
|
|
return emptyDirPluginName
|
|
|
|
}
|
|
|
|
|
2015-04-14 16:29:33 +00:00
|
|
|
func (plugin *emptyDirPlugin) CanSupport(spec *volume.Spec) bool {
|
|
|
|
if spec.VolumeSource.EmptyDir != nil {
|
2014-11-23 15:47:25 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-05-11 00:12:57 +00:00
|
|
|
func (plugin *emptyDirPlugin) NewBuilder(spec *volume.Spec, pod *api.Pod, opts volume.VolumeOptions, mounter mount.Interface) (volume.Builder, error) {
|
2015-07-07 16:40:55 +00:00
|
|
|
return plugin.newBuilderInternal(spec, pod, mounter, &realMountDetector{mounter}, opts, newChconRunner())
|
2015-03-07 20:35:00 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 16:40:55 +00:00
|
|
|
func (plugin *emptyDirPlugin) newBuilderInternal(spec *volume.Spec, pod *api.Pod, mounter mount.Interface, mountDetector mountDetector, opts volume.VolumeOptions, chconRunner chconRunner) (volume.Builder, error) {
|
2015-05-18 20:26:09 +00:00
|
|
|
medium := api.StorageMediumDefault
|
2015-04-14 16:29:33 +00:00
|
|
|
if spec.VolumeSource.EmptyDir != nil { // Support a non-specified source as EmptyDir.
|
|
|
|
medium = spec.VolumeSource.EmptyDir.Medium
|
2015-03-07 20:35:00 +00:00
|
|
|
}
|
|
|
|
return &emptyDir{
|
2015-07-07 16:40:55 +00:00
|
|
|
pod: pod,
|
2015-03-16 23:17:47 +00:00
|
|
|
volName: spec.Name,
|
|
|
|
medium: medium,
|
|
|
|
mounter: mounter,
|
|
|
|
mountDetector: mountDetector,
|
|
|
|
plugin: plugin,
|
2015-04-10 20:56:11 +00:00
|
|
|
rootContext: opts.RootContext,
|
2015-07-07 16:40:55 +00:00
|
|
|
chconRunner: chconRunner,
|
2015-03-07 20:35:00 +00:00
|
|
|
}, nil
|
2014-11-23 15:47:25 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 14:43:10 +00:00
|
|
|
func (plugin *emptyDirPlugin) NewCleaner(volName string, podUID types.UID, mounter mount.Interface) (volume.Cleaner, error) {
|
2015-03-07 20:35:00 +00:00
|
|
|
// Inject real implementations here, test through the internal function.
|
2015-05-04 14:43:10 +00:00
|
|
|
return plugin.newCleanerInternal(volName, podUID, mounter, &realMountDetector{mounter})
|
2015-03-07 20:35:00 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 23:17:47 +00:00
|
|
|
func (plugin *emptyDirPlugin) newCleanerInternal(volName string, podUID types.UID, mounter mount.Interface, mountDetector mountDetector) (volume.Cleaner, error) {
|
2015-03-07 20:35:00 +00:00
|
|
|
ed := &emptyDir{
|
2015-07-07 16:40:55 +00:00
|
|
|
pod: &api.Pod{ObjectMeta: api.ObjectMeta{UID: podUID}},
|
2015-03-16 23:17:47 +00:00
|
|
|
volName: volName,
|
2015-05-18 20:26:09 +00:00
|
|
|
medium: api.StorageMediumDefault, // might be changed later
|
2015-03-16 23:17:47 +00:00
|
|
|
mounter: mounter,
|
|
|
|
mountDetector: mountDetector,
|
|
|
|
plugin: plugin,
|
2015-03-07 20:35:00 +00:00
|
|
|
}
|
|
|
|
return ed, nil
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:17:47 +00:00
|
|
|
// mountDetector abstracts how to find what kind of mount a path is backed by.
|
|
|
|
type mountDetector interface {
|
|
|
|
// GetMountMedium determines what type of medium a given path is backed
|
|
|
|
// by and whether that path is a mount point. For example, if this
|
|
|
|
// returns (mediumMemory, false, nil), the caller knows that the path is
|
|
|
|
// on a memory FS (tmpfs on Linux) but is not the root mountpoint of
|
|
|
|
// that tmpfs.
|
|
|
|
GetMountMedium(path string) (storageMedium, bool, error)
|
2014-11-23 15:47:25 +00:00
|
|
|
}
|
|
|
|
|
2015-03-07 20:35:00 +00:00
|
|
|
type storageMedium int
|
|
|
|
|
|
|
|
const (
|
|
|
|
mediumUnknown storageMedium = 0 // assume anything we don't explicitly handle is this
|
|
|
|
mediumMemory storageMedium = 1 // memory (e.g. tmpfs on linux)
|
|
|
|
)
|
|
|
|
|
2014-11-23 15:47:25 +00:00
|
|
|
// EmptyDir volumes are temporary directories exposed to the pod.
|
|
|
|
// These do not persist beyond the lifetime of a pod.
|
|
|
|
type emptyDir struct {
|
2015-07-07 16:40:55 +00:00
|
|
|
pod *api.Pod
|
2015-03-16 23:17:47 +00:00
|
|
|
volName string
|
2015-05-18 20:26:09 +00:00
|
|
|
medium api.StorageMedium
|
2015-03-16 23:17:47 +00:00
|
|
|
mounter mount.Interface
|
|
|
|
mountDetector mountDetector
|
|
|
|
plugin *emptyDirPlugin
|
2015-04-10 20:56:11 +00:00
|
|
|
rootContext string
|
2015-07-07 16:40:55 +00:00
|
|
|
chconRunner chconRunner
|
2014-11-23 15:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetUp creates new directory.
|
|
|
|
func (ed *emptyDir) SetUp() error {
|
2015-03-07 21:38:50 +00:00
|
|
|
return ed.SetUpAt(ed.GetPath())
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetUpAt creates new directory.
|
|
|
|
func (ed *emptyDir) SetUpAt(dir string) error {
|
2015-07-07 16:40:55 +00:00
|
|
|
isMnt, err := ed.mounter.IsMountPoint(dir)
|
|
|
|
// Getting an os.IsNotExist err from is a contingency; the directory
|
|
|
|
// may not exist yet, in which case, setup should run.
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the plugin readiness file is present for this volume, and the
|
|
|
|
// storage medium is the default, then the volume is ready. If the
|
|
|
|
// medium is memory, and a mountpoint is present, then the volume is
|
|
|
|
// ready.
|
|
|
|
if volumeutil.IsReady(ed.getMetaDir()) {
|
|
|
|
if ed.medium == api.StorageMediumMemory && isMnt {
|
|
|
|
return nil
|
|
|
|
} else if ed.medium == api.StorageMediumDefault {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the effective SELinuxOptions to use for this volume.
|
|
|
|
securityContext := ""
|
|
|
|
if selinuxEnabled() {
|
|
|
|
securityContext = ed.rootContext
|
|
|
|
}
|
|
|
|
|
2015-03-07 20:35:00 +00:00
|
|
|
switch ed.medium {
|
2015-05-18 20:26:09 +00:00
|
|
|
case api.StorageMediumDefault:
|
2015-07-07 16:40:55 +00:00
|
|
|
err = ed.setupDir(dir, securityContext)
|
2015-05-18 20:26:09 +00:00
|
|
|
case api.StorageMediumMemory:
|
2015-07-07 16:40:55 +00:00
|
|
|
err = ed.setupTmpfs(dir, securityContext)
|
2015-03-07 20:35:00 +00:00
|
|
|
default:
|
2015-07-07 16:40:55 +00:00
|
|
|
err = fmt.Errorf("unknown storage medium %q", ed.medium)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
volumeutil.SetReady(ed.getMetaDir())
|
2015-03-07 20:35:00 +00:00
|
|
|
}
|
2015-07-07 16:40:55 +00:00
|
|
|
|
|
|
|
return err
|
2015-03-07 20:35:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 16:54:43 +00:00
|
|
|
func (ed *emptyDir) IsReadOnly() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-07-07 16:40:55 +00:00
|
|
|
// setupTmpfs creates a tmpfs mount at the specified directory with the
|
|
|
|
// specified SELinux context.
|
|
|
|
func (ed *emptyDir) setupTmpfs(dir string, selinuxContext string) error {
|
2015-03-07 20:35:00 +00:00
|
|
|
if ed.mounter == nil {
|
|
|
|
return fmt.Errorf("memory storage requested, but mounter is nil")
|
|
|
|
}
|
2015-07-07 16:40:55 +00:00
|
|
|
if err := ed.setupDir(dir, selinuxContext); err != nil {
|
2015-03-07 20:35:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Make SetUp idempotent.
|
2015-03-16 23:17:47 +00:00
|
|
|
medium, isMnt, err := ed.mountDetector.GetMountMedium(dir)
|
|
|
|
if err != nil {
|
2015-03-07 20:35:00 +00:00
|
|
|
return err
|
2015-03-16 23:17:47 +00:00
|
|
|
}
|
2015-04-10 20:56:11 +00:00
|
|
|
// If the directory is a mountpoint with medium memory, there is no
|
|
|
|
// work to do since we are already in the desired state.
|
2015-03-16 23:17:47 +00:00
|
|
|
if isMnt && medium == mediumMemory {
|
2015-04-10 20:56:11 +00:00
|
|
|
return nil
|
2015-03-07 20:35:00 +00:00
|
|
|
}
|
2015-04-03 01:08:04 +00:00
|
|
|
|
2015-04-10 20:56:11 +00:00
|
|
|
// By default a tmpfs mount will receive a different SELinux context
|
2015-07-07 16:40:55 +00:00
|
|
|
// which is not readable from the SELinux context of a docker container.
|
|
|
|
var opts []string
|
|
|
|
if selinuxContext != "" {
|
|
|
|
opts = []string{fmt.Sprintf("rootcontext=\"%v\"", selinuxContext)}
|
|
|
|
} else {
|
|
|
|
opts = []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(3).Infof("pod %v: mounting tmpfs for volume %v with opts %v", ed.pod.UID, ed.volName, opts)
|
2015-04-03 01:08:04 +00:00
|
|
|
return ed.mounter.Mount("tmpfs", dir, "tmpfs", opts)
|
2015-04-10 20:56:11 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 16:40:55 +00:00
|
|
|
// setupDir creates the directory with the specified SELinux context and
|
|
|
|
// the default permissions specified by the perm constant.
|
|
|
|
func (ed *emptyDir) setupDir(dir, selinuxContext string) error {
|
|
|
|
// Create the directory if it doesn't already exist.
|
|
|
|
if err := os.MkdirAll(dir, perm); err != nil {
|
|
|
|
return err
|
2015-04-10 20:56:11 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 16:40:55 +00:00
|
|
|
// stat the directory to read permission bits
|
|
|
|
fileinfo, err := os.Lstat(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fileinfo.Mode().Perm() != perm.Perm() {
|
|
|
|
// If the permissions on the created directory are wrong, the
|
|
|
|
// kubelet is probably running with a umask set. In order to
|
|
|
|
// avoid clearing the umask for the entire process or locking
|
|
|
|
// the thread, clearing the umask, creating the dir, restoring
|
|
|
|
// the umask, and unlocking the thread, we do a chmod to set
|
|
|
|
// the specific bits we need.
|
|
|
|
err := os.Chmod(dir, perm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fileinfo, err = os.Lstat(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fileinfo.Mode().Perm() != perm.Perm() {
|
|
|
|
glog.Errorf("Expected directory %q permissions to be: %s; got: %s", dir, perm.Perm(), fileinfo.Mode().Perm())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the context on the directory, if appropriate
|
|
|
|
if selinuxContext != "" {
|
|
|
|
glog.V(3).Infof("Setting SELinux context for %v to %v", dir, selinuxContext)
|
|
|
|
return ed.chconRunner.SetContext(dir, selinuxContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-11-23 15:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ed *emptyDir) GetPath() string {
|
|
|
|
name := emptyDirPluginName
|
2015-07-07 16:40:55 +00:00
|
|
|
return ed.plugin.host.GetPodVolumeDir(ed.pod.UID, util.EscapeQualifiedNameForDisk(name), ed.volName)
|
2014-11-23 15:47:25 +00:00
|
|
|
}
|
|
|
|
|
2015-03-07 20:35:00 +00:00
|
|
|
// TearDown simply discards everything in the directory.
|
2014-11-23 15:47:25 +00:00
|
|
|
func (ed *emptyDir) TearDown() error {
|
2015-03-07 21:38:50 +00:00
|
|
|
return ed.TearDownAt(ed.GetPath())
|
|
|
|
}
|
|
|
|
|
|
|
|
// TearDownAt simply discards everything in the directory.
|
|
|
|
func (ed *emptyDir) TearDownAt(dir string) error {
|
|
|
|
// Figure out the medium.
|
2015-03-16 23:17:47 +00:00
|
|
|
medium, isMnt, err := ed.mountDetector.GetMountMedium(dir)
|
|
|
|
if err != nil {
|
2015-03-07 21:38:50 +00:00
|
|
|
return err
|
2015-03-07 20:35:00 +00:00
|
|
|
}
|
2015-03-16 23:17:47 +00:00
|
|
|
if isMnt && medium == mediumMemory {
|
2015-05-18 20:26:09 +00:00
|
|
|
ed.medium = api.StorageMediumMemory
|
2015-03-16 23:17:47 +00:00
|
|
|
return ed.teardownTmpfs(dir)
|
|
|
|
}
|
2015-05-18 20:26:09 +00:00
|
|
|
// assume StorageMediumDefault
|
2015-03-16 23:17:47 +00:00
|
|
|
return ed.teardownDefault(dir)
|
2015-03-07 20:35:00 +00:00
|
|
|
}
|
|
|
|
|
2015-03-07 21:38:50 +00:00
|
|
|
func (ed *emptyDir) teardownDefault(dir string) error {
|
|
|
|
tmpDir, err := volume.RenameDirectory(dir, ed.volName+".deleting~")
|
2014-11-23 15:47:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.RemoveAll(tmpDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-03-07 20:35:00 +00:00
|
|
|
|
2015-03-07 21:38:50 +00:00
|
|
|
func (ed *emptyDir) teardownTmpfs(dir string) error {
|
2015-03-07 20:35:00 +00:00
|
|
|
if ed.mounter == nil {
|
|
|
|
return fmt.Errorf("memory storage requested, but mounter is nil")
|
|
|
|
}
|
2015-04-03 01:08:04 +00:00
|
|
|
if err := ed.mounter.Unmount(dir); err != nil {
|
2015-03-07 20:35:00 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-03-07 21:38:50 +00:00
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
2015-03-07 20:35:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-07-07 16:40:55 +00:00
|
|
|
|
|
|
|
func (ed *emptyDir) getMetaDir() string {
|
|
|
|
return path.Join(ed.plugin.host.GetPodPluginDir(ed.pod.UID, util.EscapeQualifiedNameForDisk(emptyDirPluginName)), ed.volName)
|
|
|
|
}
|