github.com/jenkins-x/jx/v2@v2.1.155/pkg/kube/runtime_objects.go (about) 1 package kube 2 3 import ( 4 "sort" 5 6 "k8s.io/apimachinery/pkg/api/meta" 7 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 8 "k8s.io/apimachinery/pkg/runtime" 9 "k8s.io/client-go/tools/cache" 10 ) 11 12 type ByName []runtime.Object 13 14 func (a ByName) Len() int { return len(a) } 15 func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 16 func (a ByName) Less(i, j int) bool { 17 o1 := a[i] 18 o2 := a[j] 19 a1, _ := meta.Accessor(o1) 20 a2, _ := meta.Accessor(o2) 21 if a1 == nil || a2 == nil { 22 return false 23 } 24 return a1.GetName() < a2.GetName() 25 } 26 27 func SortRuntimeObjectsByName(objects []runtime.Object) { 28 sort.Sort(ByName(objects)) 29 } 30 31 func SortListWatchByName(listWatch *cache.ListWatch) { 32 oldFn := listWatch.ListFunc 33 listWatch.ListFunc = func(options metav1.ListOptions) (runtime.Object, error) { 34 result, err := oldFn(options) 35 if err == nil { 36 initialItems, err := meta.ExtractList(result) 37 if err == nil { 38 SortRuntimeObjectsByName(initialItems) 39 meta.SetList(result, initialItems) //nolint:errcheck 40 } 41 } 42 return result, err 43 } 44 }