runtime.go 3.2 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 kubelet
  14. import (
  15. "errors"
  16. "fmt"
  17. "sync"
  18. "time"
  19. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  20. )
  21. type runtimeState struct {
  22. sync.RWMutex
  23. lastBaseRuntimeSync time.Time
  24. baseRuntimeSyncThreshold time.Duration
  25. networkError error
  26. runtimeError error
  27. storageError error
  28. cidr string
  29. healthChecks []*healthCheck
  30. }
  31. // A health check function should be efficient and not rely on external
  32. // components (e.g., container runtime).
  33. type healthCheckFnType func() (bool, error)
  34. type healthCheck struct {
  35. name string
  36. fn healthCheckFnType
  37. }
  38. func (s *runtimeState) addHealthCheck(name string, f healthCheckFnType) {
  39. s.Lock()
  40. defer s.Unlock()
  41. s.healthChecks = append(s.healthChecks, &healthCheck{name: name, fn: f})
  42. }
  43. func (s *runtimeState) setRuntimeSync(t time.Time) {
  44. s.Lock()
  45. defer s.Unlock()
  46. s.lastBaseRuntimeSync = t
  47. }
  48. func (s *runtimeState) setNetworkState(err error) {
  49. s.Lock()
  50. defer s.Unlock()
  51. s.networkError = err
  52. }
  53. func (s *runtimeState) setRuntimeState(err error) {
  54. s.Lock()
  55. defer s.Unlock()
  56. s.runtimeError = err
  57. }
  58. func (s *runtimeState) setStorageState(err error) {
  59. s.Lock()
  60. defer s.Unlock()
  61. s.storageError = err
  62. }
  63. func (s *runtimeState) setPodCIDR(cidr string) {
  64. s.Lock()
  65. defer s.Unlock()
  66. s.cidr = cidr
  67. }
  68. func (s *runtimeState) podCIDR() string {
  69. s.RLock()
  70. defer s.RUnlock()
  71. return s.cidr
  72. }
  73. func (s *runtimeState) runtimeErrors() error {
  74. s.RLock()
  75. defer s.RUnlock()
  76. errs := []error{}
  77. if s.lastBaseRuntimeSync.IsZero() {
  78. errs = append(errs, errors.New("container runtime status check may not have completed yet"))
  79. } else if !s.lastBaseRuntimeSync.Add(s.baseRuntimeSyncThreshold).After(time.Now()) {
  80. errs = append(errs, errors.New("container runtime is down"))
  81. }
  82. for _, hc := range s.healthChecks {
  83. if ok, err := hc.fn(); !ok {
  84. errs = append(errs, fmt.Errorf("%s is not healthy: %v", hc.name, err))
  85. }
  86. }
  87. if s.runtimeError != nil {
  88. errs = append(errs, s.runtimeError)
  89. }
  90. return utilerrors.NewAggregate(errs)
  91. }
  92. func (s *runtimeState) networkErrors() error {
  93. s.RLock()
  94. defer s.RUnlock()
  95. errs := []error{}
  96. if s.networkError != nil {
  97. errs = append(errs, s.networkError)
  98. }
  99. return utilerrors.NewAggregate(errs)
  100. }
  101. func (s *runtimeState) storageErrors() error {
  102. s.RLock()
  103. defer s.RUnlock()
  104. errs := []error{}
  105. if s.storageError != nil {
  106. errs = append(errs, s.storageError)
  107. }
  108. return utilerrors.NewAggregate(errs)
  109. }
  110. func newRuntimeState(
  111. runtimeSyncThreshold time.Duration,
  112. ) *runtimeState {
  113. return &runtimeState{
  114. lastBaseRuntimeSync: time.Time{},
  115. baseRuntimeSyncThreshold: runtimeSyncThreshold,
  116. networkError: ErrNetworkUnknown,
  117. }
  118. }