reason_cache.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  19. "k8s.io/kubernetes/pkg/types"
  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. // reasonInfo is the cached item in ReasonCache
  36. type reasonInfo struct {
  37. reason 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. func NewReasonCache() *ReasonCache {
  45. return &ReasonCache{cache: lru.New(maxReasonCacheEntries)}
  46. }
  47. func (c *ReasonCache) composeKey(uid types.UID, name string) string {
  48. return fmt.Sprintf("%s_%s", uid, name)
  49. }
  50. // add adds error reason into the cache
  51. func (c *ReasonCache) add(uid types.UID, name string, reason error, message string) {
  52. c.lock.Lock()
  53. defer c.lock.Unlock()
  54. c.cache.Add(c.composeKey(uid, name), reasonInfo{reason, message})
  55. }
  56. // Update updates the reason cache with the SyncPodResult. Only SyncResult with
  57. // StartContainer action will change the cache.
  58. func (c *ReasonCache) Update(uid types.UID, result kubecontainer.PodSyncResult) {
  59. for _, r := range result.SyncResults {
  60. if r.Action != kubecontainer.StartContainer {
  61. continue
  62. }
  63. name := r.Target.(string)
  64. if r.Error != nil {
  65. c.add(uid, name, r.Error, r.Message)
  66. } else {
  67. c.Remove(uid, name)
  68. }
  69. }
  70. }
  71. // Remove removes error reason from the cache
  72. func (c *ReasonCache) Remove(uid types.UID, name string) {
  73. c.lock.Lock()
  74. defer c.lock.Unlock()
  75. c.cache.Remove(c.composeKey(uid, name))
  76. }
  77. // Get gets error reason from the cache. The return values are error reason, error message and
  78. // whether an error reason is found in the cache. If no error reason is found, empty string will
  79. // be returned for error reason and error message.
  80. func (c *ReasonCache) Get(uid types.UID, name string) (error, string, bool) {
  81. c.lock.Lock()
  82. defer c.lock.Unlock()
  83. value, ok := c.cache.Get(c.composeKey(uid, name))
  84. if !ok {
  85. return nil, "", ok
  86. }
  87. info := value.(reasonInfo)
  88. return info.reason, info.message, ok
  89. }