snapshot.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 cache
  14. import (
  15. "fmt"
  16. v1 "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/labels"
  18. "k8s.io/apimachinery/pkg/util/sets"
  19. schedulerlisters "k8s.io/kubernetes/pkg/scheduler/listers"
  20. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  21. )
  22. // Snapshot is a snapshot of cache NodeInfo and NodeTree order. The scheduler takes a
  23. // snapshot at the beginning of each scheduling cycle and uses it for its operations in that cycle.
  24. type Snapshot struct {
  25. // nodeInfoMap a map of node name to a snapshot of its NodeInfo.
  26. nodeInfoMap map[string]*schedulernodeinfo.NodeInfo
  27. // nodeInfoList is the list of nodes as ordered in the cache's nodeTree.
  28. nodeInfoList []*schedulernodeinfo.NodeInfo
  29. // havePodsWithAffinityNodeInfoList is the list of nodes with at least one pod declaring affinity terms.
  30. havePodsWithAffinityNodeInfoList []*schedulernodeinfo.NodeInfo
  31. generation int64
  32. }
  33. var _ schedulerlisters.SharedLister = &Snapshot{}
  34. // NewEmptySnapshot initializes a Snapshot struct and returns it.
  35. func NewEmptySnapshot() *Snapshot {
  36. return &Snapshot{
  37. nodeInfoMap: make(map[string]*schedulernodeinfo.NodeInfo),
  38. }
  39. }
  40. // NewSnapshot initializes a Snapshot struct and returns it.
  41. func NewSnapshot(pods []*v1.Pod, nodes []*v1.Node) *Snapshot {
  42. nodeInfoMap := createNodeInfoMap(pods, nodes)
  43. nodeInfoList := make([]*schedulernodeinfo.NodeInfo, 0, len(nodeInfoMap))
  44. havePodsWithAffinityNodeInfoList := make([]*schedulernodeinfo.NodeInfo, 0, len(nodeInfoMap))
  45. for _, v := range nodeInfoMap {
  46. nodeInfoList = append(nodeInfoList, v)
  47. if len(v.PodsWithAffinity()) > 0 {
  48. havePodsWithAffinityNodeInfoList = append(havePodsWithAffinityNodeInfoList, v)
  49. }
  50. }
  51. s := NewEmptySnapshot()
  52. s.nodeInfoMap = nodeInfoMap
  53. s.nodeInfoList = nodeInfoList
  54. s.havePodsWithAffinityNodeInfoList = havePodsWithAffinityNodeInfoList
  55. return s
  56. }
  57. // createNodeInfoMap obtains a list of pods and pivots that list into a map
  58. // where the keys are node names and the values are the aggregated information
  59. // for that node.
  60. func createNodeInfoMap(pods []*v1.Pod, nodes []*v1.Node) map[string]*schedulernodeinfo.NodeInfo {
  61. nodeNameToInfo := make(map[string]*schedulernodeinfo.NodeInfo)
  62. for _, pod := range pods {
  63. nodeName := pod.Spec.NodeName
  64. if _, ok := nodeNameToInfo[nodeName]; !ok {
  65. nodeNameToInfo[nodeName] = schedulernodeinfo.NewNodeInfo()
  66. }
  67. nodeNameToInfo[nodeName].AddPod(pod)
  68. }
  69. imageExistenceMap := createImageExistenceMap(nodes)
  70. for _, node := range nodes {
  71. if _, ok := nodeNameToInfo[node.Name]; !ok {
  72. nodeNameToInfo[node.Name] = schedulernodeinfo.NewNodeInfo()
  73. }
  74. nodeInfo := nodeNameToInfo[node.Name]
  75. nodeInfo.SetNode(node)
  76. nodeInfo.SetImageStates(getNodeImageStates(node, imageExistenceMap))
  77. }
  78. return nodeNameToInfo
  79. }
  80. // getNodeImageStates returns the given node's image states based on the given imageExistence map.
  81. func getNodeImageStates(node *v1.Node, imageExistenceMap map[string]sets.String) map[string]*schedulernodeinfo.ImageStateSummary {
  82. imageStates := make(map[string]*schedulernodeinfo.ImageStateSummary)
  83. for _, image := range node.Status.Images {
  84. for _, name := range image.Names {
  85. imageStates[name] = &schedulernodeinfo.ImageStateSummary{
  86. Size: image.SizeBytes,
  87. NumNodes: len(imageExistenceMap[name]),
  88. }
  89. }
  90. }
  91. return imageStates
  92. }
  93. // createImageExistenceMap returns a map recording on which nodes the images exist, keyed by the images' names.
  94. func createImageExistenceMap(nodes []*v1.Node) map[string]sets.String {
  95. imageExistenceMap := make(map[string]sets.String)
  96. for _, node := range nodes {
  97. for _, image := range node.Status.Images {
  98. for _, name := range image.Names {
  99. if _, ok := imageExistenceMap[name]; !ok {
  100. imageExistenceMap[name] = sets.NewString(node.Name)
  101. } else {
  102. imageExistenceMap[name].Insert(node.Name)
  103. }
  104. }
  105. }
  106. }
  107. return imageExistenceMap
  108. }
  109. // Pods returns a PodLister
  110. func (s *Snapshot) Pods() schedulerlisters.PodLister {
  111. return podLister(s.nodeInfoList)
  112. }
  113. // NodeInfos returns a NodeInfoLister.
  114. func (s *Snapshot) NodeInfos() schedulerlisters.NodeInfoLister {
  115. return s
  116. }
  117. // NumNodes returns the number of nodes in the snapshot.
  118. func (s *Snapshot) NumNodes() int {
  119. return len(s.nodeInfoList)
  120. }
  121. type podLister []*schedulernodeinfo.NodeInfo
  122. // List returns the list of pods in the snapshot.
  123. func (p podLister) List(selector labels.Selector) ([]*v1.Pod, error) {
  124. alwaysTrue := func(*v1.Pod) bool { return true }
  125. return p.FilteredList(alwaysTrue, selector)
  126. }
  127. // FilteredList returns a filtered list of pods in the snapshot.
  128. func (p podLister) FilteredList(filter schedulerlisters.PodFilter, selector labels.Selector) ([]*v1.Pod, error) {
  129. // podFilter is expected to return true for most or all of the pods. We
  130. // can avoid expensive array growth without wasting too much memory by
  131. // pre-allocating capacity.
  132. maxSize := 0
  133. for _, n := range p {
  134. maxSize += len(n.Pods())
  135. }
  136. pods := make([]*v1.Pod, 0, maxSize)
  137. for _, n := range p {
  138. for _, pod := range n.Pods() {
  139. if filter(pod) && selector.Matches(labels.Set(pod.Labels)) {
  140. pods = append(pods, pod)
  141. }
  142. }
  143. }
  144. return pods, nil
  145. }
  146. // List returns the list of nodes in the snapshot.
  147. func (s *Snapshot) List() ([]*schedulernodeinfo.NodeInfo, error) {
  148. return s.nodeInfoList, nil
  149. }
  150. // HavePodsWithAffinityList returns the list of nodes with at least one pods with inter-pod affinity
  151. func (s *Snapshot) HavePodsWithAffinityList() ([]*schedulernodeinfo.NodeInfo, error) {
  152. return s.havePodsWithAffinityNodeInfoList, nil
  153. }
  154. // Get returns the NodeInfo of the given node name.
  155. func (s *Snapshot) Get(nodeName string) (*schedulernodeinfo.NodeInfo, error) {
  156. if v, ok := s.nodeInfoMap[nodeName]; ok && v.Node() != nil {
  157. return v, nil
  158. }
  159. return nil, fmt.Errorf("nodeinfo not found for node name %q", nodeName)
  160. }