@@ -34,6 +34,7 @@ const (
3434 awsDevicePrefixWithH = "/dev/hd"
3535 awsDevicePrefixNvme = "/dev/nvme"
3636 contextTimeout = 30 * time .Second
37+ awsErrorModificationNotFound = "InvalidVolumeModification.NotFound"
3738)
3839
3940type awsOps struct {
@@ -942,37 +943,37 @@ func (s *awsOps) detachInternal(volumeID, instanceName string, options map[strin
942943 return err
943944}
944945
945- func (s * awsOps ) getVolumeModificationState (volumeID string ) (string , error ) {
946+ func isErrorModificationNotFound (err error ) bool {
947+ return strings .HasPrefix (err .Error (), awsErrorModificationNotFound )
948+ }
949+
950+ func (s * awsOps ) IsVolumesReadyToExpand (volumeIDs []* string ) (bool , error ) {
946951 modificationStateRequest := & ec2.DescribeVolumesModificationsInput {
947- VolumeIds : [] * string { & volumeID } ,
952+ VolumeIds : volumeIDs ,
948953 }
949954 describeOutput , err := s .ec2 .DescribeVolumesModifications (modificationStateRequest )
955+ states := describeOutput .VolumesModifications
950956 if err != nil {
951- return "" , err
952- }
953- volumeModifications := describeOutput . VolumesModifications
954- if len ( volumeModifications ) == 0 {
955- return " " , nil
957+ // modification state not found, this indicates no change has occurred before.
958+ if isErrorModificationNotFound ( err ) {
959+ return true , nil
960+ }
961+ return false , fmt . Errorf ( "unable to get volumes' modification states: %v " , err )
956962 }
957963
958- volumeModification := volumeModifications [len (volumeModifications )- 1 ]
959- state := * volumeModification .ModificationState
960- return state , nil
961- }
962-
963- func (s * awsOps ) IsVolumesReadyToExpand (volumeIDs []string ) (bool , error ) {
964- for i := 0 ; i < len (volumeIDs ); i ++ {
965- state , err := s .getVolumeModificationState (volumeIDs [i ])
966- if err != nil {
967- return false , fmt .Errorf ("unable to get modification state: #{err}. " )
968- }
969- // Empty string indicates there was no volume change
970- if state == "" {
971- return true , nil
964+ var state string
965+ for i := 0 ; i < len (states ); i ++ {
966+ if states [i ] == nil || states [i ].ModificationState == nil {
967+ logrus .Infof ("volume modification state is nil for volume id: %s" , state , * volumeIDs [i ])
968+ continue
972969 }
970+
971+ state = * states [i ].ModificationState
972+ logrus .Infof ("retrived volume modification state: %s for volume id: %s" , state , * volumeIDs [i ])
973973 if state == ec2 .VolumeModificationStateModifying ||
974974 state == ec2 .VolumeModificationStateOptimizing {
975- return false , fmt .Errorf ("the last modification has not fully completed. " )
975+ return false , fmt .Errorf ("aws has not fully completed the last modification: " +
976+ "volume %s is in %s state. please retry later" , * volumeIDs [i ], state )
976977 }
977978 }
978979 return true , nil
0 commit comments