2019-01-12 04:58:27 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package kubelet
|
|
|
|
|
|
|
|
import (
|
2019-04-07 17:07:55 +00:00
|
|
|
"errors"
|
2019-01-12 04:58:27 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2019-04-07 17:07:55 +00:00
|
|
|
|
|
|
|
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
2019-01-12 04:58:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type runtimeState struct {
|
|
|
|
sync.RWMutex
|
|
|
|
lastBaseRuntimeSync time.Time
|
|
|
|
baseRuntimeSyncThreshold time.Duration
|
|
|
|
networkError error
|
2019-09-27 21:51:53 +00:00
|
|
|
runtimeError error
|
2019-04-07 17:07:55 +00:00
|
|
|
storageError error
|
2019-01-12 04:58:27 +00:00
|
|
|
cidr string
|
|
|
|
healthChecks []*healthCheck
|
|
|
|
}
|
|
|
|
|
|
|
|
// A health check function should be efficient and not rely on external
|
|
|
|
// components (e.g., container runtime).
|
|
|
|
type healthCheckFnType func() (bool, error)
|
|
|
|
|
|
|
|
type healthCheck struct {
|
|
|
|
name string
|
|
|
|
fn healthCheckFnType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *runtimeState) addHealthCheck(name string, f healthCheckFnType) {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
s.healthChecks = append(s.healthChecks, &healthCheck{name: name, fn: f})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *runtimeState) setRuntimeSync(t time.Time) {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
s.lastBaseRuntimeSync = t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *runtimeState) setNetworkState(err error) {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
s.networkError = err
|
|
|
|
}
|
|
|
|
|
2019-09-27 21:51:53 +00:00
|
|
|
func (s *runtimeState) setRuntimeState(err error) {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
s.runtimeError = err
|
|
|
|
}
|
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
func (s *runtimeState) setStorageState(err error) {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
s.storageError = err
|
|
|
|
}
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
func (s *runtimeState) setPodCIDR(cidr string) {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
s.cidr = cidr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *runtimeState) podCIDR() string {
|
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
return s.cidr
|
|
|
|
}
|
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
func (s *runtimeState) runtimeErrors() error {
|
2019-01-12 04:58:27 +00:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
2019-04-07 17:07:55 +00:00
|
|
|
errs := []error{}
|
2019-01-12 04:58:27 +00:00
|
|
|
if s.lastBaseRuntimeSync.IsZero() {
|
2019-08-30 18:33:25 +00:00
|
|
|
errs = append(errs, errors.New("container runtime status check may not have completed yet"))
|
2019-01-12 04:58:27 +00:00
|
|
|
} else if !s.lastBaseRuntimeSync.Add(s.baseRuntimeSyncThreshold).After(time.Now()) {
|
2019-08-30 18:33:25 +00:00
|
|
|
errs = append(errs, errors.New("container runtime is down"))
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
for _, hc := range s.healthChecks {
|
|
|
|
if ok, err := hc.fn(); !ok {
|
2019-08-30 18:33:25 +00:00
|
|
|
errs = append(errs, fmt.Errorf("%s is not healthy: %v", hc.name, err))
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-27 21:51:53 +00:00
|
|
|
if s.runtimeError != nil {
|
|
|
|
errs = append(errs, s.runtimeError)
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
return utilerrors.NewAggregate(errs)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
func (s *runtimeState) networkErrors() error {
|
2019-01-12 04:58:27 +00:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
2019-04-07 17:07:55 +00:00
|
|
|
errs := []error{}
|
2019-01-12 04:58:27 +00:00
|
|
|
if s.networkError != nil {
|
2019-04-07 17:07:55 +00:00
|
|
|
errs = append(errs, s.networkError)
|
|
|
|
}
|
|
|
|
return utilerrors.NewAggregate(errs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *runtimeState) storageErrors() error {
|
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
errs := []error{}
|
|
|
|
if s.storageError != nil {
|
|
|
|
errs = append(errs, s.storageError)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
2019-04-07 17:07:55 +00:00
|
|
|
return utilerrors.NewAggregate(errs)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
func newRuntimeState(runtimeSyncThreshold time.Duration) *runtimeState {
|
2019-01-12 04:58:27 +00:00
|
|
|
return &runtimeState{
|
|
|
|
lastBaseRuntimeSync: time.Time{},
|
|
|
|
baseRuntimeSyncThreshold: runtimeSyncThreshold,
|
2019-04-07 17:07:55 +00:00
|
|
|
networkError: ErrNetworkUnknown,
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
}
|