sku_predicate.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 predicates
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/onecloud/pkg/scheduler/core"
  19. skuman "yunion.io/x/onecloud/pkg/scheduler/data_manager/sku"
  20. )
  21. type InstanceTypePredicate struct {
  22. BasePredicate
  23. }
  24. func (p *InstanceTypePredicate) Name() string {
  25. return "instance_type"
  26. }
  27. func (p *InstanceTypePredicate) Clone() core.FitPredicate {
  28. return &InstanceTypePredicate{}
  29. }
  30. func (p *InstanceTypePredicate) PreExecute(ctx context.Context, u *core.Unit, cs []core.Candidater) (bool, error) {
  31. driver := u.GetHypervisorDriver()
  32. if u.SchedData().ResetCpuNumaPin {
  33. return false, nil
  34. }
  35. if u.SchedData().InstanceType == "" || (driver == nil || !driver.DoScheduleSKUFilter()) {
  36. return false, nil
  37. }
  38. return true, nil
  39. }
  40. func (p *InstanceTypePredicate) Execute(ctx context.Context, u *core.Unit, c core.Candidater) (bool, []core.PredicateFailureReason, error) {
  41. h := NewPredicateHelper(p, u, c)
  42. d := u.SchedData()
  43. regionId := c.Getter().Region().Id
  44. regionName := c.Getter().Region().Name
  45. zoneId := c.Getter().Zone().Id
  46. zoneName := c.Getter().Zone().Name
  47. instanceType := d.InstanceType
  48. reqRegion := d.PreferRegion
  49. reqZone := d.PreferZone
  50. if reqRegion != "" && reqZone == "" {
  51. skus := skuman.GetByRegion(instanceType, regionId)
  52. if len(skus) == 0 {
  53. h.Exclude(fmt.Sprintf("Not found server sku %s at region %s", instanceType, regionName))
  54. } else {
  55. zoneMatch := false
  56. for idx := range skus {
  57. sku := skus[idx]
  58. if len(sku.ZoneId) == 0 || sku.ZoneId == zoneId {
  59. zoneMatch = true
  60. break
  61. }
  62. }
  63. if !zoneMatch {
  64. h.Exclude(fmt.Sprintf("Not found server sku %s at zone %s", instanceType, zoneName))
  65. }
  66. }
  67. } else {
  68. sku := skuman.GetByZone(instanceType, regionId, zoneId)
  69. if sku == nil {
  70. h.Exclude(fmt.Sprintf("Not found server sku %s at zone %s", instanceType, zoneName))
  71. }
  72. }
  73. return h.GetResult()
  74. }