Skip to content

Commit

Permalink
adds pinned field to imageRecords
Browse files Browse the repository at this point in the history
  • Loading branch information
wgahnagl committed Nov 3, 2021
1 parent d3ae0a3 commit e9766c2
Show file tree
Hide file tree
Showing 4 changed files with 688 additions and 1,386 deletions.
18 changes: 7 additions & 11 deletions pkg/kubelet/images/image_gc_manager.go
Expand Up @@ -146,6 +146,9 @@ type imageRecord struct {

// Size of the image in bytes.
size int64

// Pinned status of the image
pinned bool
}

// NewImageGCManager instantiates a new ImageGCManager object.
Expand Down Expand Up @@ -257,6 +260,9 @@ func (im *realImageGCManager) detectImages(detectTime time.Time) (sets.String, e

klog.V(5).InfoS("Image ID has size", "imageID", image.ID, "size", image.Size)
im.imageRecords[image.ID].size = image.Size

klog.V(5).InfoS("Image ID is pinned", "imageID", image.ID, "pinned", image.Pinned)
im.imageRecords[image.ID].pinned = image.Pinned
}

// Remove old images from our records.
Expand Down Expand Up @@ -338,16 +344,6 @@ func (im *realImageGCManager) freeSpace(bytesToFree int64, freeTime time.Time) (
im.imageRecordsLock.Lock()
defer im.imageRecordsLock.Unlock()

// Make the ListImages into a map to grab an image by ID
allImages, err := im.runtime.ListImages()
if err != nil {
return 0, err
}
imagesMap := make(map[string]container.Image, len(allImages))
for _, img := range allImages {
imagesMap[img.ID] = img
}

// Get all images in eviction order.
images := make([]evictionInfo, 0, len(im.imageRecords))
for image, record := range im.imageRecords {
Expand All @@ -356,7 +352,7 @@ func (im *realImageGCManager) freeSpace(bytesToFree int64, freeTime time.Time) (
continue
}
// Check if image is pinned, prevent garbage collection
if imagesMap[image].Pinned {
if record.pinned {
klog.V(5).InfoS("Image is pinned, skipping garbage collection", "imageID", image)
continue

Expand Down
21 changes: 17 additions & 4 deletions pkg/kubelet/images/image_gc_manager_test.go
Expand Up @@ -207,7 +207,10 @@ func TestDeleteUnusedImagesExemptSandboxImage(t *testing.T) {
}

func TestDeletePinnedImage(t *testing.T) {
manager, fakeRuntime, _ := newRealImageGCManager(ImageGCPolicy{})
mockCtrl := gomock.NewController(t)
mockStatsProvider := statstest.NewMockProvider(mockCtrl)

manager, fakeRuntime := newRealImageGCManager(ImageGCPolicy{}, mockStatsProvider)
fakeRuntime.ImageList = []container.Image{
{
ID: sandboxImage,
Expand All @@ -227,7 +230,10 @@ func TestDeletePinnedImage(t *testing.T) {
}

func TestDoNotDeletePinnedImage(t *testing.T) {
manager, fakeRuntime, _ := newRealImageGCManager(ImageGCPolicy{})
mockCtrl := gomock.NewController(t)
mockStatsProvider := statstest.NewMockProvider(mockCtrl)

manager, fakeRuntime := newRealImageGCManager(ImageGCPolicy{}, mockStatsProvider)
fakeRuntime.ImageList = []container.Image{
{
ID: "1",
Expand All @@ -248,7 +254,10 @@ func TestDoNotDeletePinnedImage(t *testing.T) {
}

func TestDeleteUnPinnedImage(t *testing.T) {
manager, fakeRuntime, _ := newRealImageGCManager(ImageGCPolicy{})
mockCtrl := gomock.NewController(t)
mockStatsProvider := statstest.NewMockProvider(mockCtrl)

manager, fakeRuntime := newRealImageGCManager(ImageGCPolicy{}, mockStatsProvider)
fakeRuntime.ImageList = []container.Image{
{
ID: "1",
Expand All @@ -269,7 +278,10 @@ func TestDeleteUnPinnedImage(t *testing.T) {
}

func TestAllPinnedImages(t *testing.T) {
manager, fakeRuntime, _ := newRealImageGCManager(ImageGCPolicy{})
mockCtrl := gomock.NewController(t)
mockStatsProvider := statstest.NewMockProvider(mockCtrl)

manager, fakeRuntime := newRealImageGCManager(ImageGCPolicy{}, mockStatsProvider)
fakeRuntime.ImageList = []container.Image{
{
ID: "1",
Expand All @@ -289,6 +301,7 @@ func TestAllPinnedImages(t *testing.T) {
assert.EqualValues(0, spaceFreed)
assert.Len(fakeRuntime.ImageList, 2)
}

func TestDetectImagesContainerStopped(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
Expand Down

0 comments on commit e9766c2

Please sign in to comment.