policy_single_numa_node.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package topologymanager
  14. import (
  15. "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/bitmask"
  16. )
  17. type singleNumaNodePolicy struct {
  18. //List of NUMA Nodes available on the underlying machine
  19. numaNodes []int
  20. }
  21. var _ Policy = &singleNumaNodePolicy{}
  22. // PolicySingleNumaNode policy name.
  23. const PolicySingleNumaNode string = "single-numa-node"
  24. // NewSingleNumaNodePolicy returns single-numa-node policy.
  25. func NewSingleNumaNodePolicy(numaNodes []int) Policy {
  26. return &singleNumaNodePolicy{numaNodes: numaNodes}
  27. }
  28. func (p *singleNumaNodePolicy) Name() string {
  29. return PolicySingleNumaNode
  30. }
  31. func (p *singleNumaNodePolicy) canAdmitPodResult(hint *TopologyHint) bool {
  32. if !hint.Preferred {
  33. return false
  34. }
  35. return true
  36. }
  37. // Return hints that have valid bitmasks with exactly one bit set.
  38. func filterSingleNumaHints(allResourcesHints [][]TopologyHint) [][]TopologyHint {
  39. var filteredResourcesHints [][]TopologyHint
  40. for _, oneResourceHints := range allResourcesHints {
  41. var filtered []TopologyHint
  42. for _, hint := range oneResourceHints {
  43. if hint.NUMANodeAffinity == nil && hint.Preferred == true {
  44. filtered = append(filtered, hint)
  45. }
  46. if hint.NUMANodeAffinity != nil && hint.NUMANodeAffinity.Count() == 1 && hint.Preferred == true {
  47. filtered = append(filtered, hint)
  48. }
  49. }
  50. filteredResourcesHints = append(filteredResourcesHints, filtered)
  51. }
  52. return filteredResourcesHints
  53. }
  54. func (p *singleNumaNodePolicy) Merge(providersHints []map[string][]TopologyHint) (TopologyHint, bool) {
  55. filteredHints := filterProvidersHints(providersHints)
  56. // Filter to only include don't cares and hints with a single NUMA node.
  57. singleNumaHints := filterSingleNumaHints(filteredHints)
  58. bestHint := mergeFilteredHints(p.numaNodes, singleNumaHints)
  59. defaultAffinity, _ := bitmask.NewBitMask(p.numaNodes...)
  60. if bestHint.NUMANodeAffinity.IsEqual(defaultAffinity) {
  61. bestHint = TopologyHint{nil, bestHint.Preferred}
  62. }
  63. admit := p.canAdmitPodResult(&bestHint)
  64. return bestHint, admit
  65. }