@@ -151,7 +151,10 @@ func ApplyVirtualMachineSpec(
151151 // Attach ephemeral disk for storage: Kubernetes.
152152 // Attach containerDisk for storage: ContainerRegistry (i.e. image from DVCR).
153153
154- vi := viByName [bd .Name ]
154+ vi , ok := viByName [bd .Name ]
155+ if ! ok || vi == nil {
156+ return fmt .Errorf ("unexpected error: virtual image %q should exist in the cluster; please recreate it" , bd .Name )
157+ }
155158
156159 name := GenerateVIDiskName (bd .Name )
157160 switch vi .Spec .Storage {
@@ -183,7 +186,10 @@ func ApplyVirtualMachineSpec(
183186 case v1alpha2 .ClusterImageDevice :
184187 // ClusterVirtualImage is attached as containerDisk.
185188
186- cvi := cviByName [bd .Name ]
189+ cvi , ok := cviByName [bd .Name ]
190+ if ! ok || cvi == nil {
191+ return fmt .Errorf ("unexpected error: cluster virtual image %q should exist in the cluster; please recreate it" , bd .Name )
192+ }
187193
188194 name := GenerateCVIDiskName (bd .Name )
189195 if err := kvvm .SetDisk (name , SetDiskOptions {
@@ -199,7 +205,10 @@ func ApplyVirtualMachineSpec(
199205 case v1alpha2 .DiskDevice :
200206 // VirtualDisk is attached as a regular disk.
201207
202- vd := vdByName [bd .Name ]
208+ vd , ok := vdByName [bd .Name ]
209+ if ! ok || vd == nil {
210+ return fmt .Errorf ("unexpected error: virtual disk %q should exist in the cluster; please recreate it" , bd .Name )
211+ }
203212
204213 pvcName := vd .Status .Target .PersistentVolumeClaim
205214 // VirtualDisk doesn't have pvc yet: wait for pvc and reconcile again.
0 commit comments