File tree Expand file tree Collapse file tree 3 files changed +14
-17
lines changed Expand file tree Collapse file tree 3 files changed +14
-17
lines changed Original file line number Diff line number Diff line change @@ -113,21 +113,26 @@ type imageCache struct {
113
113
images []container.Image
114
114
}
115
115
116
- // set updates image cache.
116
+ // set sorts the input list and updates image cache.
117
+ // 'i' takes ownership of the list, you should not reference the list again
118
+ // after calling this function.
117
119
func (i * imageCache ) set (images []container.Image ) {
118
120
i .Lock ()
119
121
defer i .Unlock ()
122
+ // The image list needs to be sorted when it gets read and used in
123
+ // setNodeStatusImages. We sort the list on write instead of on read,
124
+ // because the image cache is more often read than written
125
+ sort .Sort (sliceutils .ByImageSize (images ))
120
126
i .images = images
121
127
}
122
128
123
- // get gets a sorted (by image size) image list from image cache.
124
- // There is a potentical data race in this function. See PR #60448
125
- // Because there is deepcopy function available currently, move sort
126
- // function inside this function
129
+ // get gets image list from image cache.
130
+ // NOTE: The caller of get() should not do mutating operations on the
131
+ // returned list that could cause data race against other readers (e.g.
132
+ // in-place sorting the returned list)
127
133
func (i * imageCache ) get () []container.Image {
128
134
i .Lock ()
129
135
defer i .Unlock ()
130
- sort .Sort (sliceutils .ByImageSize (i .images ))
131
136
return i .images
132
137
}
133
138
Original file line number Diff line number Diff line change @@ -548,16 +548,6 @@ func TestValidateImageGCPolicy(t *testing.T) {
548
548
}
549
549
}
550
550
551
- func TestImageCacheReturnCopiedList (t * testing.T ) {
552
- cache := & imageCache {}
553
- testList := []container.Image {{ID : "1" }, {ID : "2" }}
554
- cache .set (testList )
555
- list := cache .get ()
556
- assert .Len (t , list , 2 )
557
- list [0 ].ID = "3"
558
- assert .Equal (t , cache .get (), testList )
559
- }
560
-
561
551
func uint64Ptr (i uint64 ) * uint64 {
562
552
return & i
563
553
}
Original file line number Diff line number Diff line change @@ -446,7 +446,9 @@ func Images(nodeStatusMaxImages int32,
446
446
}
447
447
448
448
for _ , image := range containerImages {
449
- names := append (image .RepoDigests , image .RepoTags ... )
449
+ // make a copy to avoid modifying slice members of the image items in the list
450
+ names := append ([]string {}, image .RepoDigests ... )
451
+ names = append (names , image .RepoTags ... )
450
452
// Report up to MaxNamesPerImageInNodeStatus names per image.
451
453
if len (names ) > MaxNamesPerImageInNodeStatus {
452
454
names = names [0 :MaxNamesPerImageInNodeStatus ]
You can’t perform that action at this time.
0 commit comments