From f999db28ca5e75c4f753e9192f20abff25f77ca8 Mon Sep 17 00:00:00 2001 From: ioito Date: Sat, 6 May 2023 14:46:55 +0800 Subject: [PATCH] fix(glance): support filter image by os archs --- pkg/apis/image/image.go | 5 +++++ pkg/apis/list.go | 5 +++++ pkg/cloudcommon/db/archs.go | 2 ++ pkg/image/models/image_guest.go | 22 ++++++++++++---------- pkg/image/models/images.go | 1 + pkg/mcclient/options/glance/image.go | 8 ++++++++ 6 files changed, 33 insertions(+), 10 deletions(-) diff --git a/pkg/apis/image/image.go b/pkg/apis/image/image.go index a3b5fc7c72..b6e61e5c7a 100644 --- a/pkg/apis/image/image.go +++ b/pkg/apis/image/image.go @@ -49,6 +49,11 @@ type ImageListInput struct { // 操作系统精确匹配 OsTypePreciseMatch bool `json:"os_type_precise_match"` + // 操作系统架构, 可能值为: arm, x86 等 + OsArchs []string `json:"os_archs"` + // 操作系统架构精确匹配 + OsArchPreciseMatch bool `json:"os_arch_precise_match"` + // 发行版本,可能值为: CentOS, Ubuntu, Debian, ArchLinux, OpenEuler 等 Distributions []string `json:"distributions"` // 发行版精确匹配 diff --git a/pkg/apis/list.go b/pkg/apis/list.go index 0825de85f6..846ac33048 100644 --- a/pkg/apis/list.go +++ b/pkg/apis/list.go @@ -347,6 +347,11 @@ type EnabledStatusInfrasResourceBaseListInput struct { } type MultiArchResourceBaseListInput struct { + // 通过操作系统架构过滤 + // x86会过滤出os_arch为空或os_arch=i386或以x86开头的资源 + // arm会过滤出os_arch=aarch64或os_arch=aarch32或者以arm开头的资源 + // 其他的输入会过滤出以输入字符开头的资源 + // enmu: x86, arm OsArch string `json:"os_arch"` } diff --git a/pkg/cloudcommon/db/archs.go b/pkg/cloudcommon/db/archs.go index 873874242d..3e6a3fc697 100644 --- a/pkg/cloudcommon/db/archs.go +++ b/pkg/cloudcommon/db/archs.go @@ -48,11 +48,13 @@ func ListQueryByArchitecture(q *sqlchemy.SQuery, fieldKey string, arch string) * if arch == apis.OS_ARCH_X86 { q = q.Filter(sqlchemy.OR( sqlchemy.Startswith(q.Field(fieldKey), arch), + sqlchemy.Equals(q.Field(fieldKey), apis.OS_ARCH_I386), sqlchemy.IsNullOrEmpty(q.Field(fieldKey)), )) } else if arch == apis.OS_ARCH_ARM { q = q.Filter(sqlchemy.OR( sqlchemy.Startswith(q.Field(fieldKey), arch), + sqlchemy.Equals(q.Field(fieldKey), apis.OS_ARCH_AARCH32), sqlchemy.Equals(q.Field(fieldKey), apis.OS_ARCH_AARCH64), )) } else { diff --git a/pkg/image/models/image_guest.go b/pkg/image/models/image_guest.go index 949f33e7cc..a7f6431e47 100644 --- a/pkg/image/models/image_guest.go +++ b/pkg/image/models/image_guest.go @@ -411,18 +411,20 @@ func (self *SGuestImage) getMoreDetails(ctx context.Context, userCred mcclient.T return dataImages[i].Name < dataImages[j].Name }) out.Size = size - out.RootImage = *rootImage out.DataImages = dataImages - // properties of root image - properties, err := ImagePropertyManager.GetProperties(rootImage.ID) - if err != nil { - return out + if rootImage != nil { + out.RootImage = *rootImage + // properties of root image + properties, err := ImagePropertyManager.GetProperties(rootImage.ID) + if err != nil { + return out + } + propJson := jsonutils.NewDict() + for k, v := range properties { + propJson.Add(jsonutils.NewString(v), k) + } + out.Properties = propJson } - propJson := jsonutils.NewDict() - for k, v := range properties { - propJson.Add(jsonutils.NewString(v), k) - } - out.Properties = propJson out.DisableDelete = self.Protected.Bool() return out } diff --git a/pkg/image/models/images.go b/pkg/image/models/images.go index cb6bf6b395..2c2a3e34f1 100644 --- a/pkg/image/models/images.go +++ b/pkg/image/models/images.go @@ -1326,6 +1326,7 @@ func (manager *SImageManager) ListItemFilter( propSq := propQ.SubQuery() q = q.Join(propSq, sqlchemy.Equals(q.Field("id"), propSq.Field("image_id"))).Distinct() } + propFilter([]string{api.IMAGE_OS_ARCH}, query.OsArchs, query.OsArchPreciseMatch) propFilter([]string{api.IMAGE_OS_TYPE}, query.OsTypes, query.OsTypePreciseMatch) propFilter([]string{api.IMAGE_OS_DISTRO, "distro"}, query.Distributions, query.DistributionPreciseMatch) diff --git a/pkg/mcclient/options/glance/image.go b/pkg/mcclient/options/glance/image.go index 90da75be4a..4948ba14e1 100644 --- a/pkg/mcclient/options/glance/image.go +++ b/pkg/mcclient/options/glance/image.go @@ -32,6 +32,8 @@ type ImageListOptions struct { Name string `help:"Name filter"` OsType []string `help:"Type of OS filter e.g. 'Windows, Linux, Freebsd, Android, macOS, VMWare'"` OsTypePreciseMatch bool `help:"OS precise match"` + OsArch []string `help:"Type of OS arch filter e.g. 'x86, arm, arm64, x86_64'"` + OsArchPreciseMatch bool `help:"OS arch precise match"` Distribution []string `help:"Distribution filter, e.g. 'CentOS, Ubuntu, Debian, Windows'"` DistributionPreciseMatch bool `help:"Distribution precise match"` } @@ -75,6 +77,12 @@ func (o *ImageListOptions) Params() (jsonutils.JSONObject, error) { if o.OsTypePreciseMatch { params.Add(jsonutils.JSONTrue, "os_type_precise_match") } + if len(o.OsArch) > 0 { + params.Add(jsonutils.NewStringArray(o.OsArch), "os_archs") + } + if o.OsArchPreciseMatch { + params.Add(jsonutils.JSONTrue, "os_arch_precise_match") + } if len(o.Distribution) > 0 { params.Add(jsonutils.NewStringArray(o.Distribution), "distributions") }