Skip to content

Commit

Permalink
Merge pull request kubernetes#26615 from jsafrane/gce-attach-tests
Browse files Browse the repository at this point in the history
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 kubernetes#25888, so mounting is not tested at all.
  • Loading branch information
k8s-merge-robot committed Jun 9, 2016
2 parents dd345fb + 5cd5ae8 commit c9c4ada
Show file tree
Hide file tree
Showing 4 changed files with 399 additions and 19 deletions.
25 changes: 25 additions & 0 deletions pkg/cloudprovider/providers/gce/gce.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
43 changes: 25 additions & 18 deletions pkg/volume/gce_pd/attacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,32 @@ 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"
"k8s.io/kubernetes/pkg/volume"
)

type gcePersistentDiskAttacher struct {
host volume.VolumeHost
host volume.VolumeHost
gceDisks gce.Disks
}

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) {
Expand All @@ -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(
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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
}
Expand Down
Loading

0 comments on commit c9c4ada

Please sign in to comment.