image_predicate.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package guest
  15. import (
  16. "context"
  17. "database/sql"
  18. "fmt"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/pkg/utils"
  21. "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. "yunion.io/x/onecloud/pkg/scheduler/algorithm/predicates"
  24. "yunion.io/x/onecloud/pkg/scheduler/core"
  25. )
  26. type ImagePredicate struct {
  27. predicates.BasePredicate
  28. cacheImage *models.SCachedimage
  29. zones []string
  30. }
  31. func (f *ImagePredicate) Name() string {
  32. return "disk_image"
  33. }
  34. func (f *ImagePredicate) Clone() core.FitPredicate {
  35. return &ImagePredicate{}
  36. }
  37. func (f *ImagePredicate) PreExecute(ctx context.Context, u *core.Unit, cs []core.Candidater) (bool, error) {
  38. if u.SchedData().ResetCpuNumaPin {
  39. return false, nil
  40. }
  41. disks := u.SchedData().Disks
  42. if len(disks) == 0 {
  43. return false, nil
  44. }
  45. imageId := disks[0].ImageId
  46. if len(imageId) == 0 || u.SchedData().PreferZone != "" {
  47. return false, nil
  48. }
  49. if !utils.IsInStringArray(u.SchedData().Provider, compute.PUBLIC_CLOUD_PROVIDERS) && !utils.IsInStringArray(u.SchedData().Provider, compute.PRIVATE_CLOUD_PROVIDERS) {
  50. return false, nil
  51. }
  52. obj, err := models.CachedimageManager.FetchById(imageId)
  53. if err != nil {
  54. // 忽略第一次上传到glance镜像后未缓存的记录
  55. if err == sql.ErrNoRows {
  56. return false, nil
  57. }
  58. return false, fmt.Errorf("Fetch CachedImage %s: %v", imageId, err)
  59. }
  60. cacheImage := obj.(*models.SCachedimage)
  61. if cloudprovider.TImageType(cacheImage.ImageType) != cloudprovider.ImageTypeSystem {
  62. return false, nil
  63. }
  64. zones, err := cacheImage.GetUsableZoneIds()
  65. if err != nil {
  66. return false, fmt.Errorf("Fetch CachedImage %s zones: %v", cacheImage.GetName(), err)
  67. }
  68. f.cacheImage = cacheImage
  69. f.zones = zones
  70. return true, nil
  71. }
  72. func (f *ImagePredicate) Execute(ctx context.Context, u *core.Unit, c core.Candidater) (bool, []core.PredicateFailureReason, error) {
  73. h := predicates.NewPredicateHelper(f, u, c)
  74. inZone := false
  75. hostZoneId := c.Getter().Zone().GetId()
  76. if utils.IsInStringArray(hostZoneId, f.zones) {
  77. inZone = true
  78. }
  79. if !inZone {
  80. h.Exclude(fmt.Sprintf("Host zone %s not in image usable zones %v", hostZoneId, f.zones))
  81. }
  82. return h.GetResult()
  83. }