k8s.io/kubernetes@v1.29.3/pkg/kubelet/util/sliceutils/sliceutils.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package sliceutils 18 19 import ( 20 v1 "k8s.io/api/core/v1" 21 kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" 22 ) 23 24 // PodsByCreationTime makes an array of pods sortable by their creation 25 // timestamps in ascending order. 26 type PodsByCreationTime []*v1.Pod 27 28 func (s PodsByCreationTime) Len() int { 29 return len(s) 30 } 31 32 func (s PodsByCreationTime) Swap(i, j int) { 33 s[i], s[j] = s[j], s[i] 34 } 35 36 func (s PodsByCreationTime) Less(i, j int) bool { 37 return s[i].CreationTimestamp.Before(&s[j].CreationTimestamp) 38 } 39 40 // ByImageSize makes an array of images sortable by their size in descending 41 // order. 42 type ByImageSize []kubecontainer.Image 43 44 func (a ByImageSize) Less(i, j int) bool { 45 if a[i].Size == a[j].Size { 46 return a[i].ID > a[j].ID 47 } 48 return a[i].Size > a[j].Size 49 } 50 func (a ByImageSize) Len() int { return len(a) } 51 func (a ByImageSize) Swap(i, j int) { a[i], a[j] = a[j], a[i] }