runtime.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "fmt"
  16. "sync"
  17. "time"
  18. )
  19. type runtimeState struct {
  20. sync.RWMutex
  21. lastBaseRuntimeSync time.Time
  22. baseRuntimeSyncThreshold time.Duration
  23. networkError error
  24. internalError error
  25. cidr string
  26. initError error
  27. }
  28. func (s *runtimeState) setRuntimeSync(t time.Time) {
  29. s.Lock()
  30. defer s.Unlock()
  31. s.lastBaseRuntimeSync = t
  32. }
  33. func (s *runtimeState) setInternalError(err error) {
  34. s.Lock()
  35. defer s.Unlock()
  36. s.internalError = err
  37. }
  38. func (s *runtimeState) setNetworkState(err error) {
  39. s.Lock()
  40. defer s.Unlock()
  41. s.networkError = err
  42. }
  43. func (s *runtimeState) setPodCIDR(cidr string) {
  44. s.Lock()
  45. defer s.Unlock()
  46. s.cidr = cidr
  47. }
  48. func (s *runtimeState) podCIDR() string {
  49. s.RLock()
  50. defer s.RUnlock()
  51. return s.cidr
  52. }
  53. func (s *runtimeState) setInitError(err error) {
  54. s.Lock()
  55. defer s.Unlock()
  56. s.initError = err
  57. }
  58. func (s *runtimeState) errors() []string {
  59. s.RLock()
  60. defer s.RUnlock()
  61. var ret []string
  62. if s.initError != nil {
  63. ret = append(ret, s.initError.Error())
  64. }
  65. if s.networkError != nil {
  66. ret = append(ret, s.networkError.Error())
  67. }
  68. if !s.lastBaseRuntimeSync.Add(s.baseRuntimeSyncThreshold).After(time.Now()) {
  69. ret = append(ret, "container runtime is down")
  70. }
  71. if s.internalError != nil {
  72. ret = append(ret, s.internalError.Error())
  73. }
  74. return ret
  75. }
  76. func newRuntimeState(
  77. runtimeSyncThreshold time.Duration,
  78. ) *runtimeState {
  79. return &runtimeState{
  80. lastBaseRuntimeSync: time.Time{},
  81. baseRuntimeSyncThreshold: runtimeSyncThreshold,
  82. networkError: fmt.Errorf("network state unknown"),
  83. internalError: nil,
  84. }
  85. }