waiting_pods_map.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright 2019 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 v1alpha1
  14. import (
  15. "sync"
  16. "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/types"
  18. )
  19. // waitingPodsMap a thread-safe map used to maintain pods waiting in the permit phase.
  20. type waitingPodsMap struct {
  21. pods map[types.UID]WaitingPod
  22. mu sync.RWMutex
  23. }
  24. // newWaitingPodsMap returns a new waitingPodsMap.
  25. func newWaitingPodsMap() *waitingPodsMap {
  26. return &waitingPodsMap{
  27. pods: make(map[types.UID]WaitingPod),
  28. }
  29. }
  30. // add a new WaitingPod to the map.
  31. func (m *waitingPodsMap) add(wp WaitingPod) {
  32. m.mu.Lock()
  33. defer m.mu.Unlock()
  34. m.pods[wp.GetPod().UID] = wp
  35. }
  36. // remove a WaitingPod from the map.
  37. func (m *waitingPodsMap) remove(uid types.UID) {
  38. m.mu.Lock()
  39. defer m.mu.Unlock()
  40. delete(m.pods, uid)
  41. }
  42. // get a WaitingPod from the map.
  43. func (m *waitingPodsMap) get(uid types.UID) WaitingPod {
  44. m.mu.RLock()
  45. defer m.mu.RUnlock()
  46. return m.pods[uid]
  47. }
  48. // iterate acquires a read lock and iterates over the WaitingPods map.
  49. func (m *waitingPodsMap) iterate(callback func(WaitingPod)) {
  50. m.mu.RLock()
  51. defer m.mu.RUnlock()
  52. for _, v := range m.pods {
  53. callback(v)
  54. }
  55. }
  56. // waitingPod represents a pod waiting in the permit phase.
  57. type waitingPod struct {
  58. pod *v1.Pod
  59. s chan *Status
  60. }
  61. // newWaitingPod returns a new waitingPod instance.
  62. func newWaitingPod(pod *v1.Pod) *waitingPod {
  63. return &waitingPod{
  64. pod: pod,
  65. s: make(chan *Status),
  66. }
  67. }
  68. // GetPod returns a reference to the waiting pod.
  69. func (w *waitingPod) GetPod() *v1.Pod {
  70. return w.pod
  71. }
  72. // Allow the waiting pod to be scheduled. Returns true if the allow signal was
  73. // successfully delivered, false otherwise.
  74. func (w *waitingPod) Allow() bool {
  75. select {
  76. case w.s <- NewStatus(Success, ""):
  77. return true
  78. default:
  79. return false
  80. }
  81. }
  82. // Reject declares the waiting pod unschedulable. Returns true if the allow signal
  83. // was successfully delivered, false otherwise.
  84. func (w *waitingPod) Reject(msg string) bool {
  85. select {
  86. case w.s <- NewStatus(Unschedulable, msg):
  87. return true
  88. default:
  89. return false
  90. }
  91. }