k8s.io/kubernetes@v1.29.3/pkg/scheduler/internal/cache/debugger/debugger.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package debugger
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  	"os/signal"
    23  
    24  	corelisters "k8s.io/client-go/listers/core/v1"
    25  	"k8s.io/klog/v2"
    26  	internalcache "k8s.io/kubernetes/pkg/scheduler/internal/cache"
    27  	internalqueue "k8s.io/kubernetes/pkg/scheduler/internal/queue"
    28  )
    29  
    30  // CacheDebugger provides ways to check and write cache information for debugging.
    31  type CacheDebugger struct {
    32  	Comparer CacheComparer
    33  	Dumper   CacheDumper
    34  }
    35  
    36  // New creates a CacheDebugger.
    37  func New(
    38  	nodeLister corelisters.NodeLister,
    39  	podLister corelisters.PodLister,
    40  	cache internalcache.Cache,
    41  	podQueue internalqueue.SchedulingQueue,
    42  ) *CacheDebugger {
    43  	return &CacheDebugger{
    44  		Comparer: CacheComparer{
    45  			NodeLister: nodeLister,
    46  			PodLister:  podLister,
    47  			Cache:      cache,
    48  			PodQueue:   podQueue,
    49  		},
    50  		Dumper: CacheDumper{
    51  			cache:    cache,
    52  			podQueue: podQueue,
    53  		},
    54  	}
    55  }
    56  
    57  // ListenForSignal starts a goroutine that will trigger the CacheDebugger's
    58  // behavior when the process receives SIGINT (Windows) or SIGUSER2 (non-Windows).
    59  func (d *CacheDebugger) ListenForSignal(ctx context.Context) {
    60  	logger := klog.FromContext(ctx)
    61  	stopCh := ctx.Done()
    62  	ch := make(chan os.Signal, 1)
    63  	signal.Notify(ch, compareSignal)
    64  
    65  	go func() {
    66  		for {
    67  			select {
    68  			case <-stopCh:
    69  				return
    70  			case <-ch:
    71  				d.Comparer.Compare(logger)
    72  				d.Dumper.DumpAll(logger)
    73  			}
    74  		}
    75  	}()
    76  }