Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated cherry pick of #20605: fix(glance): sync service options after vmware detected #20607

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions pkg/cloudcommon/options/mergeconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"yunion.io/x/onecloud/pkg/mcclient/modules/identity"
)

func getServiceIdByType(s *mcclient.ClientSession, typeStr string, verStr string) (string, error) {
func GetServiceIdByType(s *mcclient.ClientSession, typeStr string, verStr string) (string, error) {
params := jsonutils.NewDict()
if len(verStr) > 0 {
typeStr += "_" + verStr
Expand All @@ -47,7 +47,7 @@ func getServiceIdByType(s *mcclient.ClientSession, typeStr string, verStr string
return result.Data[0].GetString("id")
}

func getServiceConfig(s *mcclient.ClientSession, serviceId string) (jsonutils.JSONObject, error) {
func GetServiceConfig(s *mcclient.ClientSession, serviceId string) (jsonutils.JSONObject, error) {
conf, err := identity.ServicesV3.GetSpecific(s, serviceId, "config", nil)
if err != nil {
return nil, errors.Wrap(err, "modules.ServicesV3.GetSpecific config")
Expand Down Expand Up @@ -81,11 +81,11 @@ func (s *mcclientServiceConfigSession) Merge(opts interface{}, serviceType strin
// epType, _ := s.config.GetString("session_endpoint_type")
s.session = auth.GetAdminSession(context.Background(), region)
if len(serviceType) > 0 {
s.serviceId, _ = getServiceIdByType(s.session, serviceType, serviceVersion)
s.serviceId, _ = GetServiceIdByType(s.session, serviceType, serviceVersion)
if len(s.serviceId) > 0 {
serviceConf, err := getServiceConfig(s.session, s.serviceId)
serviceConf, err := GetServiceConfig(s.session, s.serviceId)
if err != nil {
log.Errorf("getServiceConfig for %s failed: %s", serviceType, err)
log.Errorf("GetServiceConfig for %s failed: %s", serviceType, err)
} else if serviceConf != nil {
s.config.Update(serviceConf)
merged = true
Expand All @@ -95,11 +95,11 @@ func (s *mcclientServiceConfigSession) Merge(opts interface{}, serviceType strin
}
}
}
s.commonServiceId, _ = getServiceIdByType(s.session, consts.COMMON_SERVICE, "")
s.commonServiceId, _ = GetServiceIdByType(s.session, consts.COMMON_SERVICE, "")
if len(s.commonServiceId) > 0 {
commonConf, err := getServiceConfig(s.session, s.commonServiceId)
commonConf, err := GetServiceConfig(s.session, s.commonServiceId)
if err != nil {
log.Errorf("getServiceConfig for %s failed: %s", consts.COMMON_SERVICE, err)
log.Errorf("GetServiceConfig for %s failed: %s", consts.COMMON_SERVICE, err)
} else if commonConf != nil {
s.config.Update(commonConf)
merged = true
Expand Down
38 changes: 37 additions & 1 deletion pkg/image/models/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"yunion.io/x/onecloud/pkg/cloudcommon/db/quotas"
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
"yunion.io/x/onecloud/pkg/cloudcommon/notifyclient"
common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
deployapi "yunion.io/x/onecloud/pkg/hostman/hostdeployer/apis"
"yunion.io/x/onecloud/pkg/hostman/hostdeployer/deployclient"
"yunion.io/x/onecloud/pkg/httperrors"
Expand Down Expand Up @@ -1659,10 +1660,45 @@ func (img *SImage) PerformChangeOwner(ctx context.Context, userCred mcclient.Tok
return ret, nil
}

func UpdateImageConfigTargetImageFormats(ctx context.Context, userCred mcclient.TokenCredential) error {
s := auth.GetSession(ctx, userCred, options.Options.Region)
serviceId, err := common_options.GetServiceIdByType(s, api.SERVICE_TYPE, "")
if err != nil {
return errors.Wrap(err, "get service id")
}

defConf, err := common_options.GetServiceConfig(s, serviceId)
if err != nil {
return errors.Wrap(err, "GetServiceConfig")
}
targetFormats := make([]string, 0)
err = defConf.Unmarshal(&targetFormats, "target_image_formats")
if err != nil {
return errors.Wrap(err, "get target_image_formats")
}
if !utils.IsInStringArray(string(qemuimgfmt.VMDK), targetFormats) {
targetFormats = append(targetFormats, string(qemuimgfmt.VMDK))
}
defConfDict := defConf.(*jsonutils.JSONDict)
defConfDict.Set("target_image_formats", jsonutils.NewStringArray(targetFormats))
nconf := jsonutils.NewDict()
nconf.Add(defConfDict, "config", "default")
_, err = identity_modules.ServicesV3.PerformAction(s, serviceId, "config", nconf)
if err != nil {
return errors.Wrap(err, "fail to save config")
}
return nil
}

func (m *SImageManager) PerformVmwareAccountAdded(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformChangeProjectOwnerInput) (jsonutils.JSONObject, error) {
log.Infof("perform vmware account added")

if !utils.IsInStringArray(string(qemuimgfmt.VMDK), options.Options.TargetImageFormats) {
options.Options.TargetImageFormats = append(options.Options.TargetImageFormats, string(qemuimgfmt.VMDK))
if err := UpdateImageConfigTargetImageFormats(ctx, userCred); err != nil {
log.Errorf("failed update target_image_formats %s", err)
} else {
options.Options.TargetImageFormats = append(options.Options.TargetImageFormats, string(qemuimgfmt.VMDK))
}
}
return nil, nil
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/image/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ func StartService() {
log.Errorf("failed get vmware cloudaccounts")
} else if ok {
if !utils.IsInStringArray(string(qemuimgfmt.VMDK), options.Options.TargetImageFormats) {
options.Options.TargetImageFormats = append(options.Options.TargetImageFormats, string(qemuimgfmt.VMDK))
if err = models.UpdateImageConfigTargetImageFormats(context.Background(), auth.AdminCredential()); err != nil {
log.Errorf("failed update target_image_formats %s", err)
} else {
options.Options.TargetImageFormats = append(options.Options.TargetImageFormats, string(qemuimgfmt.VMDK))
}
}
}

Expand Down
Loading