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

     1  /*
     2  Copyright 2015 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 cache
    18  
    19  import (
    20  	v1 "k8s.io/api/core/v1"
    21  	"k8s.io/apimachinery/pkg/util/sets"
    22  	"k8s.io/klog/v2"
    23  	"k8s.io/kubernetes/pkg/scheduler/framework"
    24  )
    25  
    26  // Cache collects pods' information and provides node-level aggregated information.
    27  // It's intended for generic scheduler to do efficient lookup.
    28  // Cache's operations are pod centric. It does incremental updates based on pod events.
    29  // Pod events are sent via network. We don't have guaranteed delivery of all events:
    30  // We use Reflector to list and watch from remote.
    31  // Reflector might be slow and do a relist, which would lead to missing events.
    32  //
    33  // State Machine of a pod's events in scheduler's cache:
    34  //
    35  //	+-------------------------------------------+  +----+
    36  //	|                            Add            |  |    |
    37  //	|                                           |  |    | Update
    38  //	+      Assume                Add            v  v    |
    39  //
    40  // Initial +--------> Assumed +------------+---> Added <--+
    41  //
    42  //	^                +   +               |       +
    43  //	|                |   |               |       |
    44  //	|                |   |           Add |       | Remove
    45  //	|                |   |               |       |
    46  //	|                |   |               +       |
    47  //	+----------------+   +-----------> Expired   +----> Deleted
    48  //	      Forget             Expire
    49  //
    50  // Note that an assumed pod can expire, because if we haven't received Add event notifying us
    51  // for a while, there might be some problems and we shouldn't keep the pod in cache anymore.
    52  //
    53  // Note that "Initial", "Expired", and "Deleted" pods do not actually exist in cache.
    54  // Based on existing use cases, we are making the following assumptions:
    55  //   - No pod would be assumed twice
    56  //   - A pod could be added without going through scheduler. In this case, we will see Add but not Assume event.
    57  //   - If a pod wasn't added, it wouldn't be removed or updated.
    58  //   - Both "Expired" and "Deleted" are valid end states. In case of some problems, e.g. network issue,
    59  //     a pod might have changed its state (e.g. added and deleted) without delivering notification to the cache.
    60  type Cache interface {
    61  	// NodeCount returns the number of nodes in the cache.
    62  	// DO NOT use outside of tests.
    63  	NodeCount() int
    64  
    65  	// PodCount returns the number of pods in the cache (including those from deleted nodes).
    66  	// DO NOT use outside of tests.
    67  	PodCount() (int, error)
    68  
    69  	// AssumePod assumes a pod scheduled and aggregates the pod's information into its node.
    70  	// The implementation also decides the policy to expire pod before being confirmed (receiving Add event).
    71  	// After expiration, its information would be subtracted.
    72  	AssumePod(logger klog.Logger, pod *v1.Pod) error
    73  
    74  	// FinishBinding signals that cache for assumed pod can be expired
    75  	FinishBinding(logger klog.Logger, pod *v1.Pod) error
    76  
    77  	// ForgetPod removes an assumed pod from cache.
    78  	ForgetPod(logger klog.Logger, pod *v1.Pod) error
    79  
    80  	// AddPod either confirms a pod if it's assumed, or adds it back if it's expired.
    81  	// If added back, the pod's information would be added again.
    82  	AddPod(logger klog.Logger, pod *v1.Pod) error
    83  
    84  	// UpdatePod removes oldPod's information and adds newPod's information.
    85  	UpdatePod(logger klog.Logger, oldPod, newPod *v1.Pod) error
    86  
    87  	// RemovePod removes a pod. The pod's information would be subtracted from assigned node.
    88  	RemovePod(logger klog.Logger, pod *v1.Pod) error
    89  
    90  	// GetPod returns the pod from the cache with the same namespace and the
    91  	// same name of the specified pod.
    92  	GetPod(pod *v1.Pod) (*v1.Pod, error)
    93  
    94  	// IsAssumedPod returns true if the pod is assumed and not expired.
    95  	IsAssumedPod(pod *v1.Pod) (bool, error)
    96  
    97  	// AddNode adds overall information about node.
    98  	// It returns a clone of added NodeInfo object.
    99  	AddNode(logger klog.Logger, node *v1.Node) *framework.NodeInfo
   100  
   101  	// UpdateNode updates overall information about node.
   102  	// It returns a clone of updated NodeInfo object.
   103  	UpdateNode(logger klog.Logger, oldNode, newNode *v1.Node) *framework.NodeInfo
   104  
   105  	// RemoveNode removes overall information about node.
   106  	RemoveNode(logger klog.Logger, node *v1.Node) error
   107  
   108  	// UpdateSnapshot updates the passed infoSnapshot to the current contents of Cache.
   109  	// The node info contains aggregated information of pods scheduled (including assumed to be)
   110  	// on this node.
   111  	// The snapshot only includes Nodes that are not deleted at the time this function is called.
   112  	// nodeinfo.Node() is guaranteed to be not nil for all the nodes in the snapshot.
   113  	UpdateSnapshot(logger klog.Logger, nodeSnapshot *Snapshot) error
   114  
   115  	// Dump produces a dump of the current cache.
   116  	Dump() *Dump
   117  }
   118  
   119  // Dump is a dump of the cache state.
   120  type Dump struct {
   121  	AssumedPods sets.Set[string]
   122  	Nodes       map[string]*framework.NodeInfo
   123  }