From 032a8863204c670e24b39a8f2889d659397c9ce6 Mon Sep 17 00:00:00 2001 From: Harry Zhang Date: Sun, 8 May 2016 12:41:23 -0400 Subject: [PATCH] Only store top N images in status --- pkg/kubelet/kubelet.go | 18 ++++++++++++++++++ pkg/kubelet/kubelet_test.go | 25 +++++++++++++------------ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index 49884621646..714e0280ac6 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -151,6 +151,9 @@ const ( // Maximum period to wait for pod volume setup operations maxWaitForVolumeOps = 20 * time.Minute + + // maxImagesInStatus is the number of max images we store in image status. + maxImagesInNodeStatus = 50 ) // SyncHandler is an interface implemented by Kubelet, for testability @@ -3096,6 +3099,12 @@ func (kl *Kubelet) setNodeStatusImages(node *api.Node) { if err != nil { glog.Errorf("Error getting image list: %v", err) } else { + // sort the images from max to min, and only set top N images into the node status. + sort.Sort(ByImageSize(containerImages)) + if maxImagesInNodeStatus < len(containerImages) { + containerImages = containerImages[0 : maxImagesInNodeStatus-1] + } + for _, image := range containerImages { imagesOnNode = append(imagesOnNode, api.ContainerImage{ Names: append(image.RepoTags, image.RepoDigests...), @@ -3112,6 +3121,15 @@ func (kl *Kubelet) setNodeStatusGoRuntime(node *api.Node) { node.Status.NodeInfo.Architecture = goRuntime.GOARCH } +type ByImageSize []kubecontainer.Image + +// Sort from max to min +func (a ByImageSize) Less(i, j int) bool { + return a[i].Size > a[j].Size +} +func (a ByImageSize) Len() int { return len(a) } +func (a ByImageSize) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + // Set status for the node. func (kl *Kubelet) setNodeStatusInfo(node *api.Node) { kl.setNodeStatusMachineInfo(node) diff --git a/pkg/kubelet/kubelet_test.go b/pkg/kubelet/kubelet_test.go index 9955269eed0..f93ef5b0c16 100644 --- a/pkg/kubelet/kubelet_test.go +++ b/pkg/kubelet/kubelet_test.go @@ -2435,14 +2435,14 @@ func TestUpdateNewNodeStatus(t *testing.T) { {Type: api.NodeInternalIP, Address: "127.0.0.1"}, }, Images: []api.ContainerImage{ - { - Names: []string{"gcr.io/google_containers:v1", "gcr.io/google_containers:v2"}, - SizeBytes: 123, - }, { Names: []string{"gcr.io/google_containers:v3", "gcr.io/google_containers:v4"}, SizeBytes: 456, }, + { + Names: []string{"gcr.io/google_containers:v1", "gcr.io/google_containers:v2"}, + SizeBytes: 123, + }, }, }, } @@ -2685,15 +2685,16 @@ func TestUpdateExistingNodeStatus(t *testing.T) { {Type: api.NodeLegacyHostIP, Address: "127.0.0.1"}, {Type: api.NodeInternalIP, Address: "127.0.0.1"}, }, + // images will be sorted from max to min in node status. Images: []api.ContainerImage{ - { - Names: []string{"gcr.io/google_containers:v1", "gcr.io/google_containers:v2"}, - SizeBytes: 123, - }, { Names: []string{"gcr.io/google_containers:v3", "gcr.io/google_containers:v4"}, SizeBytes: 456, }, + { + Names: []string{"gcr.io/google_containers:v1", "gcr.io/google_containers:v2"}, + SizeBytes: 123, + }, }, }, } @@ -2969,14 +2970,14 @@ func TestUpdateNodeStatusWithRuntimeStateError(t *testing.T) { {Type: api.NodeInternalIP, Address: "127.0.0.1"}, }, Images: []api.ContainerImage{ - { - Names: []string{"gcr.io/google_containers:v1", "gcr.io/google_containers:v2"}, - SizeBytes: 123, - }, { Names: []string{"gcr.io/google_containers:v3", "gcr.io/google_containers:v4"}, SizeBytes: 456, }, + { + Names: []string{"gcr.io/google_containers:v1", "gcr.io/google_containers:v2"}, + SizeBytes: 123, + }, }, }, }