sliceutils.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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/kubernetes/pkg/api"
  16. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  17. )
  18. func StringInSlice(s string, list []string) bool {
  19. for _, v := range list {
  20. if v == s {
  21. return true
  22. }
  23. }
  24. return false
  25. }
  26. // PodsByCreationTime makes an array of pods sortable by their creation
  27. // timestamps in ascending order.
  28. type PodsByCreationTime []*api.Pod
  29. func (s PodsByCreationTime) Len() int {
  30. return len(s)
  31. }
  32. func (s PodsByCreationTime) Swap(i, j int) {
  33. s[i], s[j] = s[j], s[i]
  34. }
  35. func (s PodsByCreationTime) Less(i, j int) bool {
  36. return s[i].CreationTimestamp.Before(s[j].CreationTimestamp)
  37. }
  38. // ByImageSize makes an array of images sortable by their size in descending
  39. // order.
  40. type ByImageSize []kubecontainer.Image
  41. func (a ByImageSize) Less(i, j int) bool {
  42. return a[i].Size > a[j].Size
  43. }
  44. func (a ByImageSize) Len() int { return len(a) }
  45. func (a ByImageSize) Swap(i, j int) { a[i], a[j] = a[j], a[i] }