Skip to content

Commit

Permalink
Use slices.Compact instead of own logic
Browse files Browse the repository at this point in the history
The `slices` package can be used to remove duplicates.

Signed-off-by: Sascha Grunert <[email protected]>
  • Loading branch information
saschagrunert committed Nov 11, 2024
1 parent 98761db commit 1bca302
Showing 1 changed file with 2 additions and 17 deletions.
19 changes: 2 additions & 17 deletions pkg/validate/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"runtime"
"slices"
"sort"
"time"

Expand Down Expand Up @@ -146,7 +147,6 @@ var _ = framework.KubeDescribe("Image Manager", func() {
// Make sure test image does not exist.
removeImageList(c, testDifferentTagDifferentImageList)
ids := pullImageList(c, testDifferentTagDifferentImageList, testImagePodSandbox)
ids = removeDuplicates(ids)
Expect(ids).To(HaveLen(3), "3 image ids should be returned")

defer removeImageList(c, testDifferentTagDifferentImageList)
Expand All @@ -168,7 +168,6 @@ var _ = framework.KubeDescribe("Image Manager", func() {
// Make sure test image does not exist.
removeImageList(c, testDifferentTagSameImageList)
ids := pullImageList(c, testDifferentTagSameImageList, testImagePodSandbox)
ids = removeDuplicates(ids)
Expect(ids).To(HaveLen(1), "Only 1 image id should be returned")

defer removeImageList(c, testDifferentTagSameImageList)
Expand Down Expand Up @@ -221,7 +220,7 @@ func pullImageList(c internalapi.ImageManagerService, imageList []string, podCon
for _, imageName := range imageList {
ids = append(ids, framework.PullPublicImage(c, imageName, podConfig))
}
return ids
return slices.Compact(ids)
}

// removeImageList removes the images listed in the imageList.
Expand All @@ -243,17 +242,3 @@ func removeImage(c internalapi.ImageManagerService, imageName string) {
framework.ExpectNoError(err, "failed to remove image: %v", err)
}
}

// removeDuplicates remove duplicates strings from a list.
func removeDuplicates(ss []string) []string {
encountered := map[string]bool{}
result := []string{}
for _, s := range ss {
if encountered[s] {
continue
}
encountered[s] = true
result = append(result, s)
}
return result
}

0 comments on commit 1bca302

Please sign in to comment.