reason_cache.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 kubelet
  14. import (
  15. "fmt"
  16. "sync"
  17. "github.com/golang/groupcache/lru"
  18. "k8s.io/apimachinery/pkg/types"
  19. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  20. )
  21. // ReasonCache stores the failure reason of the latest container start
  22. // in a string, keyed by <pod_UID>_<container_name>. The goal is to
  23. // propagate this reason to the container status. This endeavor is
  24. // "best-effort" for two reasons:
  25. // 1. The cache is not persisted.
  26. // 2. We use an LRU cache to avoid extra garbage collection work. This
  27. // means that some entries may be recycled before a pod has been
  28. // deleted.
  29. // TODO(random-liu): Use more reliable cache which could collect garbage of failed pod.
  30. // TODO(random-liu): Move reason cache to somewhere better.
  31. type ReasonCache struct {
  32. lock sync.Mutex
  33. cache *lru.Cache
  34. }
  35. // ReasonItem is the cached item in ReasonCache
  36. type ReasonItem struct {
  37. Err error
  38. Message string
  39. }
  40. // maxReasonCacheEntries is the cache entry number in lru cache. 1000 is a proper number
  41. // for our 100 pods per node target. If we support more pods per node in the future, we
  42. // may want to increase the number.
  43. const maxReasonCacheEntries = 1000
  44. // NewReasonCache creates an instance of 'ReasonCache'.
  45. func NewReasonCache() *ReasonCache {
  46. return &ReasonCache{cache: lru.New(maxReasonCacheEntries)}
  47. }
  48. func (c *ReasonCache) composeKey(uid types.UID, name string) string {
  49. return fmt.Sprintf("%s_%s", uid, name)
  50. }
  51. // add adds error reason into the cache
  52. func (c *ReasonCache) add(uid types.UID, name string, reason error, message string) {
  53. c.lock.Lock()
  54. defer c.lock.Unlock()
  55. c.cache.Add(c.composeKey(uid, name), ReasonItem{reason, message})
  56. }
  57. // Update updates the reason cache with the SyncPodResult. Only SyncResult with
  58. // StartContainer action will change the cache.
  59. func (c *ReasonCache) Update(uid types.UID, result kubecontainer.PodSyncResult) {
  60. for _, r := range result.SyncResults {
  61. if r.Action != kubecontainer.StartContainer {
  62. continue
  63. }
  64. name := r.Target.(string)
  65. if r.Error != nil {
  66. c.add(uid, name, r.Error, r.Message)
  67. } else {
  68. c.Remove(uid, name)
  69. }
  70. }
  71. }
  72. // Remove removes error reason from the cache
  73. func (c *ReasonCache) Remove(uid types.UID, name string) {
  74. c.lock.Lock()
  75. defer c.lock.Unlock()
  76. c.cache.Remove(c.composeKey(uid, name))
  77. }
  78. // Get gets error reason from the cache. The return values are error reason, error message and
  79. // whether an error reason is found in the cache. If no error reason is found, empty string will
  80. // be returned for error reason and error message.
  81. func (c *ReasonCache) Get(uid types.UID, name string) (*ReasonItem, bool) {
  82. c.lock.Lock()
  83. defer c.lock.Unlock()
  84. value, ok := c.cache.Get(c.composeKey(uid, name))
  85. if !ok {
  86. return nil, false
  87. }
  88. info := value.(ReasonItem)
  89. return &info, true
  90. }