sliceutils.go 1.6 KB

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