Skip to content

Commit a9ec924

Browse files
committed
Update imageLocality plugin
to account for ImageVolume images when scoring and prioritizing nodes with required pod images Signed-off-by: bmordeha <bmordeha@redhat.com>
1 parent 931ad2a commit a9ec924

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

pkg/scheduler/framework/plugins/imagelocality/image_locality.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323

2424
v1 "k8s.io/api/core/v1"
2525
"k8s.io/apimachinery/pkg/runtime"
26+
utilfeature "k8s.io/apiserver/pkg/util/feature"
27+
"k8s.io/kubernetes/pkg/features"
2628
"k8s.io/kubernetes/pkg/scheduler/framework"
2729
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/names"
2830
)
@@ -92,7 +94,8 @@ func calculatePriority(sumScores int64, numContainers int) int64 {
9294
return framework.MaxNodeScore * (sumScores - minThreshold) / (maxThreshold - minThreshold)
9395
}
9496

95-
// sumImageScores returns the sum of image scores of all the containers that are already on the node.
97+
// sumImageScores returns the sum of image scores of all the containers, including init containers and
98+
// imageVolumes specified in the pod's spec, that are already on the node.
9699
// Each image receives a raw score of its size, scaled by scaledImageScore. The raw scores are later used to calculate
97100
// the final score.
98101
func sumImageScores(nodeInfo *framework.NodeInfo, pod *v1.Pod, totalNumNodes int) int64 {
@@ -107,6 +110,16 @@ func sumImageScores(nodeInfo *framework.NodeInfo, pod *v1.Pod, totalNumNodes int
107110
sum += scaledImageScore(state, totalNumNodes)
108111
}
109112
}
113+
if utilfeature.DefaultFeatureGate.Enabled(features.ImageVolume) {
114+
for _, volume := range pod.Spec.Volumes {
115+
if volume.Image == nil {
116+
continue
117+
}
118+
if state, ok := nodeInfo.ImageStates[normalizedImageName(volume.Image.Reference)]; ok {
119+
sum += scaledImageScore(state, totalNumNodes)
120+
}
121+
}
122+
}
110123
return sum
111124
}
112125

pkg/scheduler/framework/plugins/imagelocality/image_locality_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,24 @@ func TestImageLocalityPriority(t *testing.T) {
9292
},
9393
}
9494

95+
testImageVolume := v1.PodSpec{
96+
Containers: []v1.Container{
97+
{
98+
Image: "gcr.io/30",
99+
},
100+
},
101+
Volumes: []v1.Volume{
102+
{
103+
Name: "imageVolume",
104+
VolumeSource: v1.VolumeSource{
105+
Image: &v1.ImageVolumeSource{
106+
Reference: "gcr.io/300",
107+
},
108+
},
109+
},
110+
},
111+
}
112+
95113
test30Init300 := v1.PodSpec{
96114
Containers: []v1.Container{
97115
{
@@ -340,6 +358,21 @@ func TestImageLocalityPriority(t *testing.T) {
340358
expectedList: []framework.NodeScore{{Name: "node1", Score: 1}, {Name: "node2", Score: 0}},
341359
name: "pod with multiple small images",
342360
},
361+
{
362+
// Pod: gcr.io/300 gcr.io/30
363+
364+
// Node1
365+
// Image: gcr.io/300:latest 300MB
366+
// Score: 100 * (300M * 1/2 - 23M) / (1000M - 23M) = 12
367+
368+
// Node2
369+
// Image: gcr.io/30:latest 30MB
370+
// Score: 100 * (30M - 23M) / (1000M - 23M) = 0
371+
pod: &v1.Pod{Spec: testImageVolume},
372+
nodes: []*v1.Node{makeImageNode("node1", node300600900), makeImageNode("node2", node400030)},
373+
expectedList: []framework.NodeScore{{Name: "node1", Score: 12}, {Name: "node2", Score: 0}},
374+
name: "pod with ImageVolume",
375+
},
343376
{
344377
// Pod: gcr.io/30 InitContainers: gcr.io/300
345378

0 commit comments

Comments
 (0)