Skip to content

Commit

Permalink
Merge pull request #68931 from mlmhl/check_volume_attachment
Browse files Browse the repository at this point in the history
extract volume attachment status checking operation as a common function when attaching a CSI volume
  • Loading branch information
k8s-ci-robot committed Sep 30, 2018
2 parents 0b3a5cd + 2ff6b7e commit e019add
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions pkg/volume/csi/csi_attacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,13 @@ func (c *csiAttacher) waitForVolumeAttachmentInternal(volumeHandle, attachID str
glog.Error(log("attacher.WaitForAttach failed for volume [%s] (will continue to try): %v", volumeHandle, err))
return "", fmt.Errorf("volume %v has GET error for volume attachment %v: %v", volumeHandle, attachID, err)
}
// if being deleted, fail fast
if attach.GetDeletionTimestamp() != nil {
glog.Error(log("VolumeAttachment [%s] has deletion timestamp, will not continue to wait for attachment", attachID))
return "", errors.New("volume attachment is being deleted")
successful, err := verifyAttachmentStatus(attach, volumeHandle)
if err != nil {
return "", err
}
// attachment OK
if attach.Status.Attached {
if successful {
return attachID, nil
}
// driver reports attach error
attachErr := attach.Status.AttachError
if attachErr != nil {
glog.Error(log("attachment for %v failed: %v", volumeHandle, attachErr.Message))
return "", errors.New(attachErr.Message)
}

watcher, err := c.k8s.StorageV1beta1().VolumeAttachments().Watch(meta.SingleObject(meta.ObjectMeta{Name: attachID, ResourceVersion: attach.ResourceVersion}))
if err != nil {
Expand All @@ -194,21 +186,13 @@ func (c *csiAttacher) waitForVolumeAttachmentInternal(volumeHandle, attachID str
switch event.Type {
case watch.Added, watch.Modified:
attach, _ := event.Object.(*storage.VolumeAttachment)
// if being deleted, fail fast
if attach.GetDeletionTimestamp() != nil {
glog.Error(log("VolumeAttachment [%s] has deletion timestamp, will not continue to wait for attachment", attachID))
return "", errors.New("volume attachment is being deleted")
successful, err := verifyAttachmentStatus(attach, volumeHandle)
if err != nil {
return "", err
}
// attachment OK
if attach.Status.Attached {
if successful {
return attachID, nil
}
// driver reports attach error
attachErr := attach.Status.AttachError
if attachErr != nil {
glog.Error(log("attachment for %v failed: %v", volumeHandle, attachErr.Message))
return "", errors.New(attachErr.Message)
}
case watch.Deleted:
// if deleted, fail fast
glog.Error(log("VolumeAttachment [%s] has been deleted, will not continue to wait for attachment", attachID))
Expand All @@ -226,6 +210,25 @@ func (c *csiAttacher) waitForVolumeAttachmentInternal(volumeHandle, attachID str
}
}

func verifyAttachmentStatus(attachment *storage.VolumeAttachment, volumeHandle string) (bool, error) {
// if being deleted, fail fast
if attachment.GetDeletionTimestamp() != nil {
glog.Error(log("VolumeAttachment [%s] has deletion timestamp, will not continue to wait for attachment", attachment.Name))
return false, errors.New("volume attachment is being deleted")
}
// attachment OK
if attachment.Status.Attached {
return true, nil
}
// driver reports attach error
attachErr := attachment.Status.AttachError
if attachErr != nil {
glog.Error(log("attachment for %v failed: %v", volumeHandle, attachErr.Message))
return false, errors.New(attachErr.Message)
}
return false, nil
}

func (c *csiAttacher) VolumesAreAttached(specs []*volume.Spec, nodeName types.NodeName) (map[*volume.Spec]bool, error) {
glog.V(4).Info(log("probing attachment status for %d volume(s) ", len(specs)))

Expand Down

0 comments on commit e019add

Please sign in to comment.