From 8777eb2b6064e544b6a263568a10e7294960f33e Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Thu, 6 Jan 2022 20:31:39 +0800 Subject: [PATCH] feat(scheduler): baremetal uefi image filter --- .../predicates/baremetal/uefi_predicate.go | 81 +++++++++++++++++++ pkg/scheduler/algorithmprovider/baremetal.go | 1 + 2 files changed, 82 insertions(+) create mode 100644 pkg/scheduler/algorithm/predicates/baremetal/uefi_predicate.go diff --git a/pkg/scheduler/algorithm/predicates/baremetal/uefi_predicate.go b/pkg/scheduler/algorithm/predicates/baremetal/uefi_predicate.go new file mode 100644 index 0000000000..3ddaf6a395 --- /dev/null +++ b/pkg/scheduler/algorithm/predicates/baremetal/uefi_predicate.go @@ -0,0 +1,81 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package baremetal + +import ( + "database/sql" + "fmt" + + "yunion.io/x/pkg/errors" + + "yunion.io/x/onecloud/pkg/compute/models" + "yunion.io/x/onecloud/pkg/scheduler/algorithm/predicates" + "yunion.io/x/onecloud/pkg/scheduler/core" +) + +type UEFIImagePredicate struct { + predicates.BasePredicate + cacheImage *models.SCachedimage +} + +func (f *UEFIImagePredicate) Name() string { + return "uefi_image" +} + +func (f *UEFIImagePredicate) Clone() core.FitPredicate { + return new(UEFIImagePredicate) +} + +func (f *UEFIImagePredicate) PreExecute(u *core.Unit, cs []core.Candidater) (bool, error) { + disks := u.SchedData().Disks + if len(disks) == 0 { + return false, nil + } + imageId := disks[0].ImageId + if len(imageId) == 0 { + return false, nil + } + obj, err := models.CachedimageManager.FetchById(imageId) + if err != nil { + // 忽略第一次上传到glance镜像后未缓存的记录 + if err == sql.ErrNoRows { + return false, nil + } + return false, errors.Wrapf(err, "fetch cached image %q", imageId) + } + cacheImage := obj.(*models.SCachedimage) + f.cacheImage = cacheImage + return true, nil +} + +func (f *UEFIImagePredicate) Execute(u *core.Unit, c core.Candidater) (bool, []core.PredicateFailureReason, error) { + h := predicates.NewPredicateHelper(f, u, c) + imgName := f.cacheImage.GetName() + hostName := c.Getter().Name() + imageMsg := fmt.Sprintf("image %s is not UEFI", imgName) + hostMsg := fmt.Sprintf("host %s is not UEFI boot", hostName) + isUEFIImage := f.cacheImage.UEFI.Bool() + if isUEFIImage { + imageMsg = fmt.Sprintf("image %s is UEFI", imgName) + } + isUEFIHost := c.Getter().Host().IsUEFIBoot() + if isUEFIHost { + hostMsg = fmt.Sprintf("host %s is UEFI boot", hostName) + } + if isUEFIImage != isUEFIHost { + h.Exclude(imageMsg + " but " + hostMsg) + } + return h.GetResult() +} diff --git a/pkg/scheduler/algorithmprovider/baremetal.go b/pkg/scheduler/algorithmprovider/baremetal.go index 5724169023..960d374cf7 100644 --- a/pkg/scheduler/algorithmprovider/baremetal.go +++ b/pkg/scheduler/algorithmprovider/baremetal.go @@ -44,5 +44,6 @@ func baremetalPredicates() sets.String { factory.RegisterFitPredicate("n-CloudproviderschedtagFilter", predicates.NewCloudproviderSchedtagPredicate()), factory.RegisterFitPredicate("o-CloudregionschedtagFilter", predicates.NewCloudregionSchedtagPredicate()), factory.RegisterFitPredicate("p-ZoneschedtagFilter", predicates.NewZoneSchedtagPredicate()), + factory.RegisterFitPredicate("q-UEFIFilter", &predicatebm.UEFIImagePredicate{}), ) }