k3s/vendor/k8s.io/kubernetes/pkg/scheduler/internal/cache/snapshot.go

152 lines
5.0 KiB
Go
Raw Normal View History

2019-12-12 01:27:03 +00:00
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2020-03-26 21:07:15 +00:00
package cache
2019-12-12 01:27:03 +00:00
import (
"fmt"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
2020-08-10 17:43:49 +00:00
framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
2019-12-12 01:27:03 +00:00
)
// Snapshot is a snapshot of cache NodeInfo and NodeTree order. The scheduler takes a
// snapshot at the beginning of each scheduling cycle and uses it for its operations in that cycle.
type Snapshot struct {
2020-03-26 21:07:15 +00:00
// nodeInfoMap a map of node name to a snapshot of its NodeInfo.
2020-08-10 17:43:49 +00:00
nodeInfoMap map[string]*framework.NodeInfo
2020-03-26 21:07:15 +00:00
// nodeInfoList is the list of nodes as ordered in the cache's nodeTree.
2020-08-10 17:43:49 +00:00
nodeInfoList []*framework.NodeInfo
2020-03-26 21:07:15 +00:00
// havePodsWithAffinityNodeInfoList is the list of nodes with at least one pod declaring affinity terms.
2020-08-10 17:43:49 +00:00
havePodsWithAffinityNodeInfoList []*framework.NodeInfo
2020-03-26 21:07:15 +00:00
generation int64
2019-12-12 01:27:03 +00:00
}
2020-08-10 17:43:49 +00:00
var _ framework.SharedLister = &Snapshot{}
2019-12-12 01:27:03 +00:00
// NewEmptySnapshot initializes a Snapshot struct and returns it.
func NewEmptySnapshot() *Snapshot {
return &Snapshot{
2020-08-10 17:43:49 +00:00
nodeInfoMap: make(map[string]*framework.NodeInfo),
2019-12-12 01:27:03 +00:00
}
}
// NewSnapshot initializes a Snapshot struct and returns it.
2020-03-26 21:07:15 +00:00
func NewSnapshot(pods []*v1.Pod, nodes []*v1.Node) *Snapshot {
nodeInfoMap := createNodeInfoMap(pods, nodes)
2020-08-10 17:43:49 +00:00
nodeInfoList := make([]*framework.NodeInfo, 0, len(nodeInfoMap))
havePodsWithAffinityNodeInfoList := make([]*framework.NodeInfo, 0, len(nodeInfoMap))
2019-12-12 01:27:03 +00:00
for _, v := range nodeInfoMap {
nodeInfoList = append(nodeInfoList, v)
2020-08-10 17:43:49 +00:00
if len(v.PodsWithAffinity) > 0 {
2019-12-12 01:27:03 +00:00
havePodsWithAffinityNodeInfoList = append(havePodsWithAffinityNodeInfoList, v)
}
}
s := NewEmptySnapshot()
2020-03-26 21:07:15 +00:00
s.nodeInfoMap = nodeInfoMap
s.nodeInfoList = nodeInfoList
s.havePodsWithAffinityNodeInfoList = havePodsWithAffinityNodeInfoList
2019-12-12 01:27:03 +00:00
return s
}
2020-03-26 21:07:15 +00:00
// createNodeInfoMap obtains a list of pods and pivots that list into a map
// where the keys are node names and the values are the aggregated information
// for that node.
2020-08-10 17:43:49 +00:00
func createNodeInfoMap(pods []*v1.Pod, nodes []*v1.Node) map[string]*framework.NodeInfo {
nodeNameToInfo := make(map[string]*framework.NodeInfo)
2019-12-12 01:27:03 +00:00
for _, pod := range pods {
nodeName := pod.Spec.NodeName
if _, ok := nodeNameToInfo[nodeName]; !ok {
2020-08-10 17:43:49 +00:00
nodeNameToInfo[nodeName] = framework.NewNodeInfo()
2019-12-12 01:27:03 +00:00
}
nodeNameToInfo[nodeName].AddPod(pod)
}
imageExistenceMap := createImageExistenceMap(nodes)
for _, node := range nodes {
if _, ok := nodeNameToInfo[node.Name]; !ok {
2020-08-10 17:43:49 +00:00
nodeNameToInfo[node.Name] = framework.NewNodeInfo()
2019-12-12 01:27:03 +00:00
}
nodeInfo := nodeNameToInfo[node.Name]
nodeInfo.SetNode(node)
2020-08-10 17:43:49 +00:00
nodeInfo.ImageStates = getNodeImageStates(node, imageExistenceMap)
2019-12-12 01:27:03 +00:00
}
return nodeNameToInfo
}
// getNodeImageStates returns the given node's image states based on the given imageExistence map.
2020-08-10 17:43:49 +00:00
func getNodeImageStates(node *v1.Node, imageExistenceMap map[string]sets.String) map[string]*framework.ImageStateSummary {
imageStates := make(map[string]*framework.ImageStateSummary)
2019-12-12 01:27:03 +00:00
for _, image := range node.Status.Images {
for _, name := range image.Names {
2020-08-10 17:43:49 +00:00
imageStates[name] = &framework.ImageStateSummary{
2019-12-12 01:27:03 +00:00
Size: image.SizeBytes,
NumNodes: len(imageExistenceMap[name]),
}
}
}
return imageStates
}
// createImageExistenceMap returns a map recording on which nodes the images exist, keyed by the images' names.
func createImageExistenceMap(nodes []*v1.Node) map[string]sets.String {
imageExistenceMap := make(map[string]sets.String)
for _, node := range nodes {
for _, image := range node.Status.Images {
for _, name := range image.Names {
if _, ok := imageExistenceMap[name]; !ok {
imageExistenceMap[name] = sets.NewString(node.Name)
} else {
imageExistenceMap[name].Insert(node.Name)
}
}
}
}
return imageExistenceMap
}
// NodeInfos returns a NodeInfoLister.
2020-08-10 17:43:49 +00:00
func (s *Snapshot) NodeInfos() framework.NodeInfoLister {
2020-03-26 21:07:15 +00:00
return s
2019-12-12 01:27:03 +00:00
}
2020-03-26 21:07:15 +00:00
// NumNodes returns the number of nodes in the snapshot.
func (s *Snapshot) NumNodes() int {
return len(s.nodeInfoList)
2019-12-12 01:27:03 +00:00
}
// List returns the list of nodes in the snapshot.
2020-08-10 17:43:49 +00:00
func (s *Snapshot) List() ([]*framework.NodeInfo, error) {
2020-03-26 21:07:15 +00:00
return s.nodeInfoList, nil
2019-12-12 01:27:03 +00:00
}
// HavePodsWithAffinityList returns the list of nodes with at least one pods with inter-pod affinity
2020-08-10 17:43:49 +00:00
func (s *Snapshot) HavePodsWithAffinityList() ([]*framework.NodeInfo, error) {
2020-03-26 21:07:15 +00:00
return s.havePodsWithAffinityNodeInfoList, nil
2019-12-12 01:27:03 +00:00
}
2020-03-26 21:07:15 +00:00
// Get returns the NodeInfo of the given node name.
2020-08-10 17:43:49 +00:00
func (s *Snapshot) Get(nodeName string) (*framework.NodeInfo, error) {
2020-03-26 21:07:15 +00:00
if v, ok := s.nodeInfoMap[nodeName]; ok && v.Node() != nil {
2019-12-12 01:27:03 +00:00
return v, nil
}
return nil, fmt.Errorf("nodeinfo not found for node name %q", nodeName)
}