debugger.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. "os"
  16. "os/signal"
  17. corelisters "k8s.io/client-go/listers/core/v1"
  18. internalcache "k8s.io/kubernetes/pkg/scheduler/internal/cache"
  19. internalqueue "k8s.io/kubernetes/pkg/scheduler/internal/queue"
  20. )
  21. // CacheDebugger provides ways to check and write cache information for debugging.
  22. type CacheDebugger struct {
  23. Comparer CacheComparer
  24. Dumper CacheDumper
  25. }
  26. // New creates a CacheDebugger.
  27. func New(
  28. nodeLister corelisters.NodeLister,
  29. podLister corelisters.PodLister,
  30. cache internalcache.Cache,
  31. podQueue internalqueue.SchedulingQueue,
  32. ) *CacheDebugger {
  33. return &CacheDebugger{
  34. Comparer: CacheComparer{
  35. NodeLister: nodeLister,
  36. PodLister: podLister,
  37. Cache: cache,
  38. PodQueue: podQueue,
  39. },
  40. Dumper: CacheDumper{
  41. cache: cache,
  42. podQueue: podQueue,
  43. },
  44. }
  45. }
  46. // ListenForSignal starts a goroutine that will trigger the CacheDebugger's
  47. // behavior when the process receives SIGINT (Windows) or SIGUSER2 (non-Windows).
  48. func (d *CacheDebugger) ListenForSignal(stopCh <-chan struct{}) {
  49. ch := make(chan os.Signal, 1)
  50. signal.Notify(ch, compareSignal)
  51. go func() {
  52. for {
  53. select {
  54. case <-stopCh:
  55. return
  56. case <-ch:
  57. d.Comparer.Compare()
  58. d.Dumper.DumpAll()
  59. }
  60. }
  61. }()
  62. }