utils.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Copyright 2016 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 predicates
  14. import (
  15. "k8s.io/api/core/v1"
  16. "k8s.io/apimachinery/pkg/labels"
  17. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  18. )
  19. // FindLabelsInSet gets as many key/value pairs as possible out of a label set.
  20. func FindLabelsInSet(labelsToKeep []string, selector labels.Set) map[string]string {
  21. aL := make(map[string]string)
  22. for _, l := range labelsToKeep {
  23. if selector.Has(l) {
  24. aL[l] = selector.Get(l)
  25. }
  26. }
  27. return aL
  28. }
  29. // AddUnsetLabelsToMap backfills missing values with values we find in a map.
  30. func AddUnsetLabelsToMap(aL map[string]string, labelsToAdd []string, labelSet labels.Set) {
  31. for _, l := range labelsToAdd {
  32. // if the label is already there, dont overwrite it.
  33. if _, exists := aL[l]; exists {
  34. continue
  35. }
  36. // otherwise, backfill this label.
  37. if labelSet.Has(l) {
  38. aL[l] = labelSet.Get(l)
  39. }
  40. }
  41. }
  42. // FilterPodsByNamespace filters pods outside a namespace from the given list.
  43. func FilterPodsByNamespace(pods []*v1.Pod, ns string) []*v1.Pod {
  44. filtered := []*v1.Pod{}
  45. for _, nsPod := range pods {
  46. if nsPod.Namespace == ns {
  47. filtered = append(filtered, nsPod)
  48. }
  49. }
  50. return filtered
  51. }
  52. // CreateSelectorFromLabels is used to define a selector that corresponds to the keys in a map.
  53. func CreateSelectorFromLabels(aL map[string]string) labels.Selector {
  54. if aL == nil || len(aL) == 0 {
  55. return labels.Everything()
  56. }
  57. return labels.Set(aL).AsSelector()
  58. }
  59. // portsConflict check whether existingPorts and wantPorts conflict with each other
  60. // return true if we have a conflict
  61. func portsConflict(existingPorts schedulernodeinfo.HostPortInfo, wantPorts []*v1.ContainerPort) bool {
  62. for _, cp := range wantPorts {
  63. if existingPorts.CheckConflict(cp.HostIP, string(cp.Protocol), cp.HostPort) {
  64. return true
  65. }
  66. }
  67. return false
  68. }
  69. // SetPredicatesOrderingDuringTest sets the predicatesOrdering to the specified
  70. // value, and returns a function that restores the original value.
  71. func SetPredicatesOrderingDuringTest(value []string) func() {
  72. origVal := predicatesOrdering
  73. predicatesOrdering = value
  74. return func() {
  75. predicatesOrdering = origVal
  76. }
  77. }