From c5a731daa9717a8033778b0fe4ba6d59cc8b68b4 Mon Sep 17 00:00:00 2001 From: rainzm Date: Fri, 14 Aug 2020 20:34:05 +0800 Subject: [PATCH] fix: GetIImages correctly without 'image_cache' dir After creating vms, there is no dir named 'image_cache' exists in storage of esxi. So handle this case to not affecte the synchronization of the VMware Template. --- pkg/multicloud/esxi/storagecache.go | 35 ++++++++++++++++------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/pkg/multicloud/esxi/storagecache.go b/pkg/multicloud/esxi/storagecache.go index 6d0da0df2f..30edb99c2f 100644 --- a/pkg/multicloud/esxi/storagecache.go +++ b/pkg/multicloud/esxi/storagecache.go @@ -21,6 +21,7 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" + "yunion.io/x/pkg/errors" "yunion.io/x/onecloud/pkg/cloudprovider" "yunion.io/x/onecloud/pkg/mcclient" @@ -77,15 +78,31 @@ func (self *SDatastoreImageCache) GetPath() string { func (self *SDatastoreImageCache) GetIImages() ([]cloudprovider.ICloudImage, error) { ctx := context.Background() + ret := make([]cloudprovider.ICloudImage, 0, 2) + + // get vm template with only one disk + tems, err := self.host.GetTemplateVMs() + if err != nil { + log.Errorf("fail to get templateVMs of host '%s' in SDatastoreImageCache.GetIImages", self.host.GetName()) + return ret, nil + } + for _, tem := range tems { + // for now, add vm template with only one disk as cachedimage + if len(tem.vdisks) != 1 { + continue + } + ret = append(ret, NewVMTemplate(tem, self)) + } files, err := self.datastore.ListDir(ctx, IMAGE_CACHE_DIR_NAME) + if errors.Cause(err) == errors.ErrNotFound { + return ret, nil + } if err != nil { log.Errorf("GetIImages ListDir fail %s", err) return nil, err } - ret := make([]cloudprovider.ICloudImage, 0) - validFilenames := make(map[string]bool) for i := 0; i < len(files); i += 1 { @@ -119,20 +136,6 @@ func (self *SDatastoreImageCache) GetIImages() ([]cloudprovider.ICloudImage, err } } - // get vm template with only one disk - tems, err := self.host.GetTemplateVMs() - if err != nil { - log.Errorf("fail to get templateVMs of host '%s' in SDatastoreImageCache.GetIImages", self.host.GetName()) - return ret, nil - } - for _, tem := range tems { - // for now, add vm template with only one disk as cachedimage - if len(tem.vdisks) != 1 { - continue - } - ret = append(ret, NewVMTemplate(tem, self)) - } - return ret, nil }