@@ -90,59 +90,44 @@ const (
9090func (h * BlockDeviceHandler ) getStatusMessage (diskState virtualDisksState , vds map [string ]* virtv2.VirtualDisk ) string {
9191 summaryCount := len (vds )
9292
93- if summaryCount == 1 {
94- var diskName string
95- for _ , vd := range vds {
96- diskName = vd .Name
97- }
98-
99- return h .getSingleDiskMessage (diskState , diskName )
100- }
101- return h .getMultipleDisksMessage (diskState , summaryCount )
102- }
103-
104- func (h * BlockDeviceHandler ) getSingleDiskMessage (diskState virtualDisksState , diskName string ) string {
105- switch {
106- case diskState .counts .creatingImage == 1 :
107- return h .getDiskUsageMessage (1 , 1 , diskState .diskNames .imageCreation , UsageTypeImageCreation ) + "."
108- case diskState .counts .onOtherVMs == 1 :
109- return h .getDiskUsageMessage (1 , 1 , diskState .diskNames .usedByOtherVM , UsageTypeAnotherVM ) + "."
110- default :
111- return fmt .Sprintf ("Waiting for block device %q to be ready to use." , diskName )
112- }
113- }
114-
115- func (h * BlockDeviceHandler ) getMultipleDisksMessage (diskState virtualDisksState , summaryCount int ) string {
11693 var messages []string
11794
11895 messages = append (messages , fmt .Sprintf (
11996 "Waiting for block devices to be ready to use: %d/%d" ,
12097 diskState .counts .readyToUse , summaryCount ))
12198
122- if diskState .counts .creatingImage > 0 {
123- messages = append (messages , h .getDiskUsageMessage (
124- diskState .counts .creatingImage ,
125- summaryCount ,
126- diskState .diskNames .imageCreation ,
127- UsageTypeImageCreation ))
128- }
99+ addUsageMessage := func (count int , name string , usageType string ) {
100+ if count == 0 {
101+ return
102+ }
103+
104+ var msg string
105+ if count == 1 {
106+ msg = fmt .Sprintf ("Virtual disk %q is in use %s" , name , usageType )
107+ } else {
108+ msg = fmt .Sprintf ("Virtual disks %d/%d are in use %s" , count , summaryCount , usageType )
109+ }
129110
130- if diskState .counts .onOtherVMs > 0 {
131- messages = append (messages , h .getDiskUsageMessage (
132- diskState .counts .onOtherVMs ,
133- summaryCount ,
134- diskState .diskNames .usedByOtherVM ,
135- UsageTypeAnotherVM ))
111+ messages = append (messages , msg )
136112 }
137113
138- return strings . Join ( messages , "; " ) + "."
139- }
114+ addUsageMessage ( diskState . counts . creatingImage , diskState . diskNames . imageCreation , UsageTypeImageCreation )
115+ addUsageMessage ( diskState . counts . onOtherVMs , diskState . diskNames . usedByOtherVM , UsageTypeAnotherVM )
140116
141- func (h * BlockDeviceHandler ) getDiskUsageMessage (count , total int , diskName string , usageType string ) string {
142- if count == 1 {
143- return fmt .Sprintf ("Virtual disk %q is in use %s" , diskName , usageType )
117+ if summaryCount == 1 {
118+ if diskState .counts .creatingImage == 1 || diskState .counts .onOtherVMs == 1 {
119+ return messages [len (messages )- 1 ] + "."
120+ }
121+
122+ var diskName string
123+ for _ , vd := range vds {
124+ diskName = vd .Name
125+ }
126+
127+ return fmt .Sprintf ("Waiting for block device %q to be ready to use." , diskName )
144128 }
145- return fmt .Sprintf ("Virtual disks %d/%d are in use %s" , count , total , usageType )
129+
130+ return strings .Join (messages , "; " ) + "."
146131}
147132
148133func (h * BlockDeviceHandler ) setConditionReady (vm * virtv2.VirtualMachine ) {
@@ -202,7 +187,7 @@ func (h *BlockDeviceHandler) handleAttachedDisk(
202187 state * virtualDisksState ,
203188) {
204189 if condition .Status == metav1 .ConditionTrue && condition .Reason == vdcondition .AttachedToVirtualMachine .String () {
205- if ! h .checkVDToUseCurrentVM (vd , vm ) {
190+ if ! h .checkVMToMountVD (vd , vm ) {
206191 state .counts .onOtherVMs ++
207192 state .diskNames .usedByOtherVM = vd .Name
208193 } else {
@@ -219,12 +204,24 @@ func (h *BlockDeviceHandler) handleReadyForUseDisk(
219204) {
220205 if condition .Status != metav1 .ConditionTrue &&
221206 vm .Status .Phase == virtv2 .MachineStopped &&
222- h .checkVDToUseCurrentVM (vd , vm ) {
207+ h .checkVDToUseVM (vd , vm ) {
223208 state .counts .readyToUse ++
224209 }
225210}
226211
227- func (h * BlockDeviceHandler ) checkVDToUseCurrentVM (vd * virtv2.VirtualDisk , vm * virtv2.VirtualMachine ) bool {
212+ func (h * BlockDeviceHandler ) checkVDToUseVM (vd * virtv2.VirtualDisk , vm * virtv2.VirtualMachine ) bool {
213+ attachedVMs := vd .Status .AttachedToVirtualMachines
214+
215+ for _ , attachedVM := range attachedVMs {
216+ if attachedVM .Name == vm .Name {
217+ return true
218+ }
219+ }
220+
221+ return false
222+ }
223+
224+ func (h * BlockDeviceHandler ) checkVMToMountVD (vd * virtv2.VirtualDisk , vm * virtv2.VirtualMachine ) bool {
228225 attachedVMs := vd .Status .AttachedToVirtualMachines
229226
230227 for _ , attachedVM := range attachedVMs {
0 commit comments