mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-01 07:47:56 +00:00
prevents garbage collection from removing pinned images
This commit is contained in:
parent
4c849acea6
commit
d3ae0a381a
@ -371,6 +371,8 @@ type Image struct {
|
||||
Size int64
|
||||
// ImageSpec for the image which include annotations.
|
||||
Spec ImageSpec
|
||||
// Pin for preventing garbage collection
|
||||
Pinned bool
|
||||
}
|
||||
|
||||
// EnvVar represents the environment variable.
|
||||
|
@ -338,6 +338,16 @@ 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 {
|
||||
@ -345,6 +355,12 @@ func (im *realImageGCManager) freeSpace(bytesToFree int64, freeTime time.Time) (
|
||||
klog.V(5).InfoS("Image ID is being used", "imageID", image)
|
||||
continue
|
||||
}
|
||||
// Check if image is pinned, prevent garbage collection
|
||||
if imagesMap[image].Pinned {
|
||||
klog.V(5).InfoS("Image is pinned, skipping garbage collection", "imageID", image)
|
||||
continue
|
||||
|
||||
}
|
||||
images = append(images, evictionInfo{
|
||||
id: image,
|
||||
imageRecord: *record,
|
||||
|
@ -206,6 +206,89 @@ func TestDeleteUnusedImagesExemptSandboxImage(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestDeletePinnedImage(t *testing.T) {
|
||||
manager, fakeRuntime, _ := newRealImageGCManager(ImageGCPolicy{})
|
||||
fakeRuntime.ImageList = []container.Image{
|
||||
{
|
||||
ID: sandboxImage,
|
||||
Size: 1024,
|
||||
Pinned: true,
|
||||
},
|
||||
{
|
||||
ID: sandboxImage,
|
||||
Size: 1024,
|
||||
},
|
||||
}
|
||||
|
||||
err := manager.DeleteUnusedImages()
|
||||
assert := assert.New(t)
|
||||
assert.Len(fakeRuntime.ImageList, 2)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestDoNotDeletePinnedImage(t *testing.T) {
|
||||
manager, fakeRuntime, _ := newRealImageGCManager(ImageGCPolicy{})
|
||||
fakeRuntime.ImageList = []container.Image{
|
||||
{
|
||||
ID: "1",
|
||||
Size: 1024,
|
||||
Pinned: true,
|
||||
},
|
||||
{
|
||||
ID: "2",
|
||||
Size: 1024,
|
||||
},
|
||||
}
|
||||
|
||||
spaceFreed, err := manager.freeSpace(4096, time.Now())
|
||||
assert := assert.New(t)
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(1024, spaceFreed)
|
||||
assert.Len(fakeRuntime.ImageList, 1)
|
||||
}
|
||||
|
||||
func TestDeleteUnPinnedImage(t *testing.T) {
|
||||
manager, fakeRuntime, _ := newRealImageGCManager(ImageGCPolicy{})
|
||||
fakeRuntime.ImageList = []container.Image{
|
||||
{
|
||||
ID: "1",
|
||||
Size: 1024,
|
||||
Pinned: false,
|
||||
},
|
||||
{
|
||||
ID: "2",
|
||||
Size: 1024,
|
||||
},
|
||||
}
|
||||
|
||||
spaceFreed, err := manager.freeSpace(2048, time.Now())
|
||||
assert := assert.New(t)
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(2048, spaceFreed)
|
||||
assert.Len(fakeRuntime.ImageList, 0)
|
||||
}
|
||||
|
||||
func TestAllPinnedImages(t *testing.T) {
|
||||
manager, fakeRuntime, _ := newRealImageGCManager(ImageGCPolicy{})
|
||||
fakeRuntime.ImageList = []container.Image{
|
||||
{
|
||||
ID: "1",
|
||||
Size: 1024,
|
||||
Pinned: true,
|
||||
},
|
||||
{
|
||||
ID: "2",
|
||||
Size: 1024,
|
||||
Pinned: true,
|
||||
},
|
||||
}
|
||||
|
||||
spaceFreed, err := manager.freeSpace(2048, time.Now())
|
||||
assert := assert.New(t)
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(0, spaceFreed)
|
||||
assert.Len(fakeRuntime.ImageList, 2)
|
||||
}
|
||||
func TestDetectImagesContainerStopped(t *testing.T) {
|
||||
mockCtrl := gomock.NewController(t)
|
||||
defer mockCtrl.Finish()
|
||||
|
@ -6048,7 +6048,7 @@ type Image struct {
|
||||
// ImageSpec for image which includes annotations
|
||||
Spec *ImageSpec `protobuf:"bytes,7,opt,name=spec,proto3" json:"spec,omitempty"`
|
||||
// Recommendation on whether this image should be exempt from garbage collection.
|
||||
// It must only be treated as a recommendation--the client can still request the image be deleted,
|
||||
// It must only be treated as a recommendation -- the client can still request that the image be deleted,
|
||||
// and the runtime must oblige.
|
||||
Pinned bool `protobuf:"varint,8,opt,name=pinned,proto3" json:"pinned,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
|
@ -1248,7 +1248,7 @@ message Image {
|
||||
// ImageSpec for image which includes annotations
|
||||
ImageSpec spec = 7;
|
||||
// Recommendation on whether this image should be exempt from garbage collection.
|
||||
// It must only be treated as a recommendation--the client can still request the image be deleted,
|
||||
// It must only be treated as a recommendation -- the client can still request that the image be deleted,
|
||||
// and the runtime must oblige.
|
||||
bool pinned = 8;
|
||||
}
|
||||
|
@ -6057,7 +6057,7 @@ type Image struct {
|
||||
// ImageSpec for image which includes annotations
|
||||
Spec *ImageSpec `protobuf:"bytes,7,opt,name=spec,proto3" json:"spec,omitempty"`
|
||||
// Recommendation on whether this image should be exempt from garbage collection.
|
||||
// It must only be treated as a recommendation--the client can still request the image be deleted,
|
||||
// It must only be treated as a recommendation -- the client can still request that the image be deleted,
|
||||
// and the runtime must oblige.
|
||||
Pinned bool `protobuf:"varint,8,opt,name=pinned,proto3" json:"pinned,omitempty"`
|
||||
XXX_NoUnkeyedLiteral struct{} `json:"-"`
|
||||
|
@ -1256,7 +1256,7 @@ message Image {
|
||||
// ImageSpec for image which includes annotations
|
||||
ImageSpec spec = 7;
|
||||
// Recommendation on whether this image should be exempt from garbage collection.
|
||||
// It must only be treated as a recommendation--the client can still request the image be deleted,
|
||||
// It must only be treated as a recommendation -- the client can still request that the image be deleted,
|
||||
// and the runtime must oblige.
|
||||
bool pinned = 8;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user