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 volume
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
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"
|
2015-10-12 18:27:49 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/resource"
|
2016-02-05 21:58:03 +00:00
|
|
|
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
2015-04-10 16:54:01 +00:00
|
|
|
"k8s.io/kubernetes/pkg/cloudprovider"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/types"
|
2015-10-14 05:18:37 +00:00
|
|
|
utilerrors "k8s.io/kubernetes/pkg/util/errors"
|
2015-09-14 09:51:40 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/io"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/mount"
|
2015-09-10 22:48:28 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/validation"
|
2014-11-23 15:47:25 +00:00
|
|
|
)
|
|
|
|
|
2015-04-10 20:56:11 +00:00
|
|
|
// VolumeOptions contains option information about a volume.
|
|
|
|
type VolumeOptions struct {
|
|
|
|
// The rootcontext to use when performing mounts for a volume.
|
2015-09-07 19:55:28 +00:00
|
|
|
// This is a temporary measure in order to set the rootContext of tmpfs mounts correctly.
|
|
|
|
// it will be replaced and expanded on by future SecurityContext work.
|
2015-04-10 20:56:11 +00:00
|
|
|
RootContext string
|
2015-09-18 13:15:48 +00:00
|
|
|
|
2015-10-12 18:27:49 +00:00
|
|
|
// The attributes below are required by volume.Provisioner
|
|
|
|
// TODO: refactor all of this out of volumes when an admin can configure many kinds of provisioners.
|
2015-09-18 13:15:48 +00:00
|
|
|
|
2015-10-12 18:27:49 +00:00
|
|
|
// Capacity is the size of a volume.
|
|
|
|
Capacity resource.Quantity
|
2015-09-18 13:15:48 +00:00
|
|
|
// AccessModes of a volume
|
|
|
|
AccessModes []api.PersistentVolumeAccessMode
|
|
|
|
// Reclamation policy for a persistent volume
|
|
|
|
PersistentVolumeReclaimPolicy api.PersistentVolumeReclaimPolicy
|
2016-02-12 08:46:59 +00:00
|
|
|
// PV.Name of the appropriate PersistentVolume. Used to generate cloud volume name.
|
|
|
|
PVName string
|
|
|
|
// Unique name of Kubernetes cluster.
|
|
|
|
ClusterName string
|
2015-12-15 09:22:49 +00:00
|
|
|
// Tags to attach to the real volume in the cloud provider - e.g. AWS EBS
|
|
|
|
CloudTags *map[string]string
|
2015-04-10 20:56:11 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 05:18:31 +00:00
|
|
|
// VolumePlugin is an interface to volume plugins that can be used on a
|
|
|
|
// kubernetes node (e.g. by kubelet) to instantiate and manage volumes.
|
|
|
|
type VolumePlugin interface {
|
2014-11-23 15:47:25 +00:00
|
|
|
// Init initializes the plugin. This will be called exactly once
|
|
|
|
// before any New* calls are made - implementations of plugins may
|
|
|
|
// depend on this.
|
2015-09-30 18:31:53 +00:00
|
|
|
Init(host VolumeHost) error
|
2014-11-23 15:47:25 +00:00
|
|
|
|
|
|
|
// Name returns the plugin's name. Plugins should use namespaced names
|
|
|
|
// such as "example.com/volume". The "kubernetes.io" namespace is
|
|
|
|
// reserved for plugins which are bundled with kubernetes.
|
|
|
|
Name() string
|
|
|
|
|
2015-03-19 05:18:31 +00:00
|
|
|
// CanSupport tests whether the plugin supports a given volume
|
2014-11-23 15:47:25 +00:00
|
|
|
// specification from the API. The spec pointer should be considered
|
|
|
|
// const.
|
2015-04-14 16:29:33 +00:00
|
|
|
CanSupport(spec *Spec) bool
|
2014-11-23 15:47:25 +00:00
|
|
|
|
|
|
|
// NewBuilder creates a new volume.Builder from an API specification.
|
|
|
|
// Ownership of the spec pointer in *not* transferred.
|
|
|
|
// - spec: The api.Volume spec
|
2015-05-11 00:12:57 +00:00
|
|
|
// - pod: The enclosing pod
|
2015-09-14 09:51:40 +00:00
|
|
|
NewBuilder(spec *Spec, podRef *api.Pod, opts VolumeOptions) (Builder, error)
|
2014-11-23 15:47:25 +00:00
|
|
|
|
|
|
|
// NewCleaner creates a new volume.Cleaner from recoverable state.
|
|
|
|
// - name: The volume name, as per the api.Volume spec.
|
|
|
|
// - podUID: The UID of the enclosing pod
|
2015-09-14 09:51:40 +00:00
|
|
|
NewCleaner(name string, podUID types.UID) (Cleaner, error)
|
2014-11-23 15:47:25 +00:00
|
|
|
}
|
|
|
|
|
2015-03-12 19:37:02 +00:00
|
|
|
// PersistentVolumePlugin is an extended interface of VolumePlugin and is used
|
|
|
|
// by volumes that want to provide long term persistence of data
|
|
|
|
type PersistentVolumePlugin interface {
|
|
|
|
VolumePlugin
|
|
|
|
// GetAccessModes describes the ways a given volume can be accessed/mounted.
|
2015-05-18 20:22:30 +00:00
|
|
|
GetAccessModes() []api.PersistentVolumeAccessMode
|
2015-03-12 19:37:02 +00:00
|
|
|
}
|
|
|
|
|
2015-05-29 20:32:44 +00:00
|
|
|
// RecyclableVolumePlugin is an extended interface of VolumePlugin and is used
|
|
|
|
// by persistent volumes that want to be recycled before being made available again to new claims
|
|
|
|
type RecyclableVolumePlugin interface {
|
|
|
|
VolumePlugin
|
|
|
|
// NewRecycler creates a new volume.Recycler which knows how to reclaim this resource
|
|
|
|
// after the volume's release from a PersistentVolumeClaim
|
|
|
|
NewRecycler(spec *Spec) (Recycler, error)
|
|
|
|
}
|
|
|
|
|
2015-09-07 16:11:37 +00:00
|
|
|
// DeletableVolumePlugin is an extended interface of VolumePlugin and is used by persistent volumes that want
|
|
|
|
// to be deleted from the cluster after their release from a PersistentVolumeClaim.
|
|
|
|
type DeletableVolumePlugin interface {
|
|
|
|
VolumePlugin
|
|
|
|
// NewDeleter creates a new volume.Deleter which knows how to delete this resource
|
|
|
|
// in accordance with the underlying storage provider after the volume's release from a claim
|
|
|
|
NewDeleter(spec *Spec) (Deleter, error)
|
|
|
|
}
|
|
|
|
|
2015-10-12 18:27:49 +00:00
|
|
|
// ProvisionableVolumePlugin is an extended interface of VolumePlugin and is used to create volumes for the cluster.
|
|
|
|
type ProvisionableVolumePlugin interface {
|
2015-09-07 19:55:28 +00:00
|
|
|
VolumePlugin
|
2015-10-12 18:27:49 +00:00
|
|
|
// NewProvisioner creates a new volume.Provisioner which knows how to create PersistentVolumes in accordance with
|
2015-09-07 19:55:28 +00:00
|
|
|
// the plugin's underlying storage provider
|
2015-10-12 18:27:49 +00:00
|
|
|
NewProvisioner(options VolumeOptions) (Provisioner, error)
|
2015-09-07 19:55:28 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 21:23:28 +00:00
|
|
|
// AttachableVolumePlugin is an extended interface of VolumePlugin and is used for volumes that require attachment
|
|
|
|
// to a node before mounting.
|
|
|
|
type AttachableVolumePlugin interface {
|
|
|
|
VolumePlugin
|
|
|
|
NewAttacher(spec *Spec) (Attacher, error)
|
|
|
|
NewDetacher(name string, podUID types.UID) (Detacher, error)
|
|
|
|
}
|
|
|
|
|
2015-03-19 05:18:31 +00:00
|
|
|
// VolumeHost is an interface that plugins can use to access the kubelet.
|
|
|
|
type VolumeHost interface {
|
2014-11-23 15:47:25 +00:00
|
|
|
// GetPluginDir returns the absolute path to a directory under which
|
|
|
|
// a given plugin may store data. This directory might not actually
|
|
|
|
// exist on disk yet. For plugin data that is per-pod, see
|
|
|
|
// GetPodPluginDir().
|
|
|
|
GetPluginDir(pluginName string) string
|
|
|
|
|
|
|
|
// GetPodVolumeDir returns the absolute path a directory which
|
|
|
|
// represents the named volume under the named plugin for the given
|
|
|
|
// pod. If the specified pod does not exist, the result of this call
|
|
|
|
// might not exist.
|
|
|
|
GetPodVolumeDir(podUID types.UID, pluginName string, volumeName string) string
|
|
|
|
|
|
|
|
// GetPodPluginDir returns the absolute path to a directory under which
|
|
|
|
// a given plugin may store data for a given pod. If the specified pod
|
|
|
|
// does not exist, the result of this call might not exist. This
|
|
|
|
// directory might not actually exist on disk yet.
|
|
|
|
GetPodPluginDir(podUID types.UID, pluginName string) string
|
2015-02-18 01:26:41 +00:00
|
|
|
|
|
|
|
// GetKubeClient returns a client interface
|
2016-01-15 05:00:58 +00:00
|
|
|
GetKubeClient() clientset.Interface
|
2015-03-07 21:38:50 +00:00
|
|
|
|
|
|
|
// NewWrapperBuilder finds an appropriate plugin with which to handle
|
|
|
|
// the provided spec. This is used to implement volume plugins which
|
|
|
|
// "wrap" other plugins. For example, the "secret" volume is
|
|
|
|
// implemented in terms of the "emptyDir" volume.
|
2015-12-14 07:32:37 +00:00
|
|
|
NewWrapperBuilder(volName string, spec Spec, pod *api.Pod, opts VolumeOptions) (Builder, error)
|
2015-03-07 21:38:50 +00:00
|
|
|
|
|
|
|
// NewWrapperCleaner finds an appropriate plugin with which to handle
|
|
|
|
// the provided spec. See comments on NewWrapperBuilder for more
|
|
|
|
// context.
|
2015-12-14 07:32:37 +00:00
|
|
|
NewWrapperCleaner(volName string, spec Spec, podUID types.UID) (Cleaner, error)
|
2015-04-10 16:54:01 +00:00
|
|
|
|
2015-09-14 09:51:40 +00:00
|
|
|
// Get cloud provider from kubelet.
|
2015-04-10 16:54:01 +00:00
|
|
|
GetCloudProvider() cloudprovider.Interface
|
2015-09-14 09:51:40 +00:00
|
|
|
|
|
|
|
// Get mounter interface.
|
|
|
|
GetMounter() mount.Interface
|
|
|
|
|
|
|
|
// Get writer interface for writing data to disk.
|
|
|
|
GetWriter() io.Writer
|
2015-11-24 22:48:41 +00:00
|
|
|
|
|
|
|
// Returns the hostname of the host kubelet is running on
|
|
|
|
GetHostName() string
|
2014-11-23 15:47:25 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 05:18:31 +00:00
|
|
|
// VolumePluginMgr tracks registered plugins.
|
|
|
|
type VolumePluginMgr struct {
|
2014-11-23 15:47:25 +00:00
|
|
|
mutex sync.Mutex
|
2015-03-19 05:18:31 +00:00
|
|
|
plugins map[string]VolumePlugin
|
2014-11-23 15:47:25 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 16:29:33 +00:00
|
|
|
// Spec is an internal representation of a volume. All API volume types translate to Spec.
|
|
|
|
type Spec struct {
|
2015-08-12 19:11:03 +00:00
|
|
|
Volume *api.Volume
|
|
|
|
PersistentVolume *api.PersistentVolume
|
|
|
|
ReadOnly bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of either Volume or PersistentVolume, one of which must not be nil.
|
|
|
|
func (spec *Spec) Name() string {
|
|
|
|
switch {
|
|
|
|
case spec.Volume != nil:
|
|
|
|
return spec.Volume.Name
|
|
|
|
case spec.PersistentVolume != nil:
|
|
|
|
return spec.PersistentVolume.Name
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
2015-04-14 16:29:33 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 13:44:37 +00:00
|
|
|
// VolumeConfig is how volume plugins receive configuration. An instance specific to the plugin will be passed to
|
|
|
|
// the plugin's ProbeVolumePlugins(config) func. Reasonable defaults will be provided by the binary hosting
|
|
|
|
// the plugins while allowing override of those default values. Those config values are then set to an instance of
|
|
|
|
// VolumeConfig and passed to the plugin.
|
|
|
|
//
|
|
|
|
// Values in VolumeConfig are intended to be relevant to several plugins, but not necessarily all plugins. The
|
|
|
|
// preference is to leverage strong typing in this struct. All config items must have a descriptive but non-specific
|
|
|
|
// name (i.e, RecyclerMinimumTimeout is OK but RecyclerMinimumTimeoutForNFS is !OK). An instance of config will be
|
|
|
|
// given directly to the plugin, so config names specific to plugins are unneeded and wrongly expose plugins
|
|
|
|
// in this VolumeConfig struct.
|
|
|
|
//
|
|
|
|
// OtherAttributes is a map of string values intended for one-off configuration of a plugin or config that is only
|
|
|
|
// relevant to a single plugin. All values are passed by string and require interpretation by the plugin.
|
|
|
|
// Passing config as strings is the least desirable option but can be used for truly one-off configuration.
|
|
|
|
// The binary should still use strong typing for this value when binding CLI values before they are passed as strings
|
|
|
|
// in OtherAttributes.
|
|
|
|
type VolumeConfig struct {
|
2015-09-03 03:14:26 +00:00
|
|
|
// RecyclerPodTemplate is pod template that understands how to scrub clean a persistent volume after its release.
|
|
|
|
// The template is used by plugins which override specific properties of the pod in accordance with that plugin.
|
|
|
|
// See NewPersistentVolumeRecyclerPodTemplate for the properties that are expected to be overridden.
|
|
|
|
RecyclerPodTemplate *api.Pod
|
|
|
|
|
|
|
|
// RecyclerMinimumTimeout is the minimum amount of time in seconds for the recycler pod's ActiveDeadlineSeconds attribute.
|
|
|
|
// Added to the minimum timeout is the increment per Gi of capacity.
|
|
|
|
RecyclerMinimumTimeout int
|
|
|
|
|
|
|
|
// RecyclerTimeoutIncrement is the number of seconds added to the recycler pod's ActiveDeadlineSeconds for each
|
|
|
|
// Gi of capacity in the persistent volume.
|
|
|
|
// Example: 5Gi volume x 30s increment = 150s + 30s minimum = 180s ActiveDeadlineSeconds for recycler pod
|
|
|
|
RecyclerTimeoutIncrement int
|
|
|
|
|
|
|
|
// OtherAttributes stores config as strings. These strings are opaque to the system and only understood by the binary
|
|
|
|
// hosting the plugin and the plugin itself.
|
2015-08-31 13:44:37 +00:00
|
|
|
OtherAttributes map[string]string
|
|
|
|
}
|
|
|
|
|
2015-04-14 16:29:33 +00:00
|
|
|
// NewSpecFromVolume creates an Spec from an api.Volume
|
|
|
|
func NewSpecFromVolume(vs *api.Volume) *Spec {
|
|
|
|
return &Spec{
|
2015-08-12 19:11:03 +00:00
|
|
|
Volume: vs,
|
2015-04-14 16:29:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSpecFromPersistentVolume creates an Spec from an api.PersistentVolume
|
2015-06-26 20:37:11 +00:00
|
|
|
func NewSpecFromPersistentVolume(pv *api.PersistentVolume, readOnly bool) *Spec {
|
2015-04-14 16:29:33 +00:00
|
|
|
return &Spec{
|
2015-08-12 19:11:03 +00:00
|
|
|
PersistentVolume: pv,
|
|
|
|
ReadOnly: readOnly,
|
2015-04-14 16:29:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-23 15:47:25 +00:00
|
|
|
// InitPlugins initializes each plugin. All plugins must have unique names.
|
|
|
|
// This must be called exactly once before any New* methods are called on any
|
|
|
|
// plugins.
|
2015-03-19 05:18:31 +00:00
|
|
|
func (pm *VolumePluginMgr) InitPlugins(plugins []VolumePlugin, host VolumeHost) error {
|
2014-11-23 15:47:25 +00:00
|
|
|
pm.mutex.Lock()
|
|
|
|
defer pm.mutex.Unlock()
|
|
|
|
|
|
|
|
if pm.plugins == nil {
|
2015-03-19 05:18:31 +00:00
|
|
|
pm.plugins = map[string]VolumePlugin{}
|
2014-11-23 15:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
allErrs := []error{}
|
|
|
|
for _, plugin := range plugins {
|
|
|
|
name := plugin.Name()
|
2015-09-10 22:48:28 +00:00
|
|
|
if !validation.IsQualifiedName(name) {
|
2014-11-23 15:47:25 +00:00
|
|
|
allErrs = append(allErrs, fmt.Errorf("volume plugin has invalid name: %#v", plugin))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, found := pm.plugins[name]; found {
|
|
|
|
allErrs = append(allErrs, fmt.Errorf("volume plugin %q was registered more than once", name))
|
|
|
|
continue
|
|
|
|
}
|
2015-09-30 18:31:53 +00:00
|
|
|
err := plugin.Init(host)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Failed to load volume plugin %s, error: %s", plugin, err.Error())
|
|
|
|
allErrs = append(allErrs, err)
|
|
|
|
continue
|
|
|
|
}
|
2014-11-23 15:47:25 +00:00
|
|
|
pm.plugins[name] = plugin
|
|
|
|
glog.V(1).Infof("Loaded volume plugin %q", name)
|
|
|
|
}
|
2015-10-14 05:18:37 +00:00
|
|
|
return utilerrors.NewAggregate(allErrs)
|
2014-11-23 15:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FindPluginBySpec looks for a plugin that can support a given volume
|
|
|
|
// specification. If no plugins can support or more than one plugin can
|
|
|
|
// support it, return error.
|
2015-04-14 16:29:33 +00:00
|
|
|
func (pm *VolumePluginMgr) FindPluginBySpec(spec *Spec) (VolumePlugin, error) {
|
2014-11-23 15:47:25 +00:00
|
|
|
pm.mutex.Lock()
|
|
|
|
defer pm.mutex.Unlock()
|
|
|
|
|
|
|
|
matches := []string{}
|
|
|
|
for k, v := range pm.plugins {
|
|
|
|
if v.CanSupport(spec) {
|
|
|
|
matches = append(matches, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(matches) == 0 {
|
|
|
|
return nil, fmt.Errorf("no volume plugin matched")
|
|
|
|
}
|
|
|
|
if len(matches) > 1 {
|
|
|
|
return nil, fmt.Errorf("multiple volume plugins matched: %s", strings.Join(matches, ","))
|
|
|
|
}
|
|
|
|
return pm.plugins[matches[0]], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindPluginByName fetches a plugin by name or by legacy name. If no plugin
|
|
|
|
// is found, returns error.
|
2015-03-19 05:18:31 +00:00
|
|
|
func (pm *VolumePluginMgr) FindPluginByName(name string) (VolumePlugin, error) {
|
2014-11-23 15:47:25 +00:00
|
|
|
pm.mutex.Lock()
|
|
|
|
defer pm.mutex.Unlock()
|
|
|
|
|
|
|
|
// Once we can get rid of legacy names we can reduce this to a map lookup.
|
|
|
|
matches := []string{}
|
|
|
|
for k, v := range pm.plugins {
|
|
|
|
if v.Name() == name {
|
|
|
|
matches = append(matches, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(matches) == 0 {
|
|
|
|
return nil, fmt.Errorf("no volume plugin matched")
|
|
|
|
}
|
|
|
|
if len(matches) > 1 {
|
|
|
|
return nil, fmt.Errorf("multiple volume plugins matched: %s", strings.Join(matches, ","))
|
|
|
|
}
|
|
|
|
return pm.plugins[matches[0]], nil
|
|
|
|
}
|
2015-03-12 19:37:02 +00:00
|
|
|
|
2015-05-29 20:32:44 +00:00
|
|
|
// FindPersistentPluginBySpec looks for a persistent volume plugin that can support a given volume
|
|
|
|
// specification. If no plugin is found, return an error
|
|
|
|
func (pm *VolumePluginMgr) FindPersistentPluginBySpec(spec *Spec) (PersistentVolumePlugin, error) {
|
|
|
|
volumePlugin, err := pm.FindPluginBySpec(spec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Could not find volume plugin for spec: %+v", spec)
|
|
|
|
}
|
|
|
|
if persistentVolumePlugin, ok := volumePlugin.(PersistentVolumePlugin); ok {
|
|
|
|
return persistentVolumePlugin, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("no persistent volume plugin matched")
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindPersistentPluginByName fetches a persistent volume plugin by name. If no plugin
|
2015-03-12 19:37:02 +00:00
|
|
|
// is found, returns error.
|
|
|
|
func (pm *VolumePluginMgr) FindPersistentPluginByName(name string) (PersistentVolumePlugin, error) {
|
|
|
|
volumePlugin, err := pm.FindPluginByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-24 21:33:31 +00:00
|
|
|
if persistentVolumePlugin, ok := volumePlugin.(PersistentVolumePlugin); ok {
|
|
|
|
return persistentVolumePlugin, nil
|
|
|
|
}
|
2015-08-08 01:52:23 +00:00
|
|
|
return nil, fmt.Errorf("no persistent volume plugin matched")
|
2015-05-29 20:32:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FindRecyclablePluginByName fetches a persistent volume plugin by name. If no plugin
|
|
|
|
// is found, returns error.
|
|
|
|
func (pm *VolumePluginMgr) FindRecyclablePluginBySpec(spec *Spec) (RecyclableVolumePlugin, error) {
|
|
|
|
volumePlugin, err := pm.FindPluginBySpec(spec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if recyclableVolumePlugin, ok := volumePlugin.(RecyclableVolumePlugin); ok {
|
|
|
|
return recyclableVolumePlugin, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("no recyclable volume plugin matched")
|
2015-03-12 19:37:02 +00:00
|
|
|
}
|
2015-09-03 03:14:26 +00:00
|
|
|
|
2015-09-07 16:11:37 +00:00
|
|
|
// FindDeletablePluginByName fetches a persistent volume plugin by name. If no plugin
|
|
|
|
// is found, returns error.
|
|
|
|
func (pm *VolumePluginMgr) FindDeletablePluginBySpec(spec *Spec) (DeletableVolumePlugin, error) {
|
|
|
|
volumePlugin, err := pm.FindPluginBySpec(spec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if deletableVolumePlugin, ok := volumePlugin.(DeletableVolumePlugin); ok {
|
|
|
|
return deletableVolumePlugin, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("no deletable volume plugin matched")
|
|
|
|
}
|
|
|
|
|
2015-09-07 19:55:28 +00:00
|
|
|
// FindCreatablePluginBySpec fetches a persistent volume plugin by name. If no plugin
|
|
|
|
// is found, returns error.
|
2015-10-12 18:27:49 +00:00
|
|
|
func (pm *VolumePluginMgr) FindCreatablePluginBySpec(spec *Spec) (ProvisionableVolumePlugin, error) {
|
2015-09-07 19:55:28 +00:00
|
|
|
volumePlugin, err := pm.FindPluginBySpec(spec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-12 18:27:49 +00:00
|
|
|
if provisionableVolumePlugin, ok := volumePlugin.(ProvisionableVolumePlugin); ok {
|
|
|
|
return provisionableVolumePlugin, nil
|
2015-09-07 19:55:28 +00:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("no creatable volume plugin matched")
|
|
|
|
}
|
|
|
|
|
2016-01-11 21:23:28 +00:00
|
|
|
// FindAttachablePluginBySpec fetches a persistent volume plugin by name. Unlike the other "FindPlugin" methods, this
|
|
|
|
// does not return error if no plugin is found. All volumes require a builder and cleaner, but not every volume will
|
|
|
|
// have an attacher/detacher.
|
|
|
|
func (pm *VolumePluginMgr) FindAttachablePluginBySpec(spec *Spec) (AttachableVolumePlugin, error) {
|
|
|
|
volumePlugin, err := pm.FindPluginBySpec(spec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if attachableVolumePlugin, ok := volumePlugin.(AttachableVolumePlugin); ok {
|
|
|
|
return attachableVolumePlugin, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindAttachablePluginByName fetches an attachable volume plugin by name. Unlike the other "FindPlugin" methods, this
|
|
|
|
// does not return error if no plugin is found. All volumes require a builder and cleaner, but not every volume will
|
|
|
|
// have an attacher/detacher.
|
|
|
|
func (pm *VolumePluginMgr) FindAttachablePluginByName(name string) (AttachableVolumePlugin, error) {
|
|
|
|
volumePlugin, err := pm.FindPluginByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if attachablePlugin, ok := volumePlugin.(AttachableVolumePlugin); ok {
|
|
|
|
return attachablePlugin, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-09-03 03:14:26 +00:00
|
|
|
// NewPersistentVolumeRecyclerPodTemplate creates a template for a recycler pod. By default, a recycler pod simply runs
|
|
|
|
// "rm -rf" on a volume and tests for emptiness. Most attributes of the template will be correct for most
|
|
|
|
// plugin implementations. The following attributes can be overridden per plugin via configuration:
|
|
|
|
//
|
|
|
|
// 1. pod.Spec.Volumes[0].VolumeSource must be overridden. Recycler implementations without a valid VolumeSource will fail.
|
|
|
|
// 2. pod.GenerateName helps distinguish recycler pods by name. Recommended. Default is "pv-recycler-".
|
|
|
|
// 3. pod.Spec.ActiveDeadlineSeconds gives the recycler pod a maximum timeout before failing. Recommended. Default is 60 seconds.
|
|
|
|
//
|
|
|
|
// See HostPath and NFS for working recycler examples
|
|
|
|
func NewPersistentVolumeRecyclerPodTemplate() *api.Pod {
|
|
|
|
timeout := int64(60)
|
|
|
|
pod := &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
GenerateName: "pv-recycler-",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
ActiveDeadlineSeconds: &timeout,
|
|
|
|
RestartPolicy: api.RestartPolicyNever,
|
|
|
|
Volumes: []api.Volume{
|
|
|
|
{
|
|
|
|
Name: "vol",
|
|
|
|
// IMPORTANT! All plugins using this template MUST override pod.Spec.Volumes[0].VolumeSource
|
|
|
|
// Recycler implementations without a valid VolumeSource will fail.
|
|
|
|
VolumeSource: api.VolumeSource{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "pv-recycler",
|
|
|
|
Image: "gcr.io/google_containers/busybox",
|
|
|
|
Command: []string{"/bin/sh"},
|
2016-02-10 20:48:39 +00:00
|
|
|
Args: []string{"-c", "test -e /scrub && rm -rf /scrub/..?* /scrub/.[!.]* /scrub/* && test -z \"$(ls -A /scrub)\" || exit 1"},
|
2015-09-03 03:14:26 +00:00
|
|
|
VolumeMounts: []api.VolumeMount{
|
|
|
|
{
|
|
|
|
Name: "vol",
|
|
|
|
MountPath: "/scrub",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return pod
|
|
|
|
}
|