status_predicate.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. "yunion.io/x/pkg/util/sets"
  18. api "yunion.io/x/onecloud/pkg/apis/compute"
  19. "yunion.io/x/onecloud/pkg/scheduler/algorithm/predicates"
  20. "yunion.io/x/onecloud/pkg/scheduler/core"
  21. )
  22. var (
  23. ExpectedStatus = sets.NewString(api.BAREMETAL_RUNNING, api.BAREMETAL_START_CONVERT, api.BAREMETAL_CONVERTING)
  24. )
  25. type StatusPredicate struct {
  26. BasePredicate
  27. }
  28. func (p *StatusPredicate) Name() string {
  29. return "baremetal_status"
  30. }
  31. func (p *StatusPredicate) Clone() core.FitPredicate {
  32. return &StatusPredicate{}
  33. }
  34. func (p *StatusPredicate) Execute(ctx context.Context, u *core.Unit, c core.Candidater) (bool, []core.PredicateFailureReason, error) {
  35. h := predicates.NewPredicateHelper(p, u, c)
  36. getter := c.Getter()
  37. status := getter.Status()
  38. enabled := getter.Enabled()
  39. if !ExpectedStatus.Has(status) {
  40. h.Exclude2("status", status, ExpectedStatus)
  41. return h.GetResult()
  42. }
  43. if !enabled {
  44. h.Exclude2("enable_status", "disable", "enable")
  45. return h.GetResult()
  46. }
  47. if getter.IsEmpty() {
  48. h.SetCapacity(1)
  49. } else {
  50. h.AppendPredicateFailMsg(predicates.ErrBaremetalHasAlreadyBeenOccupied)
  51. h.SetCapacity(0)
  52. }
  53. return h.GetResult()
  54. }