@@ -95,10 +95,10 @@ func (p *DefaultProvider) GenerateTarget(ctx context.Context, class *v1alpha1.Vs
9595 poolRef := pool .Reference ()
9696 relocationSpec .Pool = & poolRef
9797 datastore , err := p .Finder .ResolveDatastore (ctx , class .Spec .DatastoreSelector )
98- dsRef := datastore .Reference ()
9998 if err != nil {
10099 return nil , err
101100 }
101+ dsRef := datastore .Reference ()
102102 relocationSpec .Datastore = & dsRef
103103
104104 return & relocationSpec , nil
@@ -208,7 +208,9 @@ func (p *DefaultProvider) Create(
208208}
209209
210210func extractCreationDate (ctx context.Context , vm * object.VirtualMachine ) (* time.Time , error ) {
211- var vmMo models.VirtualMachine
211+ vmMo := models.VirtualMachine {
212+ Config : & types.VirtualMachineConfigInfo {},
213+ }
212214 err := vm .Properties (ctx , vm .Reference (), []string {"config.createDate" }, & vmMo )
213215 if err != nil {
214216 return nil , err
@@ -223,9 +225,11 @@ func GenerateVMName(cluster, claim string) string {
223225}
224226
225227func getImageFromAnnotation (vm * object.VirtualMachine ) string {
226- var annotation models.VirtualMachine
228+ annotation := models.VirtualMachine {
229+ Config : & types.VirtualMachineConfigInfo {},
230+ }
227231 err := vm .Properties (context .Background (), vm .Reference (), []string {"config.annotation" }, & annotation )
228- if err != nil && & annotation != nil {
232+ if err != nil && annotation . Config . Annotation == "" {
229233 annotation .Config .Annotation = "image_not_found"
230234 log .Log .Info (err .Error ())
231235 }
@@ -243,15 +247,20 @@ func (p *DefaultProvider) List(ctx context.Context) ([]*Instance, error) {
243247 return instances , nil
244248 }
245249 for _ , vm := range vms {
250+ ps , err := vm .PowerState (ctx )
251+ if err != nil {
252+ log .FromContext (ctx ).Error (err , fmt .Sprintf ("failed to get power state for VM %s" , vm .Name ()))
253+ }
254+ // skip poweredOff machines
255+ if ps == "poweredOff" {
256+ continue
257+ }
246258 image := getImageFromAnnotation (vm )
247259 tags , err := p .Finder .TagsFromVM (ctx , vm )
248260 if err != nil {
249261 log .FromContext (ctx ).Error (err , fmt .Sprintf ("failed to get tags for VM %s" , vm .Name ()))
250262 }
251- ps , err := vm .PowerState (ctx )
252- if err != nil {
253- log .FromContext (ctx ).Error (err , fmt .Sprintf ("failed to get power state for VM %s" , vm .Name ()))
254- }
263+
255264 creationDate , err := extractCreationDate (ctx , vm )
256265 if err != nil {
257266 log .FromContext (ctx ).Error (err , fmt .Sprintf ("failed to extract creation date for VM %s" , vm .Name ()))
0 commit comments