dumper.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "fmt"
  16. "strings"
  17. "k8s.io/klog"
  18. "k8s.io/api/core/v1"
  19. internalcache "k8s.io/kubernetes/pkg/scheduler/internal/cache"
  20. "k8s.io/kubernetes/pkg/scheduler/internal/queue"
  21. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  22. )
  23. // CacheDumper writes some information from the scheduler cache and the scheduling queue to the
  24. // scheduler logs for debugging purposes.
  25. type CacheDumper struct {
  26. cache internalcache.Cache
  27. podQueue queue.SchedulingQueue
  28. }
  29. // DumpAll writes cached nodes and scheduling queue information to the scheduler logs.
  30. func (d *CacheDumper) DumpAll() {
  31. d.dumpNodes()
  32. d.dumpSchedulingQueue()
  33. }
  34. // dumpNodes writes NodeInfo to the scheduler logs.
  35. func (d *CacheDumper) dumpNodes() {
  36. dump := d.cache.Dump()
  37. klog.Info("Dump of cached NodeInfo")
  38. for _, nodeInfo := range dump.Nodes {
  39. klog.Info(d.printNodeInfo(nodeInfo))
  40. }
  41. }
  42. // dumpSchedulingQueue writes pods in the scheduling queue to the scheduler logs.
  43. func (d *CacheDumper) dumpSchedulingQueue() {
  44. pendingPods := d.podQueue.PendingPods()
  45. var podData strings.Builder
  46. for _, p := range pendingPods {
  47. podData.WriteString(printPod(p))
  48. }
  49. klog.Infof("Dump of scheduling queue:\n%s", podData.String())
  50. }
  51. // printNodeInfo writes parts of NodeInfo to a string.
  52. func (d *CacheDumper) printNodeInfo(n *schedulernodeinfo.NodeInfo) string {
  53. var nodeData strings.Builder
  54. nodeData.WriteString(fmt.Sprintf("\nNode name: %+v\nRequested Resources: %+v\nAllocatable Resources:%+v\nScheduled Pods(number: %v):\n",
  55. n.Node().Name, n.RequestedResource(), n.AllocatableResource(), len(n.Pods())))
  56. // Dumping Pod Info
  57. for _, p := range n.Pods() {
  58. nodeData.WriteString(printPod(p))
  59. }
  60. // Dumping nominated pods info on the node
  61. nominatedPods := d.podQueue.NominatedPodsForNode(n.Node().Name)
  62. if len(nominatedPods) != 0 {
  63. nodeData.WriteString(fmt.Sprintf("Nominated Pods(number: %v):\n", len(nominatedPods)))
  64. for _, p := range nominatedPods {
  65. nodeData.WriteString(printPod(p))
  66. }
  67. }
  68. return nodeData.String()
  69. }
  70. // printPod writes parts of a Pod object to a string.
  71. func printPod(p *v1.Pod) string {
  72. return fmt.Sprintf("name: %v, namespace: %v, uid: %v, phase: %v, nominated node: %v\n", p.Name, p.Namespace, p.UID, p.Status.Phase, p.Status.NominatedNodeName)
  73. }