Skip to content

Commit 4f7d24f

Browse files
committed
improve
1 parent 9f73994 commit 4f7d24f

File tree

4 files changed

+194
-133
lines changed

4 files changed

+194
-133
lines changed

internal/controllers/machine/drain/drain.go

+4-21
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import (
3434
"k8s.io/klog/v2"
3535
ctrl "sigs.k8s.io/controller-runtime"
3636
"sigs.k8s.io/controller-runtime/pkg/client"
37+
38+
clog "sigs.k8s.io/cluster-api/util/log"
3739
)
3840

3941
// Helper contains the parameters to control the behaviour of the drain helper.
@@ -351,33 +353,14 @@ func (r EvictionResult) ConditionMessage() string {
351353

352354
// podDeleteListToString returns a comma-separated list of the first n entries of the PodDelete list.
353355
func podDeleteListToString(podList []PodDelete, n int) string {
354-
return listToString(podList, func(pd PodDelete) string {
356+
return clog.ListToString(podList, func(pd PodDelete) string {
355357
return klog.KObj(pd.Pod).String()
356358
}, n)
357359
}
358360

359361
// PodListToString returns a comma-separated list of the first n entries of the Pod list.
360362
func PodListToString(podList []*corev1.Pod, n int) string {
361-
return listToString(podList, func(p *corev1.Pod) string {
363+
return clog.ListToString(podList, func(p *corev1.Pod) string {
362364
return klog.KObj(p).String()
363365
}, n)
364366
}
365-
366-
// listToString returns a comma-separated list of the first n entries of the list (strings are calculated via stringFunc).
367-
func listToString[T any](list []T, stringFunc func(T) string, n int) string {
368-
shortenedBy := 0
369-
if len(list) > n {
370-
shortenedBy = len(list) - n
371-
list = list[:n]
372-
}
373-
stringList := []string{}
374-
for _, p := range list {
375-
stringList = append(stringList, stringFunc(p))
376-
}
377-
378-
if shortenedBy > 0 {
379-
stringList = append(stringList, fmt.Sprintf("... (%d more)", shortenedBy))
380-
}
381-
382-
return strings.Join(stringList, ", ")
383-
}

0 commit comments

Comments
 (0)