2015-04-07 17:22:23 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 The Kubernetes Authors 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package rbd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
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/exec"
|
|
|
|
"k8s.io/kubernetes/pkg/util/mount"
|
2016-01-11 07:55:51 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/strings"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/volume"
|
2015-04-07 17:22:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// This is the primary entrypoint for volume plugins.
|
|
|
|
func ProbeVolumePlugins() []volume.VolumePlugin {
|
2015-06-29 17:07:22 +00:00
|
|
|
return []volume.VolumePlugin{&rbdPlugin{nil, exec.New()}}
|
2015-04-07 17:22:23 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 17:07:22 +00:00
|
|
|
type rbdPlugin struct {
|
2015-04-07 17:22:23 +00:00
|
|
|
host volume.VolumeHost
|
|
|
|
exe exec.Interface
|
|
|
|
}
|
|
|
|
|
2015-06-29 17:07:22 +00:00
|
|
|
var _ volume.VolumePlugin = &rbdPlugin{}
|
2015-06-26 20:37:11 +00:00
|
|
|
var _ volume.PersistentVolumePlugin = &rbdPlugin{}
|
2015-04-07 17:22:23 +00:00
|
|
|
|
|
|
|
const (
|
2015-06-29 17:07:22 +00:00
|
|
|
rbdPluginName = "kubernetes.io/rbd"
|
2015-04-07 17:22:23 +00:00
|
|
|
)
|
|
|
|
|
2015-09-30 18:31:53 +00:00
|
|
|
func (plugin *rbdPlugin) Init(host volume.VolumeHost) error {
|
2015-04-07 17:22:23 +00:00
|
|
|
plugin.host = host
|
2015-09-30 18:31:53 +00:00
|
|
|
return nil
|
2015-04-07 17:22:23 +00:00
|
|
|
}
|
|
|
|
|
2016-05-30 22:48:21 +00:00
|
|
|
func (plugin *rbdPlugin) GetPluginName() string {
|
2015-06-29 17:07:22 +00:00
|
|
|
return rbdPluginName
|
2015-04-07 17:22:23 +00:00
|
|
|
}
|
|
|
|
|
2016-05-30 22:48:21 +00:00
|
|
|
func (plugin *rbdPlugin) GetVolumeName(spec *volume.Spec) (string, error) {
|
2016-05-30 02:22:22 +00:00
|
|
|
volumeSource, _, err := getVolumeSource(spec)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2016-05-30 22:48:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"%v:%v",
|
|
|
|
volumeSource.CephMonitors,
|
|
|
|
volumeSource.RBDImage), nil
|
|
|
|
}
|
|
|
|
|
2015-06-29 17:07:22 +00:00
|
|
|
func (plugin *rbdPlugin) CanSupport(spec *volume.Spec) bool {
|
2015-08-12 19:11:03 +00:00
|
|
|
if (spec.Volume != nil && spec.Volume.RBD == nil) || (spec.PersistentVolume != nil && spec.PersistentVolume.Spec.RBD == nil) {
|
2015-04-07 17:22:23 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-02-23 23:13:14 +00:00
|
|
|
return true
|
2015-04-07 17:22:23 +00:00
|
|
|
}
|
|
|
|
|
2016-05-30 02:22:22 +00:00
|
|
|
func (plugin *rbdPlugin) RequiresRemount() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-06-29 17:07:22 +00:00
|
|
|
func (plugin *rbdPlugin) GetAccessModes() []api.PersistentVolumeAccessMode {
|
2015-04-07 17:22:23 +00:00
|
|
|
return []api.PersistentVolumeAccessMode{
|
|
|
|
api.ReadWriteOnce,
|
|
|
|
api.ReadOnlyMany,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
func (plugin *rbdPlugin) NewMounter(spec *volume.Spec, pod *api.Pod, _ volume.VolumeOptions) (volume.Mounter, error) {
|
2015-04-07 17:22:23 +00:00
|
|
|
secret := ""
|
2015-06-26 20:37:11 +00:00
|
|
|
source, _ := plugin.getRBDVolumeSource(spec)
|
2015-06-01 14:34:40 +00:00
|
|
|
|
|
|
|
if source.SecretRef != nil {
|
2015-04-07 17:22:23 +00:00
|
|
|
kubeClient := plugin.host.GetKubeClient()
|
|
|
|
if kubeClient == nil {
|
|
|
|
return nil, fmt.Errorf("Cannot get kube client")
|
|
|
|
}
|
|
|
|
|
2016-02-03 21:21:05 +00:00
|
|
|
secretName, err := kubeClient.Core().Secrets(pod.Namespace).Get(source.SecretRef.Name)
|
2015-04-07 17:22:23 +00:00
|
|
|
if err != nil {
|
2015-06-01 14:34:40 +00:00
|
|
|
glog.Errorf("Couldn't get secret %v/%v", pod.Namespace, source.SecretRef)
|
2015-04-07 17:22:23 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for name, data := range secretName.Data {
|
|
|
|
secret = string(data)
|
|
|
|
glog.V(1).Infof("ceph secret info: %s/%s", name, secret)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
// Inject real implementations here, test through the internal function.
|
2016-03-23 05:12:21 +00:00
|
|
|
return plugin.newMounterInternal(spec, pod.UID, &RBDUtil{}, plugin.host.GetMounter(), secret)
|
2015-04-07 17:22:23 +00:00
|
|
|
}
|
|
|
|
|
2015-06-26 20:37:11 +00:00
|
|
|
func (plugin *rbdPlugin) getRBDVolumeSource(spec *volume.Spec) (*api.RBDVolumeSource, bool) {
|
|
|
|
// rbd volumes used directly in a pod have a ReadOnly flag set by the pod author.
|
|
|
|
// rbd volumes used as a PersistentVolume gets the ReadOnly flag indirectly through the persistent-claim volume used to mount the PV
|
2015-08-12 19:11:03 +00:00
|
|
|
if spec.Volume != nil && spec.Volume.RBD != nil {
|
|
|
|
return spec.Volume.RBD, spec.Volume.RBD.ReadOnly
|
2015-06-01 14:34:40 +00:00
|
|
|
} else {
|
2015-08-12 19:11:03 +00:00
|
|
|
return spec.PersistentVolume.Spec.RBD, spec.ReadOnly
|
2015-06-01 14:34:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
func (plugin *rbdPlugin) newMounterInternal(spec *volume.Spec, podUID types.UID, manager diskManager, mounter mount.Interface, secret string) (volume.Mounter, error) {
|
2015-06-26 20:37:11 +00:00
|
|
|
source, readOnly := plugin.getRBDVolumeSource(spec)
|
2015-06-01 14:34:40 +00:00
|
|
|
pool := source.RBDPool
|
|
|
|
id := source.RadosUser
|
|
|
|
keyring := source.Keyring
|
2015-04-07 17:22:23 +00:00
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
return &rbdMounter{
|
2015-07-24 09:20:42 +00:00
|
|
|
rbd: &rbd{
|
|
|
|
podUID: podUID,
|
2015-08-12 19:11:03 +00:00
|
|
|
volName: spec.Name(),
|
2015-07-24 09:20:42 +00:00
|
|
|
Image: source.RBDImage,
|
|
|
|
Pool: pool,
|
2015-06-26 20:37:11 +00:00
|
|
|
ReadOnly: readOnly,
|
2015-07-24 09:20:42 +00:00
|
|
|
manager: manager,
|
2016-03-23 23:45:24 +00:00
|
|
|
mounter: &mount.SafeFormatAndMount{Interface: mounter, Runner: exec.New()},
|
2015-07-24 09:20:42 +00:00
|
|
|
plugin: plugin,
|
|
|
|
},
|
|
|
|
Mon: source.CephMonitors,
|
|
|
|
Id: id,
|
|
|
|
Keyring: keyring,
|
|
|
|
Secret: secret,
|
|
|
|
fsType: source.FSType,
|
2015-04-07 17:22:23 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
func (plugin *rbdPlugin) NewUnmounter(volName string, podUID types.UID) (volume.Unmounter, error) {
|
2015-04-07 17:22:23 +00:00
|
|
|
// Inject real implementations here, test through the internal function.
|
2016-03-23 05:12:21 +00:00
|
|
|
return plugin.newUnmounterInternal(volName, podUID, &RBDUtil{}, plugin.host.GetMounter())
|
2015-04-07 17:22:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
func (plugin *rbdPlugin) newUnmounterInternal(volName string, podUID types.UID, manager diskManager, mounter mount.Interface) (volume.Unmounter, error) {
|
|
|
|
return &rbdUnmounter{
|
|
|
|
rbdMounter: &rbdMounter{
|
2015-08-10 22:33:25 +00:00
|
|
|
rbd: &rbd{
|
|
|
|
podUID: podUID,
|
|
|
|
volName: volName,
|
|
|
|
manager: manager,
|
2016-03-23 23:45:24 +00:00
|
|
|
mounter: &mount.SafeFormatAndMount{Interface: mounter, Runner: exec.New()},
|
2015-08-10 22:33:25 +00:00
|
|
|
plugin: plugin,
|
|
|
|
},
|
|
|
|
Mon: make([]string, 0),
|
|
|
|
},
|
|
|
|
}, nil
|
2015-04-07 17:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type rbd struct {
|
2015-07-24 09:20:42 +00:00
|
|
|
volName string
|
|
|
|
podUID types.UID
|
2015-06-29 19:07:52 +00:00
|
|
|
Pool string
|
|
|
|
Image string
|
|
|
|
ReadOnly bool
|
2015-06-29 17:07:22 +00:00
|
|
|
plugin *rbdPlugin
|
2015-11-05 21:49:40 +00:00
|
|
|
mounter *mount.SafeFormatAndMount
|
2015-04-07 17:22:23 +00:00
|
|
|
// Utility interface that provides API calls to the provider to attach/detach disks.
|
|
|
|
manager diskManager
|
2015-12-04 20:40:01 +00:00
|
|
|
volume.MetricsNil
|
2015-04-07 17:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rbd *rbd) GetPath() string {
|
2015-06-29 17:07:22 +00:00
|
|
|
name := rbdPluginName
|
2015-04-07 17:22:23 +00:00
|
|
|
// safe to use PodVolumeDir now: volume teardown occurs before pod is cleaned up
|
2016-01-11 07:55:51 +00:00
|
|
|
return rbd.plugin.host.GetPodVolumeDir(rbd.podUID, strings.EscapeQualifiedNameForDisk(name), rbd.volName)
|
2015-04-07 17:22:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
type rbdMounter struct {
|
2015-07-24 09:20:42 +00:00
|
|
|
*rbd
|
|
|
|
// capitalized so they can be exported in persistRBD()
|
|
|
|
Mon []string
|
|
|
|
Id string
|
|
|
|
Keyring string
|
|
|
|
Secret string
|
|
|
|
fsType string
|
2015-04-07 17:22:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
var _ volume.Mounter = &rbdMounter{}
|
2015-07-24 09:20:42 +00:00
|
|
|
|
2015-10-30 20:25:36 +00:00
|
|
|
func (b *rbd) GetAttributes() volume.Attributes {
|
|
|
|
return volume.Attributes{
|
2016-01-11 16:10:55 +00:00
|
|
|
ReadOnly: b.ReadOnly,
|
|
|
|
Managed: !b.ReadOnly,
|
|
|
|
SupportsSELinux: true,
|
2015-10-30 20:25:36 +00:00
|
|
|
}
|
2015-10-20 18:49:39 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
func (b *rbdMounter) SetUp(fsGroup *int64) error {
|
2015-12-18 15:55:11 +00:00
|
|
|
return b.SetUpAt(b.GetPath(), fsGroup)
|
2015-07-24 09:20:42 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
func (b *rbdMounter) SetUpAt(dir string, fsGroup *int64) error {
|
2015-04-07 17:22:23 +00:00
|
|
|
// diskSetUp checks mountpoints and prevent repeated calls
|
2015-12-18 18:57:25 +00:00
|
|
|
err := diskSetUp(b.manager, *b, dir, b.mounter, fsGroup)
|
2015-04-07 17:22:23 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("rbd: failed to setup")
|
|
|
|
}
|
2015-10-08 17:50:41 +00:00
|
|
|
return err
|
2015-04-07 17:22:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
type rbdUnmounter struct {
|
|
|
|
*rbdMounter
|
2015-07-24 09:20:42 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
var _ volume.Unmounter = &rbdUnmounter{}
|
2015-07-24 09:20:42 +00:00
|
|
|
|
2015-04-07 17:22:23 +00:00
|
|
|
// Unmounts the bind mount, and detaches the disk only if the disk
|
|
|
|
// resource was the last reference to that disk on the kubelet.
|
2016-03-23 05:12:21 +00:00
|
|
|
func (c *rbdUnmounter) TearDown() error {
|
2015-07-24 09:20:42 +00:00
|
|
|
return c.TearDownAt(c.GetPath())
|
2015-04-07 17:22:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
func (c *rbdUnmounter) TearDownAt(dir string) error {
|
2015-07-24 09:20:42 +00:00
|
|
|
return diskTearDown(c.manager, *c, dir, c.mounter)
|
2015-04-07 17:22:23 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 17:07:22 +00:00
|
|
|
func (plugin *rbdPlugin) execCommand(command string, args []string) ([]byte, error) {
|
2015-04-07 17:22:23 +00:00
|
|
|
cmd := plugin.exe.Command(command, args...)
|
|
|
|
return cmd.CombinedOutput()
|
|
|
|
}
|
2016-05-30 22:48:21 +00:00
|
|
|
|
2016-05-30 02:22:22 +00:00
|
|
|
func getVolumeSource(
|
|
|
|
spec *volume.Spec) (*api.RBDVolumeSource, bool, error) {
|
2016-05-30 22:48:21 +00:00
|
|
|
if spec.Volume != nil && spec.Volume.RBD != nil {
|
2016-05-30 02:22:22 +00:00
|
|
|
return spec.Volume.RBD, spec.Volume.RBD.ReadOnly, nil
|
|
|
|
} else if spec.PersistentVolume != nil &&
|
|
|
|
spec.PersistentVolume.Spec.RBD != nil {
|
|
|
|
return spec.PersistentVolume.Spec.RBD, spec.ReadOnly, nil
|
2016-05-30 22:48:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-30 02:22:22 +00:00
|
|
|
return nil, false, fmt.Errorf("Spec does not reference a RBD volume type")
|
2016-05-30 22:48:21 +00:00
|
|
|
}
|