net_bonding_predicate.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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"
  18. "yunion.io/x/onecloud/pkg/scheduler/algorithm/predicates"
  19. "yunion.io/x/onecloud/pkg/scheduler/core"
  20. )
  21. type NetBondingPredicate struct {
  22. BasePredicate
  23. }
  24. func (p *NetBondingPredicate) Name() string {
  25. return "net_bonding"
  26. }
  27. func (p *NetBondingPredicate) Clone() core.FitPredicate {
  28. return &NetBondingPredicate{}
  29. }
  30. func (p *NetBondingPredicate) PreExecute(ctx context.Context, u *core.Unit, _ []core.Candidater) (bool, error) {
  31. netConfs := u.SchedData().Networks
  32. requireTeaming := false
  33. for _, conf := range netConfs {
  34. if conf.RequireTeaming {
  35. requireTeaming = true
  36. break
  37. }
  38. }
  39. if requireTeaming {
  40. return true, nil
  41. }
  42. return false, nil
  43. }
  44. func (p *NetBondingPredicate) Execute(ctx context.Context, u *core.Unit, c core.Candidater) (bool, []core.PredicateFailureReason, error) {
  45. h := predicates.NewPredicateHelper(p, u, c)
  46. wireNetIfs := c.Getter().NetInterfaces()
  47. netConfs := u.SchedData().Networks
  48. bondingCount := make(map[string]int)
  49. for _, netConf := range netConfs {
  50. if !netConf.RequireTeaming {
  51. continue
  52. }
  53. count := 0
  54. wireId := netConf.Wire
  55. if len(wireId) == 0 && len(netConf.Network) > 0 {
  56. for _, n := range c.Getter().Networks() {
  57. if n.Id == netConf.Network || n.Name == netConf.Network {
  58. wireId = n.WireId
  59. break
  60. }
  61. }
  62. }
  63. if _, ok := bondingCount[wireId]; ok {
  64. count = bondingCount[wireId]
  65. }
  66. bondingCount[wireId] = count + 2
  67. }
  68. for wireId, count := range bondingCount {
  69. if len(wireId) > 0 {
  70. ifCount := len(wireNetIfs[wireId])
  71. if ifCount < count {
  72. h.Exclude(fmt.Sprintf("Wire %s has %d netifs, require %d, can't do bonding", wireId, ifCount, count))
  73. return h.GetResult()
  74. }
  75. } else {
  76. maxIfCount := 0
  77. for _, netIfs := range wireNetIfs {
  78. ifCount := len(netIfs)
  79. if ifCount > maxIfCount {
  80. maxIfCount = ifCount
  81. }
  82. if ifCount >= count {
  83. return h.GetResult()
  84. }
  85. }
  86. h.Exclude(fmt.Sprintf("Not enough netifs for bonding, require %d netifs, max count %d", count, maxIfCount))
  87. }
  88. }
  89. return h.GetResult()
  90. }