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 <sgrunert@redhat.com>
  • Loading branch information
saschagrunert committed Nov 13, 2024
1 parent 98761db commit 70827fe
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 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 @@ -216,11 +215,15 @@ func testPullPublicImage(c internalapi.ImageManagerService, imageName string, po
}

// pullImageList pulls the images listed in the imageList.
func pullImageList(c internalapi.ImageManagerService, imageList []string, podConfig *runtimeapi.PodSandboxConfig) []string {
ids := []string{}
func pullImageList(c internalapi.ImageManagerService, imageList []string, podConfig *runtimeapi.PodSandboxConfig) (ids []string) {
fmt.Printf("imageList: %v\n", imageList)
for _, imageName := range imageList {
ids = append(ids, framework.PullPublicImage(c, imageName, podConfig))
}
fmt.Printf("IDS (before): %v\n", ids)
slices.Sort(ids)
ids = slices.Compact(ids)
fmt.Printf("IDS (after): %v\n", ids)
return ids
}

Expand All @@ -243,17 +246,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 70827fe

Please sign in to comment.