2015-04-10 16:54:01 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-04-10 16:54:01 +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 cinder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2016-08-17 19:24:53 +00:00
|
|
|
"fmt"
|
2016-11-22 06:49:18 +00:00
|
|
|
"io/ioutil"
|
2015-04-10 16:54:01 +00:00
|
|
|
"os"
|
2016-08-17 19:24:53 +00:00
|
|
|
"strings"
|
2015-04-10 16:54:01 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2017-05-01 18:59:24 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
2017-06-22 17:25:57 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2017-06-22 18:24:23 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2017-06-23 20:56:37 +00:00
|
|
|
clientset "k8s.io/client-go/kubernetes"
|
2017-05-30 14:46:00 +00:00
|
|
|
kubeletapis "k8s.io/kubernetes/pkg/kubelet/apis"
|
2015-04-10 16:54:01 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/exec"
|
2015-12-15 11:38:59 +00:00
|
|
|
"k8s.io/kubernetes/pkg/volume"
|
2015-04-10 16:54:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CinderDiskUtil struct{}
|
|
|
|
|
|
|
|
// Attaches a disk specified by a volume.CinderPersistenDisk to the current kubelet.
|
2016-10-03 09:29:50 +00:00
|
|
|
// Mounts the disk to its global path.
|
2016-03-23 05:12:21 +00:00
|
|
|
func (util *CinderDiskUtil) AttachDisk(b *cinderVolumeMounter, globalPDPath string) error {
|
2015-04-10 16:54:01 +00:00
|
|
|
options := []string{}
|
|
|
|
if b.readOnly {
|
|
|
|
options = append(options, "ro")
|
|
|
|
}
|
2015-12-14 13:55:26 +00:00
|
|
|
cloud, err := b.plugin.getCloudProvider()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-04-10 16:54:01 +00:00
|
|
|
}
|
2016-02-24 17:07:04 +00:00
|
|
|
instanceid, err := cloud.InstanceID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
diskid, err := cloud.AttachDisk(instanceid, b.pdName)
|
2015-04-10 16:54:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var devicePath string
|
|
|
|
numTries := 0
|
|
|
|
for {
|
2016-02-24 17:07:04 +00:00
|
|
|
devicePath = cloud.GetDevicePath(diskid)
|
2015-04-10 16:54:01 +00:00
|
|
|
probeAttachedVolume()
|
|
|
|
|
|
|
|
_, err := os.Stat(devicePath)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
numTries++
|
|
|
|
if numTries == 10 {
|
|
|
|
return errors.New("Could not attach disk: Timeout after 60s")
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second * 6)
|
|
|
|
}
|
|
|
|
notmnt, err := b.mounter.IsLikelyNotMountPoint(globalPDPath)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if err := os.MkdirAll(globalPDPath, 0750); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
notmnt = true
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if notmnt {
|
2016-01-19 08:36:38 +00:00
|
|
|
err = b.blockDeviceMounter.FormatAndMount(devicePath, globalPDPath, b.fsType, options)
|
2015-04-10 16:54:01 +00:00
|
|
|
if err != nil {
|
|
|
|
os.Remove(globalPDPath)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
glog.V(2).Infof("Safe mount successful: %q\n", devicePath)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmounts the device and detaches the disk from the kubelet's host machine.
|
2016-03-23 05:12:21 +00:00
|
|
|
func (util *CinderDiskUtil) DetachDisk(cd *cinderVolumeUnmounter) error {
|
2015-04-10 16:54:01 +00:00
|
|
|
globalPDPath := makeGlobalPDName(cd.plugin.host, cd.pdName)
|
|
|
|
if err := cd.mounter.Unmount(globalPDPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Remove(globalPDPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
glog.V(2).Infof("Successfully unmounted main device: %s\n", globalPDPath)
|
|
|
|
|
2015-12-14 13:55:26 +00:00
|
|
|
cloud, err := cd.plugin.getCloudProvider()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-04-10 16:54:01 +00:00
|
|
|
}
|
2016-02-24 17:07:04 +00:00
|
|
|
instanceid, err := cloud.InstanceID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = cloud.DetachDisk(instanceid, cd.pdName); err != nil {
|
2015-04-10 16:54:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
glog.V(2).Infof("Successfully detached cinder volume %s", cd.pdName)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-15 11:38:59 +00:00
|
|
|
func (util *CinderDiskUtil) DeleteVolume(cd *cinderVolumeDeleter) error {
|
2015-12-14 13:55:26 +00:00
|
|
|
cloud, err := cd.plugin.getCloudProvider()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-12-15 11:38:59 +00:00
|
|
|
}
|
|
|
|
|
2015-12-14 13:55:26 +00:00
|
|
|
if err = cloud.DeleteVolume(cd.pdName); err != nil {
|
2016-09-07 08:16:17 +00:00
|
|
|
// OpenStack cloud provider returns volume.tryAgainError when necessary,
|
|
|
|
// no handling needed here.
|
2015-12-15 11:38:59 +00:00
|
|
|
glog.V(2).Infof("Error deleting cinder volume %s: %v", cd.pdName, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
glog.V(2).Infof("Successfully deleted cinder volume %s", cd.pdName)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 18:59:24 +00:00
|
|
|
func getZonesFromNodes(kubeClient clientset.Interface) (sets.String, error) {
|
|
|
|
// TODO: caching, currently it is overkill because it calls this function
|
|
|
|
// only when it creates dynamic PV
|
|
|
|
zones := make(sets.String)
|
|
|
|
nodes, err := kubeClient.Core().Nodes().List(metav1.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
glog.V(2).Infof("Error listing nodes")
|
|
|
|
return zones, err
|
|
|
|
}
|
|
|
|
for _, node := range nodes.Items {
|
2017-05-30 14:46:00 +00:00
|
|
|
if zone, ok := node.Labels[kubeletapis.LabelZoneFailureDomain]; ok {
|
2017-05-01 18:59:24 +00:00
|
|
|
zones.Insert(zone)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
glog.V(4).Infof("zones found: %v", zones)
|
|
|
|
return zones, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (util *CinderDiskUtil) CreateVolume(c *cinderVolumeProvisioner) (volumeID string, volumeSizeGB int, volumeLabels map[string]string, err error) {
|
2015-12-14 13:55:26 +00:00
|
|
|
cloud, err := c.plugin.getCloudProvider()
|
|
|
|
if err != nil {
|
2017-05-01 18:59:24 +00:00
|
|
|
return "", 0, nil, err
|
2015-12-15 11:38:59 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 20:58:56 +00:00
|
|
|
capacity := c.options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
|
2016-10-12 10:22:01 +00:00
|
|
|
volSizeBytes := capacity.Value()
|
2015-12-15 11:38:59 +00:00
|
|
|
// Cinder works with gigabytes, convert to GiB with rounding up
|
|
|
|
volSizeGB := int(volume.RoundUpSize(volSizeBytes, 1024*1024*1024))
|
2016-02-12 08:46:59 +00:00
|
|
|
name := volume.GenerateVolumeName(c.options.ClusterName, c.options.PVName, 255) // Cinder volume name can have up to 255 characters
|
2016-08-17 19:24:53 +00:00
|
|
|
vtype := ""
|
|
|
|
availability := ""
|
|
|
|
// Apply ProvisionerParameters (case-insensitive). We leave validation of
|
|
|
|
// the values to the cloud provider.
|
|
|
|
for k, v := range c.options.Parameters {
|
|
|
|
switch strings.ToLower(k) {
|
|
|
|
case "type":
|
|
|
|
vtype = v
|
|
|
|
case "availability":
|
|
|
|
availability = v
|
|
|
|
default:
|
2017-05-01 18:59:24 +00:00
|
|
|
return "", 0, nil, fmt.Errorf("invalid option %q for volume plugin %s", k, c.plugin.GetPluginName())
|
2016-08-17 19:24:53 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-12 10:22:01 +00:00
|
|
|
// TODO: implement PVC.Selector parsing
|
|
|
|
if c.options.PVC.Spec.Selector != nil {
|
2017-05-01 18:59:24 +00:00
|
|
|
return "", 0, nil, fmt.Errorf("claim.Spec.Selector is not supported for dynamic provisioning on Cinder")
|
2016-08-17 19:24:53 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 18:59:24 +00:00
|
|
|
if availability == "" {
|
|
|
|
// No zone specified, choose one randomly in the same region
|
|
|
|
zones, err := getZonesFromNodes(c.plugin.host.GetKubeClient())
|
|
|
|
if err != nil {
|
|
|
|
glog.V(2).Infof("error getting zone information: %v", err)
|
|
|
|
return "", 0, nil, err
|
|
|
|
}
|
|
|
|
// if we did not get any zones, lets leave it blank and gophercloud will
|
|
|
|
// use zone "nova" as default
|
|
|
|
if len(zones) > 0 {
|
|
|
|
availability = volume.ChooseZoneForVolume(zones, c.options.PVC.Name)
|
|
|
|
}
|
2015-12-15 11:38:59 +00:00
|
|
|
}
|
2017-05-01 18:59:24 +00:00
|
|
|
|
2017-02-21 10:58:16 +00:00
|
|
|
volumeID, volumeAZ, errr := cloud.CreateVolume(name, volSizeGB, vtype, availability, c.options.CloudTags)
|
2017-05-01 18:59:24 +00:00
|
|
|
if errr != nil {
|
|
|
|
glog.V(2).Infof("Error creating cinder volume: %v", errr)
|
|
|
|
return "", 0, nil, errr
|
|
|
|
}
|
2017-02-21 10:58:16 +00:00
|
|
|
glog.V(2).Infof("Successfully created cinder volume %s", volumeID)
|
2017-05-01 18:59:24 +00:00
|
|
|
|
|
|
|
// these are needed that pod is spawning to same AZ
|
|
|
|
volumeLabels = make(map[string]string)
|
2017-05-30 14:46:00 +00:00
|
|
|
volumeLabels[kubeletapis.LabelZoneFailureDomain] = volumeAZ
|
2017-05-01 18:59:24 +00:00
|
|
|
|
2017-02-21 10:58:16 +00:00
|
|
|
return volumeID, volSizeGB, volumeLabels, nil
|
2015-12-15 11:38:59 +00:00
|
|
|
}
|
|
|
|
|
2015-04-10 16:54:01 +00:00
|
|
|
func probeAttachedVolume() error {
|
2016-11-22 06:49:18 +00:00
|
|
|
// rescan scsi bus
|
|
|
|
scsiHostRescan()
|
|
|
|
|
2015-04-10 16:54:01 +00:00
|
|
|
executor := exec.New()
|
|
|
|
args := []string{"trigger"}
|
2017-02-10 21:22:32 +00:00
|
|
|
cmd := executor.Command("udevadm", args...)
|
2015-04-10 16:54:01 +00:00
|
|
|
_, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("error running udevadm trigger %v\n", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
glog.V(4).Infof("Successfully probed all attachments")
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-22 06:49:18 +00:00
|
|
|
|
|
|
|
func scsiHostRescan() {
|
|
|
|
scsi_path := "/sys/class/scsi_host/"
|
|
|
|
if dirs, err := ioutil.ReadDir(scsi_path); err == nil {
|
|
|
|
for _, f := range dirs {
|
|
|
|
name := scsi_path + f.Name() + "/scan"
|
|
|
|
data := []byte("- - -")
|
|
|
|
ioutil.WriteFile(name, data, 0666)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|