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"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2015-03-07 20:35:00 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/mount"
|
2015-03-19 05:18:31 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/volume"
|
2015-04-10 20:56:11 +00:00
|
|
|
"github.com/golang/glog"
|
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-05-04 14:43:10 +00:00
|
|
|
&emptyDirPlugin{nil, false},
|
|
|
|
&emptyDirPlugin{nil, true},
|
2015-03-07 21:38:50 +00:00
|
|
|
}
|
2014-11-23 15:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type emptyDirPlugin struct {
|
2015-03-19 05:18:31 +00:00
|
|
|
host volume.VolumeHost
|
2014-11-23 15:47:25 +00:00
|
|
|
legacyMode bool // if set, plugin answers to the legacy name
|
|
|
|
}
|
|
|
|
|
2015-03-19 05:18:31 +00:00
|
|
|
var _ volume.VolumePlugin = &emptyDirPlugin{}
|
2014-11-23 15:47:25 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
emptyDirPluginName = "kubernetes.io/empty-dir"
|
|
|
|
emptyDirPluginLegacyName = "empty"
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
if plugin.legacyMode {
|
|
|
|
return emptyDirPluginLegacyName
|
|
|
|
}
|
|
|
|
return emptyDirPluginName
|
|
|
|
}
|
|
|
|
|
2015-04-14 16:29:33 +00:00
|
|
|
func (plugin *emptyDirPlugin) CanSupport(spec *volume.Spec) bool {
|
2014-11-23 15:47:25 +00:00
|
|
|
if plugin.legacyMode {
|
|
|
|
// Legacy mode instances can be cleaned up but not created anew.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-04-14 16:29:33 +00:00
|
|
|
if spec.VolumeSource.EmptyDir != nil {
|
2014-11-23 15:47:25 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-05-04 14:43:10 +00:00
|
|
|
func (plugin *emptyDirPlugin) NewBuilder(spec *volume.Spec, podRef *api.ObjectReference, opts volume.VolumeOptions, mounter mount.Interface) (volume.Builder, error) {
|
|
|
|
return plugin.newBuilderInternal(spec, podRef, mounter, &realMountDetector{mounter}, opts)
|
2015-03-07 20:35:00 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 16:29:33 +00:00
|
|
|
func (plugin *emptyDirPlugin) newBuilderInternal(spec *volume.Spec, podRef *api.ObjectReference, mounter mount.Interface, mountDetector mountDetector, opts volume.VolumeOptions) (volume.Builder, error) {
|
2014-11-23 15:47:25 +00:00
|
|
|
if plugin.legacyMode {
|
|
|
|
// Legacy mode instances can be cleaned up but not created anew.
|
|
|
|
return nil, fmt.Errorf("legacy mode: can not create new instances")
|
|
|
|
}
|
2015-03-07 20:35:00 +00:00
|
|
|
medium := api.StorageTypeDefault
|
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-03-16 23:17:47 +00:00
|
|
|
podUID: podRef.UID,
|
|
|
|
volName: spec.Name,
|
|
|
|
medium: medium,
|
|
|
|
mounter: mounter,
|
|
|
|
mountDetector: mountDetector,
|
|
|
|
plugin: plugin,
|
|
|
|
legacyMode: false,
|
2015-04-10 20:56:11 +00:00
|
|
|
rootContext: opts.RootContext,
|
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) {
|
2014-11-23 15:47:25 +00:00
|
|
|
legacy := false
|
|
|
|
if plugin.legacyMode {
|
|
|
|
legacy = true
|
|
|
|
}
|
2015-03-07 20:35:00 +00:00
|
|
|
ed := &emptyDir{
|
2015-03-16 23:17:47 +00:00
|
|
|
podUID: podUID,
|
|
|
|
volName: volName,
|
|
|
|
medium: api.StorageTypeDefault, // might be changed later
|
|
|
|
mounter: mounter,
|
|
|
|
mountDetector: mountDetector,
|
|
|
|
plugin: plugin,
|
|
|
|
legacyMode: legacy,
|
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-03-16 23:17:47 +00:00
|
|
|
podUID types.UID
|
|
|
|
volName string
|
|
|
|
medium api.StorageType
|
|
|
|
mounter mount.Interface
|
|
|
|
mountDetector mountDetector
|
|
|
|
plugin *emptyDirPlugin
|
|
|
|
legacyMode bool
|
2015-04-10 20:56:11 +00:00
|
|
|
rootContext string
|
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 {
|
2014-11-23 15:47:25 +00:00
|
|
|
if ed.legacyMode {
|
|
|
|
return fmt.Errorf("legacy mode: can not create new instances")
|
|
|
|
}
|
2015-03-07 20:35:00 +00:00
|
|
|
switch ed.medium {
|
|
|
|
case api.StorageTypeDefault:
|
2015-03-07 21:38:50 +00:00
|
|
|
return ed.setupDefault(dir)
|
2015-03-07 20:35:00 +00:00
|
|
|
case api.StorageTypeMemory:
|
2015-03-07 21:38:50 +00:00
|
|
|
return ed.setupTmpfs(dir)
|
2015-03-07 20:35:00 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown storage medium %q", ed.medium)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-07 21:38:50 +00:00
|
|
|
func (ed *emptyDir) setupDefault(dir string) error {
|
|
|
|
return os.MkdirAll(dir, 0750)
|
2015-03-07 20:35:00 +00:00
|
|
|
}
|
|
|
|
|
2015-03-07 21:38:50 +00:00
|
|
|
func (ed *emptyDir) setupTmpfs(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-03-07 21:38:50 +00:00
|
|
|
if err := os.MkdirAll(dir, 0750); 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
|
|
|
|
// from that of the Kubelet root directory which is not readable from
|
|
|
|
// the SELinux context of a docker container.
|
|
|
|
//
|
|
|
|
// getTmpfsMountOptions gets the mount option to set the context of
|
|
|
|
// the tmpfs mount so that it can be read from the SELinux context of
|
|
|
|
// the container.
|
|
|
|
opts := ed.getTmpfsMountOptions()
|
|
|
|
glog.V(3).Infof("pod %v: mounting tmpfs for volume %v with opts %v", ed.podUID, 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-04-03 01:08:04 +00:00
|
|
|
func (ed *emptyDir) getTmpfsMountOptions() []string {
|
2015-04-10 20:56:11 +00:00
|
|
|
if ed.rootContext == "" {
|
2015-04-03 01:08:04 +00:00
|
|
|
return []string{""}
|
2015-04-10 20:56:11 +00:00
|
|
|
}
|
|
|
|
|
2015-04-03 01:08:04 +00:00
|
|
|
return []string{fmt.Sprintf("rootcontext=\"%v\"", ed.rootContext)}
|
2014-11-23 15:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ed *emptyDir) GetPath() string {
|
|
|
|
name := emptyDirPluginName
|
|
|
|
if ed.legacyMode {
|
|
|
|
name = emptyDirPluginLegacyName
|
|
|
|
}
|
2015-03-24 14:39:51 +00:00
|
|
|
return ed.plugin.host.GetPodVolumeDir(ed.podUID, 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 {
|
|
|
|
ed.medium = api.StorageTypeMemory
|
|
|
|
return ed.teardownTmpfs(dir)
|
|
|
|
}
|
|
|
|
// assume StorageTypeDefault
|
|
|
|
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
|
|
|
|
}
|