results_manager.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 results
  14. import (
  15. "sync"
  16. "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/types"
  18. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  19. )
  20. // Manager provides a probe results cache and channel of updates.
  21. type Manager interface {
  22. // Get returns the cached result for the container with the given ID.
  23. Get(kubecontainer.ContainerID) (Result, bool)
  24. // Set sets the cached result for the container with the given ID.
  25. // The pod is only included to be sent with the update.
  26. Set(kubecontainer.ContainerID, Result, *v1.Pod)
  27. // Remove clears the cached result for the container with the given ID.
  28. Remove(kubecontainer.ContainerID)
  29. // Updates creates a channel that receives an Update whenever its result changes (but not
  30. // removed).
  31. // NOTE: The current implementation only supports a single updates channel.
  32. Updates() <-chan Update
  33. }
  34. // Result is the type for probe results.
  35. type Result bool
  36. const (
  37. Success Result = true
  38. Failure Result = false
  39. )
  40. func (r Result) String() string {
  41. switch r {
  42. case Success:
  43. return "Success"
  44. case Failure:
  45. return "Failure"
  46. default:
  47. return "UNKNOWN"
  48. }
  49. }
  50. // ToPrometheusType translates a Result to a form which is better understood by prometheus.
  51. func (r Result) ToPrometheusType() float64 {
  52. switch r {
  53. case Success:
  54. return 0
  55. case Failure:
  56. return 1
  57. default:
  58. return -1
  59. }
  60. }
  61. // Update is an enum of the types of updates sent over the Updates channel.
  62. type Update struct {
  63. ContainerID kubecontainer.ContainerID
  64. Result Result
  65. PodUID types.UID
  66. }
  67. // Manager implementation.
  68. type manager struct {
  69. // guards the cache
  70. sync.RWMutex
  71. // map of container ID -> probe Result
  72. cache map[kubecontainer.ContainerID]Result
  73. // channel of updates
  74. updates chan Update
  75. }
  76. var _ Manager = &manager{}
  77. // NewManager creates and returns an empty results manager.
  78. func NewManager() Manager {
  79. return &manager{
  80. cache: make(map[kubecontainer.ContainerID]Result),
  81. updates: make(chan Update, 20),
  82. }
  83. }
  84. func (m *manager) Get(id kubecontainer.ContainerID) (Result, bool) {
  85. m.RLock()
  86. defer m.RUnlock()
  87. result, found := m.cache[id]
  88. return result, found
  89. }
  90. func (m *manager) Set(id kubecontainer.ContainerID, result Result, pod *v1.Pod) {
  91. if m.setInternal(id, result) {
  92. m.updates <- Update{id, result, pod.UID}
  93. }
  94. }
  95. // Internal helper for locked portion of set. Returns whether an update should be sent.
  96. func (m *manager) setInternal(id kubecontainer.ContainerID, result Result) bool {
  97. m.Lock()
  98. defer m.Unlock()
  99. prev, exists := m.cache[id]
  100. if !exists || prev != result {
  101. m.cache[id] = result
  102. return true
  103. }
  104. return false
  105. }
  106. func (m *manager) Remove(id kubecontainer.ContainerID) {
  107. m.Lock()
  108. defer m.Unlock()
  109. delete(m.cache, id)
  110. }
  111. func (m *manager) Updates() <-chan Update {
  112. return m.updates
  113. }