storage_predicate.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 baremetal
  15. import (
  16. "context"
  17. "fmt" // computeapi "yunion.io/x/onecloud/pkg/apis/compute"
  18. "yunion.io/x/onecloud/pkg/compute/baremetal"
  19. "yunion.io/x/onecloud/pkg/scheduler/algorithm/predicates"
  20. "yunion.io/x/onecloud/pkg/scheduler/core"
  21. )
  22. type StoragePredicate struct {
  23. BasePredicate
  24. }
  25. func (p *StoragePredicate) Name() string {
  26. return "baremetal_storage"
  27. }
  28. func (p *StoragePredicate) Clone() core.FitPredicate {
  29. return &StoragePredicate{}
  30. }
  31. /*func toBaremetalDisks(disks []*computeapi.DiskConfig) []*baremetal.Disk {
  32. ret := make([]*baremetal.Disk, len(disks))
  33. for i, disk := range disks {
  34. ret[i] = &baremetal.Disk{
  35. Backend: disk.Backend,
  36. ImageID: disk.ImageId,
  37. Fs: &disk.Fs,
  38. Format: disk.Format,
  39. MountPoint: &disk.Mountpoint,
  40. Driver: &disk.Driver,
  41. Cache: &disk.Cache,
  42. Size: int64(disk.SizeMb),
  43. Storage: &disk.Storage,
  44. }
  45. }
  46. return ret
  47. }*/
  48. func (p *StoragePredicate) Execute(ctx context.Context, u *core.Unit, c core.Candidater) (bool, []core.PredicateFailureReason, error) {
  49. h := predicates.NewPredicateHelper(p, u, c)
  50. schedData := u.SchedData()
  51. storageInfo := c.Getter().StorageInfo()
  52. layouts, err := baremetal.CalculateLayout(
  53. schedData.BaremetalDiskConfigs,
  54. storageInfo,
  55. )
  56. if err == nil && baremetal.IsDisksAllocable(layouts, schedData.Disks) {
  57. h.SetCapacity(int64(1))
  58. } else {
  59. h.SetCapacity(int64(0))
  60. h.AppendPredicateFailMsg(fmt.Sprintf("%s err: %v", predicates.ErrNoEnoughStorage, err))
  61. }
  62. return h.GetResult()
  63. }