hypervisor_predicate.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/log"
  18. computeapi "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. "yunion.io/x/onecloud/pkg/scheduler/data_manager/schedtag"
  22. )
  23. const (
  24. CONTAINER_ALLOWED_TAG = "container"
  25. )
  26. // HypervisorPredicate is to select candidates match guest hyperviosr
  27. // runtime
  28. type HypervisorPredicate struct {
  29. predicates.BasePredicate
  30. }
  31. func (f *HypervisorPredicate) Name() string {
  32. return "host_hypervisor_runtime"
  33. }
  34. func (f *HypervisorPredicate) Clone() core.FitPredicate {
  35. return &HypervisorPredicate{}
  36. }
  37. func hostHasContainerTag(c core.Candidater) bool {
  38. aggs := schedtag.GetCandidateSchedtags("hosts", c.Getter().Host().GetId())
  39. for _, agg := range aggs {
  40. if agg.GetName() == CONTAINER_ALLOWED_TAG {
  41. return true
  42. }
  43. }
  44. return false
  45. }
  46. func hostAllowRunContainer(c core.Candidater) bool {
  47. getter := c.Getter()
  48. hostType := getter.HostType()
  49. if hostType == computeapi.HOST_TYPE_CONTAINER {
  50. return true
  51. }
  52. if hostHasContainerTag(c) {
  53. log.Debugf("Host %q has %q tag, allow it run container", c.IndexKey(), CONTAINER_ALLOWED_TAG)
  54. return true
  55. }
  56. return false
  57. }
  58. func (f *HypervisorPredicate) Execute(ctx context.Context, u *core.Unit, c core.Candidater) (bool, []core.PredicateFailureReason, error) {
  59. h := predicates.NewPredicateHelper(f, u, c)
  60. hostType := c.Getter().HostType()
  61. guestNeedType := u.SchedData().Hypervisor
  62. if guestNeedType != hostType {
  63. if guestNeedType == computeapi.HYPERVISOR_POD && hostAllowRunContainer(c) {
  64. return h.GetResult()
  65. }
  66. h.Exclude2(f.Name(), hostType, guestNeedType)
  67. }
  68. return h.GetResult()
  69. }