2018-01-09 18:57:14 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 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.
|
|
|
|
*/
|
|
|
|
|
2018-07-18 14:47:22 +00:00
|
|
|
package cache
|
2018-01-09 18:57:14 +00:00
|
|
|
|
2018-11-26 18:23:56 +00:00
|
|
|
import (
|
|
|
|
"k8s.io/api/core/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
|
|
|
)
|
2018-01-09 18:57:14 +00:00
|
|
|
|
|
|
|
// CreateNodeNameToInfoMap 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.
|
|
|
|
func CreateNodeNameToInfoMap(pods []*v1.Pod, nodes []*v1.Node) map[string]*NodeInfo {
|
|
|
|
nodeNameToInfo := make(map[string]*NodeInfo)
|
|
|
|
for _, pod := range pods {
|
|
|
|
nodeName := pod.Spec.NodeName
|
|
|
|
if _, ok := nodeNameToInfo[nodeName]; !ok {
|
|
|
|
nodeNameToInfo[nodeName] = NewNodeInfo()
|
|
|
|
}
|
|
|
|
nodeNameToInfo[nodeName].AddPod(pod)
|
|
|
|
}
|
2018-11-26 18:23:56 +00:00
|
|
|
imageExistenceMap := createImageExistenceMap(nodes)
|
|
|
|
|
2018-01-09 18:57:14 +00:00
|
|
|
for _, node := range nodes {
|
|
|
|
if _, ok := nodeNameToInfo[node.Name]; !ok {
|
|
|
|
nodeNameToInfo[node.Name] = NewNodeInfo()
|
|
|
|
}
|
2018-11-26 18:23:56 +00:00
|
|
|
nodeInfo := nodeNameToInfo[node.Name]
|
|
|
|
nodeInfo.SetNode(node)
|
|
|
|
nodeInfo.imageStates = getNodeImageStates(node, imageExistenceMap)
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
|
|
|
return nodeNameToInfo
|
|
|
|
}
|
2018-11-26 18:23:56 +00:00
|
|
|
|
|
|
|
// getNodeImageStates returns the given node's image states based on the given imageExistence map.
|
|
|
|
func getNodeImageStates(node *v1.Node, imageExistenceMap map[string]sets.String) map[string]*ImageStateSummary {
|
|
|
|
imageStates := make(map[string]*ImageStateSummary)
|
|
|
|
|
|
|
|
for _, image := range node.Status.Images {
|
|
|
|
for _, name := range image.Names {
|
|
|
|
imageStates[name] = &ImageStateSummary{
|
|
|
|
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
|
|
|
|
}
|