results_manager.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. v1 "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 int
  36. const (
  37. // Unknown is encoded as -1 (type Result)
  38. Unknown Result = iota - 1
  39. // Success is encoded as 0 (type Result)
  40. Success
  41. // Failure is encoded as 1 (type Result)
  42. Failure
  43. )
  44. func (r Result) String() string {
  45. switch r {
  46. case Success:
  47. return "Success"
  48. case Failure:
  49. return "Failure"
  50. default:
  51. return "UNKNOWN"
  52. }
  53. }
  54. // ToPrometheusType translates a Result to a form which is better understood by prometheus.
  55. func (r Result) ToPrometheusType() float64 {
  56. switch r {
  57. case Success:
  58. return 0
  59. case Failure:
  60. return 1
  61. default:
  62. return -1
  63. }
  64. }
  65. // Update is an enum of the types of updates sent over the Updates channel.
  66. type Update struct {
  67. ContainerID kubecontainer.ContainerID
  68. Result Result
  69. PodUID types.UID
  70. }
  71. // Manager implementation.
  72. type manager struct {
  73. // guards the cache
  74. sync.RWMutex
  75. // map of container ID -> probe Result
  76. cache map[kubecontainer.ContainerID]Result
  77. // channel of updates
  78. updates chan Update
  79. }
  80. var _ Manager = &manager{}
  81. // NewManager creates and returns an empty results manager.
  82. func NewManager() Manager {
  83. return &manager{
  84. cache: make(map[kubecontainer.ContainerID]Result),
  85. updates: make(chan Update, 20),
  86. }
  87. }
  88. func (m *manager) Get(id kubecontainer.ContainerID) (Result, bool) {
  89. m.RLock()
  90. defer m.RUnlock()
  91. result, found := m.cache[id]
  92. return result, found
  93. }
  94. func (m *manager) Set(id kubecontainer.ContainerID, result Result, pod *v1.Pod) {
  95. if m.setInternal(id, result) {
  96. m.updates <- Update{id, result, pod.UID}
  97. }
  98. }
  99. // Internal helper for locked portion of set. Returns whether an update should be sent.
  100. func (m *manager) setInternal(id kubecontainer.ContainerID, result Result) bool {
  101. m.Lock()
  102. defer m.Unlock()
  103. prev, exists := m.cache[id]
  104. if !exists || prev != result {
  105. m.cache[id] = result
  106. return true
  107. }
  108. return false
  109. }
  110. func (m *manager) Remove(id kubecontainer.ContainerID) {
  111. m.Lock()
  112. defer m.Unlock()
  113. delete(m.cache, id)
  114. }
  115. func (m *manager) Updates() <-chan Update {
  116. return m.updates
  117. }