Merge pull request #10834 from zexi/automated-cherry-pick-of-#10833-upstream-master

Automated cherry pick of #10833: fix(region): fetch cached image from glance by default
This commit is contained in:
Zexi Li
2021-04-21 11:19:10 +08:00
committed by GitHub
4 changed files with 42 additions and 20 deletions
+30 -15
View File
@@ -28,6 +28,7 @@ import (
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/utils"
"yunion.io/x/onecloud/pkg/apis"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudcommon/cmdline"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
@@ -38,6 +39,7 @@ import (
"yunion.io/x/onecloud/pkg/compute/options"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/auth"
"yunion.io/x/onecloud/pkg/util/httputils"
"yunion.io/x/onecloud/pkg/util/k8s/tokens"
)
@@ -157,19 +159,10 @@ func (self *SKVMHostDriver) CheckAndSetCacheImage(ctx context.Context, host *mod
params := task.GetParams()
imageId, err := params.GetString("image_id")
if err != nil {
return err
return errors.Wrap(err, "Get image_id from params")
}
format, _ := params.GetString("format")
isForce := jsonutils.QueryBoolean(params, "is_force", false)
obj, err := models.CachedimageManager.FetchById(imageId)
if err != nil {
return err
}
cacheImage := obj.(*models.SCachedimage)
srcHostCacheImage, err := cacheImage.ChooseSourceStoragecacheInRange(api.HOST_TYPE_HYPERVISOR, []string{host.Id}, []interface{}{host.GetZone()})
if err != nil {
return err
}
type contentStruct struct {
ImageId string
@@ -183,17 +176,39 @@ func (self *SKVMHostDriver) CheckAndSetCacheImage(ctx context.Context, host *mod
content.ImageId = imageId
content.Format = format
if srcHostCacheImage != nil {
err = srcHostCacheImage.AddDownloadRefcount()
if options.Options.ImageCacheFromHost {
obj, err := models.CachedimageManager.FetchById(imageId)
if err != nil {
return errors.Wrapf(err, "Fetch cached image by image_id %s", imageId)
}
cacheImage := obj.(*models.SCachedimage)
srcHostCacheImage, err := cacheImage.ChooseSourceStoragecacheInRange(api.HOST_TYPE_HYPERVISOR, []string{host.Id}, []interface{}{host.GetZone()})
if err != nil {
return err
}
srcHost, err := srcHostCacheImage.GetHost()
if srcHostCacheImage != nil {
err = srcHostCacheImage.AddDownloadRefcount()
if err != nil {
return err
}
srcHost, err := srcHostCacheImage.GetHost()
if err != nil {
return err
}
content.SrcUrl = fmt.Sprintf("%s/download/images/%s", srcHost.ManagerUri, imageId)
}
} else {
// from glance service
glanceURL, err := auth.GetServiceURL(apis.SERVICE_TYPE_IMAGE, "", host.GetZone().GetName(), "")
if err != nil {
return err
return errors.Wrapf(err, "Get %s service url", apis.SERVICE_TYPE_IMAGE)
}
content.SrcUrl = fmt.Sprintf("%s/images/%s", glanceURL, imageId)
if content.Format != "" {
content.SrcUrl = fmt.Sprintf("%s?format=%s", content.SrcUrl, content.Format)
}
content.SrcUrl = fmt.Sprintf("%s/download/images/%s", srcHost.ManagerUri, imageId)
}
url := fmt.Sprintf("%s/disks/image_cache", host.ManagerUri)
if isForce {
+1
View File
@@ -50,6 +50,7 @@ type ComputeOptions struct {
LoadbalancerPendingDeleteCheckInterval int `default:"3600" help:"Interval between checks of pending deleted loadbalancer objects, defaults to 1h"`
ImageCacheStoragePolicy string `default:"least_used" choices:"best_fit|least_used" help:"Policy to choose storage for image cache, best_fit or least_used"`
ImageCacheFromHost bool `default:"false" help:"Download cached image from host"`
MetricsRetentionDays int32 `default:"30" help:"Retention days for monitoring metrics in influxdb"`
DefaultBandwidth int `default:"1000" help:"Default bandwidth"`
+2 -1
View File
@@ -26,6 +26,7 @@ import (
"yunion.io/x/log"
"yunion.io/x/onecloud/pkg/apis"
"yunion.io/x/onecloud/pkg/hostman/hostutils"
"yunion.io/x/onecloud/pkg/hostman/storageman/remotefile"
"yunion.io/x/onecloud/pkg/mcclient/auth"
@@ -174,7 +175,7 @@ func (l *SLocalImageCache) prepare(ctx context.Context, zone, srcUrl, format str
l.consumerCount++
return true, true
}
url, err := auth.GetServiceURL("image", "", zone, "")
url, err := auth.GetServiceURL(apis.SERVICE_TYPE_IMAGE, "", zone, "")
if err != nil {
log.Errorf("Failed to acquire image %s", err)
return false, true
@@ -82,14 +82,17 @@ func NewRemoteFile(
func (r *SRemoteFile) Fetch() bool {
if len(r.preChksum) > 0 {
log.Infof("Fetch remote file with precheck sum: %s", r.preChksum)
return r.fetch(r.preChksum)
} else if fileutils2.Exists(r.localPath) {
if !r.VerifyIntegrity() {
log.Warningf("Local path %s file mistmatch, refetch", r.localPath)
return r.fetch("")
} else {
return true
}
} else {
log.Infof("Fetch remote file %s to %s", r.downloadUrl, r.tmpPath)
return r.fetch("")
}
}
@@ -118,10 +121,11 @@ func (r *SRemoteFile) VerifyIntegrity() bool {
return false
}
if localChksum == r.chksum {
log.Infof("identical chksum, skip download")
log.Infof("Identical chksum, skip download")
return true
}
}
log.Warningf("Integrity mistmatch, fetch from remote")
return r.fetch("")
}
@@ -138,9 +142,10 @@ func (r *SRemoteFile) fetch(preChksum string) bool {
if fetchSucc {
if len(r.chksum) > 0 && fileutils2.Exists(r.tmpPath) {
if localChksum, err := fileutils2.MD5(r.tmpPath); err != nil {
log.Errorln(err)
log.Errorf("TmpPath %s MD5SUM error: %v", r.tmpPath, err)
fetchSucc = false
} else if r.chksum != localChksum {
log.Errorf("remote checksum %s != local checksum %s", r.chksum, localChksum)
fetchSucc = false
}
}
@@ -210,7 +215,7 @@ func (r *SRemoteFile) downloadInternal(getData bool, preChksum string) bool {
resp, err := httputils.Request(httpCli, r.ctx,
httputils.THttpMethod(method), url, header, nil, false)
if err != nil {
log.Errorln(err)
log.Errorf("Request %s to url %s error: %v", method, url, err)
return false
} else {
defer resp.Body.Close()
@@ -255,7 +260,7 @@ func (r *SRemoteFile) downloadInternal(getData bool, preChksum string) bool {
_, err = io.Copy(fi, reader)
close(finishChan)
if err != nil {
log.Errorln(err)
log.Errorf("Copy to tmpPath %s from reader error: %v", r.tmpPath, err)
return false
}
}