Skip to content

Commit

Permalink
[workload] Make better use of container generics (#3139)
Browse files Browse the repository at this point in the history
* [workload] Make better use of container generics

* Review Remarks
  • Loading branch information
trasc authored Sep 30, 2024
1 parent ae17586 commit 71af438
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions pkg/workload/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"sigs.k8s.io/kueue/pkg/util/api"
"sigs.k8s.io/kueue/pkg/util/limitrange"
utilmaps "sigs.k8s.io/kueue/pkg/util/maps"
utilslices "sigs.k8s.io/kueue/pkg/util/slices"
)

const (
Expand Down Expand Up @@ -256,21 +257,15 @@ func QueueKey(w *kueue.Workload) string {
}

func reclaimableCounts(wl *kueue.Workload) map[string]int32 {
ret := make(map[string]int32, len(wl.Status.ReclaimablePods))
for i := range wl.Status.ReclaimablePods {
reclaimInfo := &wl.Status.ReclaimablePods[i]
ret[reclaimInfo.Name] = reclaimInfo.Count
}
return ret
return utilslices.ToMap(wl.Status.ReclaimablePods, func(i int) (string, int32) {
return wl.Status.ReclaimablePods[i].Name, wl.Status.ReclaimablePods[i].Count
})
}

func podSetsCounts(wl *kueue.Workload) map[string]int32 {
ret := make(map[string]int32, len(wl.Spec.PodSets))
for i := range wl.Spec.PodSets {
ps := &wl.Spec.PodSets[i]
ret[ps.Name] = ps.Count
}
return ret
return utilslices.ToMap(wl.Spec.PodSets, func(i int) (string, int32) {
return wl.Spec.PodSets[i].Name, wl.Spec.PodSets[i].Count
})
}

func podSetsCountsAfterReclaim(wl *kueue.Workload) map[string]int32 {
Expand Down Expand Up @@ -573,18 +568,9 @@ func ReclaimablePodsAreEqual(a, b []kueue.ReclaimablePod) bool {
if len(a) != len(b) {
return false
}

mb := make(map[string]int32, len(b))
for i := range b {
mb[b[i].Name] = b[i].Count
}

for i := range a {
if bCount, found := mb[a[i].Name]; !found || bCount != a[i].Count {
return false
}
}
return true
ma := utilslices.ToMap(a, func(i int) (string, int32) { return a[i].Name, a[i].Count })
mb := utilslices.ToMap(b, func(i int) (string, int32) { return b[i].Name, b[i].Count })
return maps.Equal(ma, mb)
}

// IsAdmitted returns true if the workload is admitted.
Expand Down

0 comments on commit 71af438

Please sign in to comment.