Skip to content

Commit

Permalink
[build][enhance] enhance api detach volume
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed Jul 1, 2024
1 parent 8a7be0c commit d94c529
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 35 deletions.
2 changes: 1 addition & 1 deletion pkg/cloud/cloud_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (s *cloud) waitDiskAttached(instanceID string, volumeID string) error {
return true, lfmt.Errorf("volume %q is in error state", volumeID)
}

if vol.VmId != nil && *vol.VmId == instanceID && vol.Status == VolumeInUseStatus {
if vol.VmId == instanceID && vol.Status == VolumeInUseStatus {
return true, nil
}

Expand Down
10 changes: 3 additions & 7 deletions pkg/cloud/entity/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (s *Volume) IsCreating() bool {
}

func (s *Volume) AttachedTheInstance(pinstanceId string) bool {
if s.VmId != nil && *s.VmId == pinstanceId {
if s.VmId == pinstanceId {
return true
}

Expand All @@ -41,11 +41,7 @@ func (s *Volume) CanDelete() bool {
return true
}

if s.VmId == nil {
return true
}

if *s.VmId == "" {
if s.VmId == "" {
return true
}

Expand All @@ -61,7 +57,7 @@ func (s *Volume) IsAttched() bool {
return true
}

if s.VmId != nil && *s.VmId != "" {
if s.VmId != "" {
return true
}

Expand Down
35 changes: 10 additions & 25 deletions pkg/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,49 +293,34 @@ func (s *controllerService) ControllerPublishVolume(pctx lctx.Context, preq *lcs
}

func (s *controllerService) ControllerUnpublishVolume(_ lctx.Context, preq *lcsi.ControllerUnpublishVolumeRequest) (*lcsi.ControllerUnpublishVolumeResponse, error) {
llog.V(4).InfoS("[INFO] - ControllerUnpublishVolume: Called", "preq", *preq)
llog.InfoS("[INFO] - ControllerUnpublishVolume: Called", "request", *preq)

if err := validateControllerUnpublishVolumeRequest(preq); err != nil {
llog.ErrorS(err, "[ERROR] - ControllerUnpublishVolume: Invalid request")
llog.ErrorS(err, "[ERROR] - ControllerUnpublishVolume: Invalid request", "request", *preq)
return nil, err
}

volumeID := preq.GetVolumeId()
nodeID := preq.GetNodeId()
key := volumeID + nodeID

if !s.inFlight.Insert(volumeID + nodeID) {
llog.InfoS("[INFO] - ControllerUnpublishVolume: Operation is already in-flight", "volumeID", volumeID, "nodeID", nodeID)
if !s.inFlight.Insert(key) {
llog.InfoS("[INFO] - ControllerUnpublishVolume: Operation is already in-flight", "volumeID", volumeID, "nodeID", nodeID, "inflightKey", key)
return nil, ErrOperationAlreadyExists(volumeID)
}

llog.V(5).InfoS("[INFO] - ControllerUnpublishVolume: Insert this action to inflight cach3", "volumeID", volumeID, "nodeID", nodeID, "inflightKey", key)
defer func() {
llog.InfoS("[INFO] - ControllerUnpublishVolume: Operation completed", "volumeID", volumeID, "nodeID", nodeID)
llog.InfoS("[INFO] - ControllerUnpublishVolume: Operation completed", "volumeID", volumeID, "nodeID", nodeID, "inflightKey", key)
s.inFlight.Delete(volumeID + nodeID)
}()

if volumeID == "" {
llog.Errorf("[ERROR] - ControllerUnpublishVolume: VolumeID is required")
return nil, ErrVolumeIDNotProvided
}

vol, getErr := s.cloud.GetVolume(volumeID)
if getErr != nil && getErr.IsError(lsdkErrs.EcVServerVolumeNotFound) {
llog.InfoS("[INFO] - ControllerUnpublishVolume: Volume not found", "volumeID", volumeID)
return &lcsi.ControllerUnpublishVolumeResponse{}, nil
}

if !vol.AttachedTheInstance(nodeID) {
llog.InfoS("[INFO] - ControllerUnpublishVolume: Server does not attach this volume", "volumeID", volumeID, "serverId", nodeID)
return &lcsi.ControllerUnpublishVolumeResponse{}, nil
}

err := s.cloud.DetachVolume(nodeID, volumeID)
if err != nil {
llog.ErrorS(err, "[ERROR] - ControllerUnpublishVolume: Failed to detach volume from instance", "volumeID", volumeID, "nodeID", nodeID)
if ierr := s.cloud.DetachVolume(nodeID, volumeID); ierr != nil {
llog.ErrorS(ierr.GetError(), "[ERROR] - ControllerUnpublishVolume: Failed to detach volume from instance", "volumeID", volumeID, "nodeID", nodeID)
return nil, ErrDetachVolume(volumeID, nodeID)
}

llog.V(4).InfoS("[INFO] - ControllerUnpublishVolume: volume detached from instance successfully", "volumeID", volumeID, "nodeID", nodeID)
llog.InfoS("[INFO] - ControllerUnpublishVolume: Volume detached from instance successfully", "volumeID", volumeID, "nodeID", nodeID)
return &lcsi.ControllerUnpublishVolumeResponse{}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/driver/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ func validateControllerPublishVolumeRequest(preq *lcsi.ControllerPublishVolumeRe
}

func validateControllerUnpublishVolumeRequest(req *lcsi.ControllerUnpublishVolumeRequest) error {
if len(req.GetVolumeId()) == 0 {
if len(req.GetVolumeId()) < 1 {
return ErrVolumeIDNotProvided
}

if len(req.GetNodeId()) == 0 {
if len(req.GetNodeId()) < 1 {
return ErrNodeIdNotProvided
}

Expand Down

0 comments on commit d94c529

Please sign in to comment.