mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Cache image history
This commit is contained in:
parent
d2a45f0ba5
commit
56bde2df9f
@ -18,54 +18,85 @@ package dockertools
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
|
|
||||||
dockertypes "github.com/docker/engine-api/types"
|
dockertypes "github.com/docker/engine-api/types"
|
||||||
runtime "k8s.io/kubernetes/pkg/kubelet/container"
|
runtime "k8s.io/kubernetes/pkg/kubelet/container"
|
||||||
"k8s.io/kubernetes/pkg/util/sets"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// imageStatsProvider exposes stats about all images currently available.
|
// imageStatsProvider exposes stats about all images currently available.
|
||||||
type imageStatsProvider struct {
|
type imageStatsProvider struct {
|
||||||
|
sync.Mutex
|
||||||
|
// layers caches the current layers, key is the layer ID.
|
||||||
|
layers map[string]*dockertypes.ImageHistory
|
||||||
|
// imageToLayerIDs maps image to its layer IDs.
|
||||||
|
imageToLayerIDs map[string][]string
|
||||||
// Docker remote API client
|
// Docker remote API client
|
||||||
c DockerInterface
|
c DockerInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newImageStatsProvider(c DockerInterface) *imageStatsProvider {
|
||||||
|
return &imageStatsProvider{
|
||||||
|
layers: make(map[string]*dockertypes.ImageHistory),
|
||||||
|
imageToLayerIDs: make(map[string][]string),
|
||||||
|
c: c,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (isp *imageStatsProvider) ImageStats() (*runtime.ImageStats, error) {
|
func (isp *imageStatsProvider) ImageStats() (*runtime.ImageStats, error) {
|
||||||
images, err := isp.c.ListImages(dockertypes.ImageListOptions{})
|
images, err := isp.c.ListImages(dockertypes.ImageListOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to list docker images - %v", err)
|
return nil, fmt.Errorf("failed to list docker images - %v", err)
|
||||||
}
|
}
|
||||||
// A map of all the image layers to its corresponding size.
|
// Take the lock to protect the cache
|
||||||
imageMap := sets.NewString()
|
isp.Lock()
|
||||||
ret := &runtime.ImageStats{}
|
defer isp.Unlock()
|
||||||
|
// Create new cache each time, this is a little more memory consuming, but:
|
||||||
|
// * ImageStats is only called every 10 seconds
|
||||||
|
// * We use pointers and reference to copy cache elements.
|
||||||
|
// The memory usage should be acceptable.
|
||||||
|
// TODO(random-liu): Add more logic to implement in place cache update.
|
||||||
|
newLayers := make(map[string]*dockertypes.ImageHistory)
|
||||||
|
newImageToLayerIDs := make(map[string][]string)
|
||||||
for _, image := range images {
|
for _, image := range images {
|
||||||
// Get information about the various layers of each docker image.
|
layerIDs, ok := isp.imageToLayerIDs[image.ID]
|
||||||
history, err := isp.c.ImageHistory(image.ID)
|
if !ok {
|
||||||
if err != nil {
|
// Get information about the various layers of the given docker image.
|
||||||
glog.V(2).Infof("failed to get history of docker image %v - %v", image, err)
|
history, err := isp.c.ImageHistory(image.ID)
|
||||||
continue
|
if err != nil {
|
||||||
}
|
// Skip the image and inspect again in next ImageStats if the image is still there
|
||||||
// Store size information of each layer.
|
glog.V(2).Infof("failed to get history of docker image %+v - %v", image, err)
|
||||||
for _, layer := range history {
|
|
||||||
// Skip empty layers.
|
|
||||||
if layer.Size == 0 {
|
|
||||||
glog.V(10).Infof("skipping image layer %v with size 0", layer)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
key := layer.ID
|
// Cache each layer
|
||||||
// Some of the layers are empty.
|
for i := range history {
|
||||||
// We are hoping that these layers are unique to each image.
|
layer := &history[i]
|
||||||
// Still keying with the CreatedBy field to be safe.
|
key := layer.ID
|
||||||
if key == "" || key == "<missing>" {
|
// Some of the layers are empty.
|
||||||
key = key + layer.CreatedBy
|
// We are hoping that these layers are unique to each image.
|
||||||
|
// Still keying with the CreatedBy field to be safe.
|
||||||
|
if key == "" || key == "<missing>" {
|
||||||
|
key = key + layer.CreatedBy
|
||||||
|
}
|
||||||
|
layerIDs = append(layerIDs, key)
|
||||||
|
newLayers[key] = layer
|
||||||
}
|
}
|
||||||
if !imageMap.Has(key) {
|
} else {
|
||||||
ret.TotalStorageBytes += uint64(layer.Size)
|
for _, layerID := range layerIDs {
|
||||||
|
newLayers[layerID] = isp.layers[layerID]
|
||||||
}
|
}
|
||||||
imageMap.Insert(key)
|
|
||||||
}
|
}
|
||||||
|
newImageToLayerIDs[image.ID] = layerIDs
|
||||||
}
|
}
|
||||||
|
ret := &runtime.ImageStats{}
|
||||||
|
// Calculate the total storage bytes
|
||||||
|
for _, layer := range newLayers {
|
||||||
|
ret.TotalStorageBytes += uint64(layer.Size)
|
||||||
|
}
|
||||||
|
// Update current cache
|
||||||
|
isp.layers = newLayers
|
||||||
|
isp.imageToLayerIDs = newImageToLayerIDs
|
||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
@ -254,7 +254,7 @@ func NewDockerManager(
|
|||||||
cpuCFSQuota: cpuCFSQuota,
|
cpuCFSQuota: cpuCFSQuota,
|
||||||
enableCustomMetrics: enableCustomMetrics,
|
enableCustomMetrics: enableCustomMetrics,
|
||||||
configureHairpinMode: hairpinMode,
|
configureHairpinMode: hairpinMode,
|
||||||
imageStatsProvider: &imageStatsProvider{client},
|
imageStatsProvider: newImageStatsProvider(client),
|
||||||
seccompProfileRoot: seccompProfileRoot,
|
seccompProfileRoot: seccompProfileRoot,
|
||||||
}
|
}
|
||||||
dm.runner = lifecycle.NewHandlerRunner(httpClient, dm, dm)
|
dm.runner = lifecycle.NewHandlerRunner(httpClient, dm, dm)
|
||||||
|
Loading…
Reference in New Issue
Block a user