fix: ensure s3 client init for s3 storage driver (#24319)

Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
This commit is contained in:
Jian Qiu
2026-02-11 22:08:19 +08:00
committed by GitHub
parent ecb65c2563
commit c6d5c9ea0a
2 changed files with 31 additions and 6 deletions

View File

@@ -15,12 +15,16 @@
package options
import (
"strings"
"yunion.io/x/log"
common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
"yunion.io/x/onecloud/pkg/cloudcommon/pending_delete"
)
type SImageOptions struct {
common_options.HostCommonOptions
common_options.HostCommonOptions `"s3_bucket_name->default":"onecloud-images" "s3_bucket_lifecycle_keep_day->default":"0"`
common_options.DBOptions
@@ -81,5 +85,22 @@ func OnOptionsChange(oldO, newO interface{}) bool {
}
func (opt SImageOptions) HasValidS3Options() bool {
return len(opt.S3Endpoint) > 0 && len(opt.S3AccessKey) > 0 && len(opt.S3SecretKey) > 0 && len(opt.S3BucketName) > 0
msg := []string{}
if len(opt.S3Endpoint) <= 0 {
msg = append(msg, "s3_endpoint is required")
}
if len(opt.S3AccessKey) <= 0 {
msg = append(msg, "s3_access_key is required")
}
if len(opt.S3SecretKey) <= 0 {
msg = append(msg, "s3_secret_key is required")
}
if len(opt.S3BucketName) <= 0 {
msg = append(msg, "s3_bucket_name is required")
}
if len(msg) > 0 {
log.Errorf("invalid s3 options: %s", strings.Join(msg, ", "))
return false
}
return true
}

View File

@@ -147,6 +147,11 @@ func StartService() {
if options.Options.HasValidS3Options() {
initS3()
log.Infof("init s3 client success")
} else if options.Options.StorageDriver == api.IMAGE_STORAGE_DRIVER_S3 {
log.Fatalf("storage driver is s3, but s3 options are not valid")
} else {
log.Infof("storage driver is not s3 and no valid s3 options, skip init s3 client")
}
// check image after s3 mounted
models.CheckImages(app.GetContext())
@@ -215,10 +220,9 @@ func initS3() {
if err != nil {
log.Fatalf("failed init s3 client %s", err)
}
if options.Options.S3BucketName == "onecloud-screendump" {
if err = s3.SetBucketLifecycle(""); err != nil {
log.Warningf("remove onecloud-screendump lifecycle %s", err)
}
// clear glance bucket lifecycle definiton
if err = s3.SetBucketLifecycle(""); err != nil {
log.Warningf("remove onecloud-screendump lifecycle %s", err)
}
func() {