comparer.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Copyright 2018 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 debugger
  14. import (
  15. "sort"
  16. "strings"
  17. "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/labels"
  19. corelisters "k8s.io/client-go/listers/core/v1"
  20. "k8s.io/klog"
  21. internalcache "k8s.io/kubernetes/pkg/scheduler/internal/cache"
  22. internalqueue "k8s.io/kubernetes/pkg/scheduler/internal/queue"
  23. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  24. )
  25. // CacheComparer is an implementation of the Scheduler's cache comparer.
  26. type CacheComparer struct {
  27. NodeLister corelisters.NodeLister
  28. PodLister corelisters.PodLister
  29. Cache internalcache.Cache
  30. PodQueue internalqueue.SchedulingQueue
  31. }
  32. // Compare compares the nodes and pods of NodeLister with Cache.Snapshot.
  33. func (c *CacheComparer) Compare() error {
  34. klog.V(3).Info("cache comparer started")
  35. defer klog.V(3).Info("cache comparer finished")
  36. nodes, err := c.NodeLister.List(labels.Everything())
  37. if err != nil {
  38. return err
  39. }
  40. pods, err := c.PodLister.List(labels.Everything())
  41. if err != nil {
  42. return err
  43. }
  44. snapshot := c.Cache.Snapshot()
  45. pendingPods := c.PodQueue.PendingPods()
  46. if missed, redundant := c.CompareNodes(nodes, snapshot.Nodes); len(missed)+len(redundant) != 0 {
  47. klog.Warningf("cache mismatch: missed nodes: %s; redundant nodes: %s", missed, redundant)
  48. }
  49. if missed, redundant := c.ComparePods(pods, pendingPods, snapshot.Nodes); len(missed)+len(redundant) != 0 {
  50. klog.Warningf("cache mismatch: missed pods: %s; redundant pods: %s", missed, redundant)
  51. }
  52. return nil
  53. }
  54. // CompareNodes compares actual nodes with cached nodes.
  55. func (c *CacheComparer) CompareNodes(nodes []*v1.Node, nodeinfos map[string]*schedulernodeinfo.NodeInfo) (missed, redundant []string) {
  56. actual := []string{}
  57. for _, node := range nodes {
  58. actual = append(actual, node.Name)
  59. }
  60. cached := []string{}
  61. for nodeName := range nodeinfos {
  62. cached = append(cached, nodeName)
  63. }
  64. return compareStrings(actual, cached)
  65. }
  66. // ComparePods compares actual pods with cached pods.
  67. func (c *CacheComparer) ComparePods(pods, waitingPods []*v1.Pod, nodeinfos map[string]*schedulernodeinfo.NodeInfo) (missed, redundant []string) {
  68. actual := []string{}
  69. for _, pod := range pods {
  70. actual = append(actual, string(pod.UID))
  71. }
  72. cached := []string{}
  73. for _, nodeinfo := range nodeinfos {
  74. for _, pod := range nodeinfo.Pods() {
  75. cached = append(cached, string(pod.UID))
  76. }
  77. }
  78. for _, pod := range waitingPods {
  79. cached = append(cached, string(pod.UID))
  80. }
  81. return compareStrings(actual, cached)
  82. }
  83. func compareStrings(actual, cached []string) (missed, redundant []string) {
  84. missed, redundant = []string{}, []string{}
  85. sort.Strings(actual)
  86. sort.Strings(cached)
  87. compare := func(i, j int) int {
  88. if i == len(actual) {
  89. return 1
  90. } else if j == len(cached) {
  91. return -1
  92. }
  93. return strings.Compare(actual[i], cached[j])
  94. }
  95. for i, j := 0, 0; i < len(actual) || j < len(cached); {
  96. switch compare(i, j) {
  97. case 0:
  98. i++
  99. j++
  100. case -1:
  101. missed = append(missed, actual[i])
  102. i++
  103. case 1:
  104. redundant = append(redundant, cached[j])
  105. j++
  106. }
  107. }
  108. return
  109. }