Skip to content

Commit

Permalink
fix: prefetch image error when create guest with vmware template
Browse files Browse the repository at this point in the history
1. Only need the corresponding vmware template in the vcenter where the host is
located.
2. Chose storagecache which cacheimage is ready before deploying.
  • Loading branch information
rainzm committed Jul 16, 2020
1 parent 1b29a59 commit 4ef5267
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/compute/guestdrivers/esxi.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (self *SESXiGuestDriver) GetJsonDescAtHost(ctx context.Context, userCred mc
if img.ImageType != cloudprovider.CachedImageTypeSystem {
return desc
}
sciSubQ := models.StoragecachedimageManager.Query("storagecache_id").Equals("cachedimage_id", templateId).SubQuery()
sciSubQ := models.StoragecachedimageManager.Query("storagecache_id").Equals("cachedimage_id", templateId).Equals("status", api.CACHED_IMAGE_STATUS_READY).SubQuery()
scQ := models.StoragecacheManager.Query().In("id", sciSubQ)
storageCaches := make([]models.SStoragecache, 0, 1)
err = db.FetchModelObjects(models.StoragecacheManager, scQ, &storageCaches)
Expand Down
6 changes: 5 additions & 1 deletion pkg/hostman/storageman/imagecachemanager_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,11 @@ func (c *SAgentImageCacheManager) perfetchTemplateVMImageCache(ctx context.Conte
if err != nil {
return nil, err
}
_, err = host.GetTemplateVMById(data.ImageExternalId)
dc, err := host.GetDatacenter()
if err != nil {
return nil, errors.Wrap(err, "host.GetDatacenter")
}
_, err = dc.GetTemplateVMById(data.ImageExternalId)
if err != nil {
return nil, err
}
Expand Down
38 changes: 38 additions & 0 deletions pkg/multicloud/esxi/datacenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,41 @@ func (dc *SDatacenter) GetNetworks() ([]IVMNetwork, error) {
}
return dc.inetworks, nil
}

func (dc *SDatacenter) GetTemplateVMs() ([]*SVirtualMachine, error) {
hosts, err := dc.GetIHosts()
if err != nil {
return nil, errors.Wrap(err, "SDatacenter.GetIHosts")
}
templateVms := make([]*SVirtualMachine, 5)
for _, ihost := range hosts {
host := ihost.(*SHost)
tvms, err := host.GetTemplateVMs()
if err != nil {
return nil, errors.Wrap(err, "host.GetTemplateVMs")
}
templateVms = append(templateVms, tvms...)
}
return templateVms, nil
}

func (dc *SDatacenter) GetTemplateVMById(id string) (*SVirtualMachine, error) {
id = dc.manager.getPrivateId(id)
hosts, err := dc.GetIHosts()
if err != nil {
return nil, errors.Wrap(err, "SDatacenter.GetIHosts")
}
for _, ihost := range hosts {
host := ihost.(*SHost)
tvms, err := host.GetTemplateVMs()
if err != nil {
return nil, errors.Wrap(err, "host.GetTemplateVMs")
}
for i := range tvms {
if tvms[i].GetGlobalId() == id {
return tvms[i], nil
}
}
}
return nil, cloudprovider.ErrNotFound
}

0 comments on commit 4ef5267

Please sign in to comment.