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"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/exec"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/mount"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/volume"
|
|
|
|
"github.com/golang/glog"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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-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-06-29 17:07:22 +00:00
|
|
|
func (plugin *rbdPlugin) Init(host volume.VolumeHost) {
|
2015-04-07 17:22:23 +00:00
|
|
|
plugin.host = host
|
|
|
|
}
|
|
|
|
|
2015-06-29 17:07:22 +00:00
|
|
|
func (plugin *rbdPlugin) Name() string {
|
|
|
|
return rbdPluginName
|
2015-04-07 17:22:23 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 17:07:22 +00:00
|
|
|
func (plugin *rbdPlugin) CanSupport(spec *volume.Spec) bool {
|
2015-05-23 13:53:33 +00:00
|
|
|
if spec.VolumeSource.RBD == nil && spec.PersistentVolumeSource.RBD == nil {
|
2015-04-07 17:22:23 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
// see if rbd is there
|
|
|
|
_, err := plugin.execCommand("rbd", []string{"-h"})
|
|
|
|
if err == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-29 17:07:22 +00:00
|
|
|
func (plugin *rbdPlugin) NewBuilder(spec *volume.Spec, pod *api.Pod, _ volume.VolumeOptions, mounter mount.Interface) (volume.Builder, error) {
|
2015-04-07 17:22:23 +00:00
|
|
|
secret := ""
|
2015-06-01 14:34:40 +00:00
|
|
|
source := plugin.getRBDVolumeSource(spec)
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2015-06-01 14:34:40 +00:00
|
|
|
secretName, err := kubeClient.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.
|
|
|
|
return plugin.newBuilderInternal(spec, pod.UID, &RBDUtil{}, mounter, secret)
|
|
|
|
}
|
|
|
|
|
2015-06-29 17:07:22 +00:00
|
|
|
func (plugin *rbdPlugin) getRBDVolumeSource(spec *volume.Spec) *api.RBDVolumeSource {
|
2015-06-01 14:34:40 +00:00
|
|
|
if spec.VolumeSource.RBD != nil {
|
|
|
|
return spec.VolumeSource.RBD
|
|
|
|
} else {
|
|
|
|
return spec.PersistentVolumeSource.RBD
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-29 17:07:22 +00:00
|
|
|
func (plugin *rbdPlugin) newBuilderInternal(spec *volume.Spec, podUID types.UID, manager diskManager, mounter mount.Interface, secret string) (volume.Builder, error) {
|
2015-06-01 14:34:40 +00:00
|
|
|
source := plugin.getRBDVolumeSource(spec)
|
|
|
|
pool := source.RBDPool
|
2015-04-07 17:22:23 +00:00
|
|
|
if pool == "" {
|
|
|
|
pool = "rbd"
|
|
|
|
}
|
2015-06-01 14:34:40 +00:00
|
|
|
id := source.RadosUser
|
2015-04-07 17:22:23 +00:00
|
|
|
if id == "" {
|
|
|
|
id = "admin"
|
|
|
|
}
|
2015-06-01 14:34:40 +00:00
|
|
|
keyring := source.Keyring
|
2015-04-07 17:22:23 +00:00
|
|
|
if keyring == "" {
|
|
|
|
keyring = "/etc/ceph/keyring"
|
|
|
|
}
|
|
|
|
|
|
|
|
return &rbd{
|
|
|
|
podUID: podUID,
|
|
|
|
volName: spec.Name,
|
2015-06-01 14:34:40 +00:00
|
|
|
mon: source.CephMonitors,
|
|
|
|
image: source.RBDImage,
|
2015-04-07 17:22:23 +00:00
|
|
|
pool: pool,
|
|
|
|
id: id,
|
|
|
|
keyring: keyring,
|
|
|
|
secret: secret,
|
2015-06-01 14:34:40 +00:00
|
|
|
fsType: source.FSType,
|
|
|
|
readOnly: source.ReadOnly,
|
2015-04-07 17:22:23 +00:00
|
|
|
manager: manager,
|
|
|
|
mounter: mounter,
|
|
|
|
plugin: plugin,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-06-29 17:07:22 +00:00
|
|
|
func (plugin *rbdPlugin) NewCleaner(volName string, podUID types.UID, mounter mount.Interface) (volume.Cleaner, error) {
|
2015-04-07 17:22:23 +00:00
|
|
|
// Inject real implementations here, test through the internal function.
|
|
|
|
return plugin.newCleanerInternal(volName, podUID, &RBDUtil{}, mounter)
|
|
|
|
}
|
|
|
|
|
2015-06-29 17:07:22 +00:00
|
|
|
func (plugin *rbdPlugin) newCleanerInternal(volName string, podUID types.UID, manager diskManager, mounter mount.Interface) (volume.Cleaner, error) {
|
2015-04-07 17:22:23 +00:00
|
|
|
return &rbd{
|
|
|
|
podUID: podUID,
|
|
|
|
volName: volName,
|
|
|
|
manager: manager,
|
|
|
|
mounter: mounter,
|
|
|
|
plugin: plugin,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type rbd struct {
|
|
|
|
volName string
|
|
|
|
podUID types.UID
|
|
|
|
mon []string
|
|
|
|
pool string
|
|
|
|
id string
|
|
|
|
image string
|
|
|
|
keyring string
|
|
|
|
secret string
|
|
|
|
fsType string
|
|
|
|
readOnly bool
|
2015-06-29 17:07:22 +00:00
|
|
|
plugin *rbdPlugin
|
2015-04-07 17:22:23 +00:00
|
|
|
mounter mount.Interface
|
|
|
|
// Utility interface that provides API calls to the provider to attach/detach disks.
|
|
|
|
manager diskManager
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
return rbd.plugin.host.GetPodVolumeDir(rbd.podUID, util.EscapeQualifiedNameForDisk(name), rbd.volName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rbd *rbd) SetUp() error {
|
|
|
|
return rbd.SetUpAt(rbd.GetPath())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rbd *rbd) SetUpAt(dir string) error {
|
|
|
|
// diskSetUp checks mountpoints and prevent repeated calls
|
|
|
|
err := diskSetUp(rbd.manager, *rbd, dir, rbd.mounter)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("rbd: failed to setup")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
globalPDPath := rbd.manager.MakeGlobalPDName(*rbd)
|
|
|
|
// make mountpoint rw/ro work as expected
|
|
|
|
//FIXME revisit pkg/util/mount and ensure rw/ro is implemented as expected
|
|
|
|
mode := "rw"
|
|
|
|
if rbd.readOnly {
|
|
|
|
mode = "ro"
|
|
|
|
}
|
|
|
|
rbd.plugin.execCommand("mount", []string{"-o", "remount," + mode, globalPDPath, dir})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmounts the bind mount, and detaches the disk only if the disk
|
|
|
|
// resource was the last reference to that disk on the kubelet.
|
|
|
|
func (rbd *rbd) TearDown() error {
|
|
|
|
return rbd.TearDownAt(rbd.GetPath())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rbd *rbd) TearDownAt(dir string) error {
|
|
|
|
return diskTearDown(rbd.manager, *rbd, dir, rbd.mounter)
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|