status_predicate.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. "yunion.io/x/pkg/utils"
  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. const (
  23. ExpectedStatus = "running"
  24. ExpectedHostStatus = "online"
  25. ExpectedEnableStatus = "enable"
  26. )
  27. // StatusPredicate is to filter the current state of host is available,
  28. // not available host's capacity will be set to 0 and filtered out.
  29. type StatusPredicate struct {
  30. predicates.BasePredicate
  31. }
  32. func (p *StatusPredicate) Name() string {
  33. return "host_status"
  34. }
  35. func (p *StatusPredicate) Clone() core.FitPredicate {
  36. return &StatusPredicate{}
  37. }
  38. func (p *StatusPredicate) Execute(ctx context.Context, u *core.Unit, c core.Candidater) (bool, []core.PredicateFailureReason, error) {
  39. h := predicates.NewPredicateHelper(p, u, c)
  40. getter := c.Getter()
  41. curStatus := getter.Status()
  42. curHostStatus := getter.HostStatus()
  43. curEnableStatus := getter.Enabled()
  44. if curStatus != ExpectedStatus {
  45. h.Exclude2("status", curStatus, ExpectedStatus)
  46. }
  47. if curHostStatus != ExpectedHostStatus {
  48. h.Exclude2("host_status", curHostStatus, ExpectedHostStatus)
  49. }
  50. if !curEnableStatus {
  51. h.Exclude2("enable_status", curEnableStatus, true)
  52. }
  53. zone := getter.Zone()
  54. if zone.Status != ExpectedEnableStatus {
  55. h.Exclude2("zone_status", zone.Status, ExpectedEnableStatus)
  56. }
  57. cloudprovider := getter.Cloudprovider()
  58. if cloudprovider != nil {
  59. if !utils.IsInStringArray(cloudprovider.Status, api.CLOUD_PROVIDER_VALID_STATUS) {
  60. h.Exclude2("cloud_provider_status", cloudprovider.Status, api.CLOUD_PROVIDER_VALID_STATUS)
  61. }
  62. if !utils.IsInStringArray(cloudprovider.HealthStatus, api.CLOUD_PROVIDER_VALID_HEALTH_STATUS) {
  63. h.Exclude2("cloud_provider_health_status", cloudprovider.HealthStatus, api.CLOUD_PROVIDER_VALID_HEALTH_STATUS)
  64. }
  65. if !cloudprovider.Enabled.Bool() {
  66. h.Exclude2("cloud_provider_enable", cloudprovider.Enabled.Bool(), true)
  67. }
  68. }
  69. return h.GetResult()
  70. }