results_manager.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. Copyright 2015 The Kubernetes Authors.
  16. Licensed under the Apache License, Version 2.0 (the "License");
  17. you may not use this file except in compliance with the License.
  18. You may obtain a copy of the License at
  19. http://www.apache.org/licenses/LICENSE-2.0
  20. Unless required by applicable law or agreed to in writing, software
  21. distributed under the License is distributed on an "AS IS" BASIS,
  22. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. See the License for the specific language governing permissions and
  24. limitations under the License.
  25. */
  26. package results
  27. import (
  28. "fmt"
  29. "strings"
  30. "sync"
  31. )
  32. func NewFailure(reason string) ProbeResult {
  33. return newProbeResult(Failure, reason)
  34. }
  35. func NewSuccess(reason string) ProbeResult {
  36. return newProbeResult(Success, reason)
  37. }
  38. func NewUnknown(reason string) ProbeResult {
  39. return newProbeResult(Unknown, reason)
  40. }
  41. func newProbeResult(r Result, reason string) ProbeResult {
  42. return ProbeResult{
  43. Result: r,
  44. Reason: reason,
  45. }
  46. }
  47. type ProbeResult struct {
  48. Result
  49. Reason string
  50. }
  51. func (pr ProbeResult) String() string {
  52. return fmt.Sprintf("%s: %s", pr.Result.String(), pr.Reason)
  53. }
  54. func (pr ProbeResult) IsNetFailedError() bool {
  55. if pr.Result != Failure {
  56. return false
  57. }
  58. netFailedMsg := []string{
  59. "no route to host",
  60. "i/o timeout",
  61. }
  62. for _, msg := range netFailedMsg {
  63. if strings.Contains(pr.Reason, msg) {
  64. return true
  65. }
  66. }
  67. return false
  68. }
  69. // Result is the type for probe results.
  70. type Result int
  71. const (
  72. // Unknown is encoded as -1 (type Result)
  73. Unknown Result = iota - 1
  74. // Success is encoded as 0 (type Result)
  75. Success
  76. // Failure is encoded as 1 (type Result)
  77. Failure
  78. )
  79. func (r Result) String() string {
  80. switch r {
  81. case Success:
  82. return "Success"
  83. case Failure:
  84. return "Failure"
  85. default:
  86. return "UNKNOWN"
  87. }
  88. }
  89. type IPod interface {
  90. GetId() string
  91. IsRunning() bool
  92. }
  93. // Update is an enum of the types of updates sent over the Updates channel.
  94. type Update struct {
  95. ContainerID string
  96. Result ProbeResult
  97. PodUID string
  98. Pod IPod
  99. }
  100. // Manager provides a probe results cache and channel of updates
  101. type Manager interface {
  102. // Get returns the cached result for the container with the given ID.
  103. Get(containerId string) (ProbeResult, bool)
  104. // Set sets the cached result for the container with the given ID.
  105. // The pod is only included to be sent with the update.
  106. Set(containerId string, result ProbeResult, pod IPod, force bool)
  107. // Remove clears the cached result for the container with the given ID.
  108. Remove(containerId string)
  109. // Updates creates a channel that receives an Update whenever its result changes (but not
  110. // removed).
  111. // NOTE: The current implementation only supports a single updates channel.
  112. Updates() <-chan Update
  113. }
  114. var _ Manager = &manager{}
  115. type manager struct {
  116. // guards the cache
  117. sync.RWMutex
  118. // map of container ID -> probe Result
  119. cache map[string]ProbeResult
  120. // channel of updates
  121. updates chan Update
  122. }
  123. func NewManager() Manager {
  124. return &manager{
  125. cache: make(map[string]ProbeResult),
  126. updates: make(chan Update, 20),
  127. }
  128. }
  129. func (m *manager) Get(id string) (ProbeResult, bool) {
  130. m.RLock()
  131. defer m.RUnlock()
  132. result, found := m.cache[id]
  133. return result, found
  134. }
  135. func (m *manager) Set(id string, result ProbeResult, pod IPod, force bool) {
  136. if m.setInternal(id, result, force) {
  137. m.updates <- Update{
  138. ContainerID: id,
  139. Result: result,
  140. PodUID: pod.GetId(),
  141. Pod: pod,
  142. }
  143. }
  144. }
  145. // Internal helper for locked portion of set. Returns whether an update should be sent.
  146. func (m *manager) setInternal(id string, result ProbeResult, force bool) bool {
  147. m.Lock()
  148. defer m.Unlock()
  149. prev, exists := m.cache[id]
  150. if !exists || prev.Result != result.Result || prev.IsNetFailedError() != result.IsNetFailedError() || force {
  151. m.cache[id] = result
  152. return true
  153. }
  154. return false
  155. }
  156. func (m *manager) Remove(id string) {
  157. m.Lock()
  158. defer m.Unlock()
  159. delete(m.cache, id)
  160. }
  161. func (m *manager) Updates() <-chan Update {
  162. return m.updates
  163. }