Commit c9c4ada3 authored by k8s-merge-robot's avatar k8s-merge-robot

Merge pull request #26615 from jsafrane/gce-attach-tests

Automatic merge from submit-queue GCE attach tests Add basic tests for GCE attacher. Looking at the code, it would deserve some refactoring as suggested in #25888, so mounting is not tested at all.
parents dd345fbf 5cd5ae8d
......@@ -100,6 +100,31 @@ type Config struct {
}
}
// Disks is interface for manipulation with GCE PDs.
type Disks interface {
// AttachDisk attaches given disk to given instance. Current instance
// is used when instanceID is empty string.
AttachDisk(diskName, instanceID string, readOnly bool) error
// DetachDisk detaches given disk to given instance. Current instance
// is used when instanceID is empty string.
DetachDisk(devicePath, instanceID string) error
// DiskIsAttached checks if a disk is attached to the given node.
DiskIsAttached(diskName, instanceID string) (bool, error)
// CreateDisk creates a new PD with given properties. Tags are serialized
// as JSON into Description field.
CreateDisk(name string, zone string, sizeGb int64, tags map[string]string) error
// DeleteDisk deletes PD.
DeleteDisk(diskToDelete string) error
// GetAutoLabelsForPD returns labels to apply to PeristentVolume
// representing this PD, namely failure domain and zone.
GetAutoLabelsForPD(name string) (map[string]string, error)
}
type instRefSlice []*compute.InstanceReference
func (p instRefSlice) Len() int { return len(p) }
......
......@@ -25,6 +25,7 @@ import (
"time"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/cloudprovider/providers/gce"
"k8s.io/kubernetes/pkg/util/exec"
"k8s.io/kubernetes/pkg/util/mount"
"k8s.io/kubernetes/pkg/util/sets"
......@@ -32,7 +33,8 @@ import (
)
type gcePersistentDiskAttacher struct {
host volume.VolumeHost
host volume.VolumeHost
gceDisks gce.Disks
}
var _ volume.Attacher = &gcePersistentDiskAttacher{}
......@@ -40,7 +42,15 @@ var _ volume.Attacher = &gcePersistentDiskAttacher{}
var _ volume.AttachableVolumePlugin = &gcePersistentDiskPlugin{}
func (plugin *gcePersistentDiskPlugin) NewAttacher() (volume.Attacher, error) {
return &gcePersistentDiskAttacher{host: plugin.host}, nil
gceCloud, err := getCloudProvider(plugin.host.GetCloudProvider())
if err != nil {
return nil, err
}
return &gcePersistentDiskAttacher{
host: plugin.host,
gceDisks: gceCloud,
}, nil
}
func (plugin *gcePersistentDiskPlugin) GetDeviceName(spec *volume.Spec) (string, error) {
......@@ -63,12 +73,7 @@ func (attacher *gcePersistentDiskAttacher) Attach(spec *volume.Spec, hostName st
volumeSource, readOnly := getVolumeSource(spec)
pdName := volumeSource.PDName
gceCloud, err := getCloudProvider(attacher.host.GetCloudProvider())
if err != nil {
return err
}
attached, err := gceCloud.DiskIsAttached(pdName, hostName)
attached, err := attacher.gceDisks.DiskIsAttached(pdName, hostName)
if err != nil {
// Log error and continue with attach
glog.Errorf(
......@@ -82,7 +87,7 @@ func (attacher *gcePersistentDiskAttacher) Attach(spec *volume.Spec, hostName st
return nil
}
if err = gceCloud.AttachDisk(pdName, hostName, readOnly); err != nil {
if err = attacher.gceDisks.AttachDisk(pdName, hostName, readOnly); err != nil {
glog.Errorf("Error attaching PD %q to node %q: %+v", pdName, hostName, err)
return err
}
......@@ -166,13 +171,20 @@ func (attacher *gcePersistentDiskAttacher) MountDevice(spec *volume.Spec, device
}
type gcePersistentDiskDetacher struct {
host volume.VolumeHost
gceDisks gce.Disks
}
var _ volume.Detacher = &gcePersistentDiskDetacher{}
func (plugin *gcePersistentDiskPlugin) NewDetacher() (volume.Detacher, error) {
return &gcePersistentDiskDetacher{host: plugin.host}, nil
gceCloud, err := getCloudProvider(plugin.host.GetCloudProvider())
if err != nil {
return nil, err
}
return &gcePersistentDiskDetacher{
gceDisks: gceCloud,
}, nil
}
// Detach checks with the GCE cloud provider if the specified volume is already
......@@ -185,12 +197,7 @@ func (plugin *gcePersistentDiskPlugin) NewDetacher() (volume.Detacher, error) {
func (detacher *gcePersistentDiskDetacher) Detach(deviceMountPath string, hostName string) error {
pdName := path.Base(deviceMountPath)
gceCloud, err := getCloudProvider(detacher.host.GetCloudProvider())
if err != nil {
return err
}
attached, err := gceCloud.DiskIsAttached(pdName, hostName)
attached, err := detacher.gceDisks.DiskIsAttached(pdName, hostName)
if err != nil {
// Log error and continue with detach
glog.Errorf(
......@@ -204,7 +211,7 @@ func (detacher *gcePersistentDiskDetacher) Detach(deviceMountPath string, hostNa
return nil
}
if err = gceCloud.DetachDisk(pdName, hostName); err != nil {
if err = detacher.gceDisks.DetachDisk(pdName, hostName); err != nil {
glog.Errorf("Error detaching PD %q from node %q: %v", pdName, hostName, err)
return err
}
......
......@@ -43,7 +43,12 @@ const (
maxChecks = 60
maxRetries = 10
checkSleepDuration = time.Second
errorSleepDuration = 5 * time.Second
)
// These variables are modified only in unit tests and should be constant
// otherwise.
var (
errorSleepDuration time.Duration = 5 * time.Second
)
type GCEDiskUtil struct{}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment