runtime.go 3.0 KB

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