2014-10-08 19:14:37 -04:00
/ *
2017-10-11 19:36:39 -04:00
Copyright 2017 The Kubernetes Authors .
2014-10-08 19:14:37 -04:00
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 .
* /
2017-10-11 19:36:39 -04:00
// The Controller sets tainted annotations on nodes.
// Tainted nodes should not be used for new work loads and
// some effort should be given to getting existing work
// loads off of tainted nodes.
2014-10-08 19:14:37 -04:00
2017-10-11 19:36:39 -04:00
package nodelifecycle
2017-08-08 15:55:57 -04:00
2017-10-11 19:36:39 -04:00
import (
2018-03-14 03:08:17 -04:00
"fmt"
"sync"
"time"
2018-11-09 13:49:10 -05:00
"k8s.io/klog"
2018-03-14 03:08:17 -04:00
2018-10-01 14:32:56 -04:00
coordv1beta1 "k8s.io/api/coordination/v1beta1"
2017-10-11 19:36:39 -04:00
"k8s.io/api/core/v1"
2017-01-25 08:39:54 -05:00
apiequality "k8s.io/apimachinery/pkg/api/equality"
2017-02-06 13:35:50 -05:00
apierrors "k8s.io/apimachinery/pkg/api/errors"
2017-01-11 09:09:48 -05:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
2018-10-01 14:32:56 -04:00
utilfeature "k8s.io/apiserver/pkg/util/feature"
2018-08-15 22:06:39 -04:00
appsv1informers "k8s.io/client-go/informers/apps/v1"
2018-10-01 14:32:56 -04:00
coordinformers "k8s.io/client-go/informers/coordination/v1beta1"
2017-06-23 16:56:37 -04:00
coreinformers "k8s.io/client-go/informers/core/v1"
clientset "k8s.io/client-go/kubernetes"
2017-10-11 19:36:39 -04:00
"k8s.io/client-go/kubernetes/scheme"
2018-10-19 17:19:23 -04:00
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
2018-08-15 22:06:39 -04:00
appsv1listers "k8s.io/client-go/listers/apps/v1"
2018-10-01 14:32:56 -04:00
coordlisters "k8s.io/client-go/listers/coordination/v1beta1"
2017-06-23 16:56:37 -04:00
corelisters "k8s.io/client-go/listers/core/v1"
2017-10-11 19:36:39 -04:00
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/flowcontrol"
2018-08-31 03:26:19 -04:00
"k8s.io/client-go/util/workqueue"
2017-02-06 07:58:48 -05:00
"k8s.io/kubernetes/pkg/controller"
2017-10-11 19:36:39 -04:00
"k8s.io/kubernetes/pkg/controller/nodelifecycle/scheduler"
nodeutil "k8s.io/kubernetes/pkg/controller/util/node"
2018-10-01 14:32:56 -04:00
"k8s.io/kubernetes/pkg/features"
2019-02-22 19:09:07 -05:00
kubeletapis "k8s.io/kubernetes/pkg/kubelet/apis"
2018-09-27 22:37:38 -04:00
schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
2016-04-13 14:38:32 -04:00
"k8s.io/kubernetes/pkg/util/metrics"
2016-07-12 03:38:57 -04:00
utilnode "k8s.io/kubernetes/pkg/util/node"
2016-05-16 05:20:23 -04:00
"k8s.io/kubernetes/pkg/util/system"
2017-07-06 09:13:13 -04:00
taintutils "k8s.io/kubernetes/pkg/util/taints"
2014-10-08 19:14:37 -04:00
)
2016-08-16 11:08:26 -04:00
func init ( ) {
// Register prometheus metrics
Register ( )
}
2015-01-16 17:28:20 -05:00
var (
2017-08-08 19:25:20 -04:00
// UnreachableTaintTemplate is the taint for when a node becomes unreachable.
2017-02-06 07:58:48 -05:00
UnreachableTaintTemplate = & v1 . Taint {
2018-09-27 22:37:38 -04:00
Key : schedulerapi . TaintNodeUnreachable ,
2017-02-06 07:58:48 -05:00
Effect : v1 . TaintEffectNoExecute ,
}
2017-10-11 19:36:39 -04:00
2017-08-08 19:25:20 -04:00
// NotReadyTaintTemplate is the taint for when a node is not ready for
// executing pods
2017-02-06 07:58:48 -05:00
NotReadyTaintTemplate = & v1 . Taint {
2018-09-27 22:37:38 -04:00
Key : schedulerapi . TaintNodeNotReady ,
2017-02-06 07:58:48 -05:00
Effect : v1 . TaintEffectNoExecute ,
}
2017-07-19 11:51:19 -04:00
2018-08-22 18:26:46 -04:00
// map {NodeConditionType: {ConditionStatus: TaintKey}}
// represents which NodeConditionType under which ConditionStatus should be
// tainted with which TaintKey
// for certain NodeConditionType, there are multiple {ConditionStatus,TaintKey} pairs
nodeConditionToTaintKeyStatusMap = map [ v1 . NodeConditionType ] map [ v1 . ConditionStatus ] string {
2018-05-09 09:36:05 -04:00
v1 . NodeReady : {
2018-09-27 22:37:38 -04:00
v1 . ConditionFalse : schedulerapi . TaintNodeNotReady ,
v1 . ConditionUnknown : schedulerapi . TaintNodeUnreachable ,
2018-05-09 09:36:05 -04:00
} ,
v1 . NodeMemoryPressure : {
2018-09-27 22:37:38 -04:00
v1 . ConditionTrue : schedulerapi . TaintNodeMemoryPressure ,
2018-05-09 09:36:05 -04:00
} ,
v1 . NodeDiskPressure : {
2018-09-27 22:37:38 -04:00
v1 . ConditionTrue : schedulerapi . TaintNodeDiskPressure ,
2018-05-09 09:36:05 -04:00
} ,
v1 . NodeNetworkUnavailable : {
2018-09-27 22:37:38 -04:00
v1 . ConditionTrue : schedulerapi . TaintNodeNetworkUnavailable ,
2018-05-09 09:36:05 -04:00
} ,
v1 . NodePIDPressure : {
2018-09-27 22:37:38 -04:00
v1 . ConditionTrue : schedulerapi . TaintNodePIDPressure ,
2018-05-09 09:36:05 -04:00
} ,
2017-07-19 11:51:19 -04:00
}
taintKeyToNodeConditionMap = map [ string ] v1 . NodeConditionType {
2018-09-27 22:37:38 -04:00
schedulerapi . TaintNodeNotReady : v1 . NodeReady ,
schedulerapi . TaintNodeUnreachable : v1 . NodeReady ,
schedulerapi . TaintNodeNetworkUnavailable : v1 . NodeNetworkUnavailable ,
schedulerapi . TaintNodeMemoryPressure : v1 . NodeMemoryPressure ,
schedulerapi . TaintNodeDiskPressure : v1 . NodeDiskPressure ,
schedulerapi . TaintNodePIDPressure : v1 . NodePIDPressure ,
2017-07-19 11:51:19 -04:00
}
2015-01-16 17:28:20 -05:00
)
2017-08-08 19:25:20 -04:00
// ZoneState is the state of a given zone.
type ZoneState string
2016-07-12 03:38:57 -04:00
const (
2017-08-08 19:25:20 -04:00
stateInitial = ZoneState ( "Initial" )
stateNormal = ZoneState ( "Normal" )
stateFullDisruption = ZoneState ( "FullDisruption" )
statePartialDisruption = ZoneState ( "PartialDisruption" )
2016-07-12 03:38:57 -04:00
)
2017-10-11 19:36:39 -04:00
const (
2018-10-01 14:32:56 -04:00
// The amount of time the nodecontroller should sleep between retrying node health updates
2017-10-11 19:36:39 -04:00
retrySleepTime = 20 * time . Millisecond
)
2019-02-22 19:09:07 -05:00
// labelReconcileInfo lists Node labels to reconcile, and how to reconcile them.
// primaryKey and secondaryKey are keys of labels to reconcile.
// - If both keys exist, but their values don't match. Use the value from the
// primaryKey as the source of truth to reconcile.
// - If ensureSecondaryExists is true, and the secondaryKey does not
// exist, secondaryKey will be added with the value of the primaryKey.
var labelReconcileInfo = [ ] struct {
primaryKey string
secondaryKey string
ensureSecondaryExists bool
} {
{
// Reconcile the beta and the stable OS label using the beta label as
// the source of truth.
// TODO(#73084): switch to using the stable label as the source of
// truth in v1.18.
primaryKey : kubeletapis . LabelOS ,
secondaryKey : v1 . LabelOSStable ,
ensureSecondaryExists : true ,
} ,
{
// Reconcile the beta and the stable arch label using the beta label as
// the source of truth.
// TODO(#73084): switch to using the stable label as the source of
// truth in v1.18.
primaryKey : kubeletapis . LabelArch ,
secondaryKey : v1 . LabelArchStable ,
ensureSecondaryExists : true ,
} ,
}
2018-10-01 14:32:56 -04:00
type nodeHealthData struct {
2016-12-03 13:57:26 -05:00
probeTimestamp metav1 . Time
readyTransitionTimestamp metav1 . Time
2018-10-01 14:32:56 -04:00
status * v1 . NodeStatus
2018-10-01 14:32:56 -04:00
lease * coordv1beta1 . Lease
2015-03-31 11:15:39 -04:00
}
2017-10-11 19:36:39 -04:00
// Controller is the controller that manages node's life cycle.
2017-08-08 19:25:20 -04:00
type Controller struct {
2017-10-11 19:36:39 -04:00
taintManager * scheduler . NoExecuteTaintManager
podInformerSynced cache . InformerSynced
kubeClient clientset . Interface
// This timestamp is to be used instead of LastProbeTime stored in Condition. We do this
2018-11-11 20:43:53 -05:00
// to avoid the problem with time skew across the cluster.
2017-10-11 19:36:39 -04:00
now func ( ) metav1 . Time
enterPartialDisruptionFunc func ( nodeNum int ) float32
enterFullDisruptionFunc func ( nodeNum int ) float32
computeZoneStateFunc func ( nodeConditions [ ] * v1 . NodeCondition ) ( int , ZoneState )
2017-02-27 03:33:55 -05:00
knownNodeSet map [ string ] * v1 . Node
2018-10-01 14:32:56 -04:00
// per Node map storing last observed health together with a local time when it was observed.
nodeHealthMap map [ string ] * nodeHealthData
2017-10-11 19:36:39 -04:00
// Lock to access evictor workers
evictorLock sync . Mutex
// workers that evicts pods from unresponsive nodes.
zonePodEvictor map [ string ] * scheduler . RateLimitedTimedQueue
// workers that are responsible for tainting nodes.
zoneNoExecuteTainter map [ string ] * scheduler . RateLimitedTimedQueue
zoneStates map [ string ] ZoneState
2018-08-15 22:06:39 -04:00
daemonSetStore appsv1listers . DaemonSetLister
2017-10-11 19:36:39 -04:00
daemonSetInformerSynced cache . InformerSynced
2018-10-28 21:57:23 -04:00
leaseLister coordlisters . LeaseLister
leaseInformerSynced cache . InformerSynced
nodeLister corelisters . NodeLister
nodeInformerSynced cache . InformerSynced
2017-10-11 19:36:39 -04:00
recorder record . EventRecorder
// Value controlling Controller monitoring period, i.e. how often does Controller
2018-10-01 14:32:56 -04:00
// check node health signal posted from kubelet. This value should be lower than
// nodeMonitorGracePeriod.
// TODO: Change node health monitor to watch based.
2017-10-11 19:36:39 -04:00
nodeMonitorPeriod time . Duration
2018-10-01 14:32:56 -04:00
// When node is just created, e.g. cluster bootstrap or node creation, we give
// a longer grace period.
2017-10-11 19:36:39 -04:00
nodeStartupGracePeriod time . Duration
2018-10-01 14:32:56 -04:00
// Controller will not proactively sync node health, but will monitor node
2018-10-01 14:32:56 -04:00
// health signal updated from kubelet. There are 2 kinds of node healthiness
// signals: NodeStatus and NodeLease. NodeLease signal is generated only when
// NodeLease feature is enabled. If it doesn't receive update for this amount
// of time, it will start posting "NodeReady==ConditionUnknown". The amount of
// time before which Controller start evicting pods is controlled via flag
// 'pod-eviction-timeout'.
2018-10-01 14:32:56 -04:00
// Note: be cautious when changing the constant, it must work with
2018-10-01 14:32:56 -04:00
// nodeStatusUpdateFrequency in kubelet and renewInterval in NodeLease
// controller. The node health signal update frequency is the minimal of the
// two.
// There are several constraints:
// 1. nodeMonitorGracePeriod must be N times more than the node health signal
// update frequency, where N means number of retries allowed for kubelet to
// post node status/lease. It is pointless to make nodeMonitorGracePeriod
// be less than the node health signal update frequency, since there will
// only be fresh values from Kubelet at an interval of node health signal
// update frequency. The constant must be less than podEvictionTimeout.
2018-10-01 14:32:56 -04:00
// 2. nodeMonitorGracePeriod can't be too large for user experience - larger
// value takes longer for user to see up-to-date node health.
2015-03-31 07:17:12 -04:00
nodeMonitorGracePeriod time . Duration
2016-05-16 05:20:23 -04:00
2017-10-11 19:36:39 -04:00
podEvictionTimeout time . Duration
2016-08-05 08:50:19 -04:00
evictionLimiterQPS float32
secondaryEvictionLimiterQPS float32
largeClusterThreshold int32
unhealthyZoneThreshold float32
2017-01-23 04:28:51 -05:00
2017-08-08 19:25:20 -04:00
// if set to true Controller will start TaintManager that will evict Pods from
2017-01-23 04:28:51 -05:00
// tainted nodes, if they're not tolerated.
runTaintManager bool
2017-02-06 07:58:48 -05:00
2017-08-08 19:25:20 -04:00
// if set to true Controller will taint Nodes with 'TaintNodeNotReady' and 'TaintNodeUnreachable'
2017-02-06 07:58:48 -05:00
// taints instead of evicting Pods itself.
useTaintBasedEvictions bool
2017-07-19 11:51:19 -04:00
// if set to true, NodeController will taint Nodes based on its condition for 'NetworkUnavailable',
2018-12-13 03:31:46 -05:00
// 'MemoryPressure', 'PIDPressure' and 'DiskPressure'.
2017-07-19 11:51:19 -04:00
taintNodeByCondition bool
2018-08-31 03:26:19 -04:00
2018-09-10 20:24:16 -04:00
nodeUpdateQueue workqueue . Interface
2014-10-08 19:14:37 -04:00
}
2017-10-11 19:36:39 -04:00
// NewNodeLifecycleController returns a new taint controller.
2018-10-01 14:32:56 -04:00
func NewNodeLifecycleController (
leaseInformer coordinformers . LeaseInformer ,
podInformer coreinformers . PodInformer ,
2017-02-06 13:35:50 -05:00
nodeInformer coreinformers . NodeInformer ,
2018-08-15 22:06:39 -04:00
daemonSetInformer appsv1informers . DaemonSetInformer ,
2016-01-29 01:34:08 -05:00
kubeClient clientset . Interface ,
2017-10-11 19:36:39 -04:00
nodeMonitorPeriod time . Duration ,
nodeStartupGracePeriod time . Duration ,
nodeMonitorGracePeriod time . Duration ,
2015-04-02 11:13:13 -04:00
podEvictionTimeout time . Duration ,
2016-07-12 08:29:46 -04:00
evictionLimiterQPS float32 ,
2016-08-05 08:50:19 -04:00
secondaryEvictionLimiterQPS float32 ,
largeClusterThreshold int32 ,
unhealthyZoneThreshold float32 ,
2017-02-06 07:58:48 -05:00
runTaintManager bool ,
2017-07-19 11:51:19 -04:00
useTaintBasedEvictions bool ,
2017-08-08 19:25:20 -04:00
taintNodeByCondition bool ) ( * Controller , error ) {
if kubeClient == nil {
2018-11-09 13:49:10 -05:00
klog . Fatalf ( "kubeClient is nil when starting Controller" )
2017-08-08 19:25:20 -04:00
}
2015-04-08 07:45:37 -04:00
eventBroadcaster := record . NewBroadcaster ( )
2017-08-31 20:25:18 -04:00
recorder := eventBroadcaster . NewRecorder ( scheme . Scheme , v1 . EventSource { Component : "node-controller" } )
2018-11-09 13:49:10 -05:00
eventBroadcaster . StartLogging ( klog . Infof )
2018-10-19 17:19:23 -04:00
2018-11-09 13:49:10 -05:00
klog . Infof ( "Sending events to api server." )
2018-10-19 17:19:23 -04:00
eventBroadcaster . StartRecordingToSink (
& v1core . EventSinkImpl {
Interface : v1core . New ( kubeClient . CoreV1 ( ) . RESTClient ( ) ) . Events ( "" ) ,
} )
2016-04-13 14:38:32 -04:00
2018-10-24 09:48:06 -04:00
if kubeClient . CoreV1 ( ) . RESTClient ( ) . GetRateLimiter ( ) != nil {
2017-10-11 19:36:39 -04:00
metrics . RegisterMetricAndTrackRateLimiterUsage ( "node_lifecycle_controller" , kubeClient . CoreV1 ( ) . RESTClient ( ) . GetRateLimiter ( ) )
2015-05-06 17:48:45 -04:00
}
2015-10-19 22:25:31 -04:00
2017-08-08 19:25:20 -04:00
nc := & Controller {
2018-10-28 21:57:23 -04:00
kubeClient : kubeClient ,
now : metav1 . Now ,
knownNodeSet : make ( map [ string ] * v1 . Node ) ,
nodeHealthMap : make ( map [ string ] * nodeHealthData ) ,
2017-10-11 19:36:39 -04:00
recorder : recorder ,
nodeMonitorPeriod : nodeMonitorPeriod ,
nodeStartupGracePeriod : nodeStartupGracePeriod ,
nodeMonitorGracePeriod : nodeMonitorGracePeriod ,
zonePodEvictor : make ( map [ string ] * scheduler . RateLimitedTimedQueue ) ,
zoneNoExecuteTainter : make ( map [ string ] * scheduler . RateLimitedTimedQueue ) ,
zoneStates : make ( map [ string ] ZoneState ) ,
podEvictionTimeout : podEvictionTimeout ,
2017-07-31 07:39:34 -04:00
evictionLimiterQPS : evictionLimiterQPS ,
secondaryEvictionLimiterQPS : secondaryEvictionLimiterQPS ,
largeClusterThreshold : largeClusterThreshold ,
unhealthyZoneThreshold : unhealthyZoneThreshold ,
runTaintManager : runTaintManager ,
useTaintBasedEvictions : useTaintBasedEvictions && runTaintManager ,
2017-09-30 20:26:35 -04:00
taintNodeByCondition : taintNodeByCondition ,
2018-12-03 13:15:52 -05:00
nodeUpdateQueue : workqueue . NewNamed ( "node_lifecycle_controller" ) ,
2014-10-14 18:45:09 -04:00
}
2017-03-08 02:03:57 -05:00
if useTaintBasedEvictions {
2018-11-09 13:49:10 -05:00
klog . Infof ( "Controller is using taint based evictions." )
2017-03-08 02:03:57 -05:00
}
2017-10-11 19:36:39 -04:00
2016-08-05 08:50:19 -04:00
nc . enterPartialDisruptionFunc = nc . ReducedQPSFunc
nc . enterFullDisruptionFunc = nc . HealthyQPSFunc
nc . computeZoneStateFunc = nc . ComputeZoneState
2015-10-19 22:25:31 -04:00
2016-09-23 12:01:58 -04:00
podInformer . Informer ( ) . AddEventHandler ( cache . ResourceEventHandlerFuncs {
2017-01-23 04:28:51 -05:00
AddFunc : func ( obj interface { } ) {
pod := obj . ( * v1 . Pod )
if nc . taintManager != nil {
nc . taintManager . PodUpdated ( nil , pod )
}
} ,
UpdateFunc : func ( prev , obj interface { } ) {
prevPod := prev . ( * v1 . Pod )
newPod := obj . ( * v1 . Pod )
if nc . taintManager != nil {
nc . taintManager . PodUpdated ( prevPod , newPod )
}
} ,
DeleteFunc : func ( obj interface { } ) {
pod , isPod := obj . ( * v1 . Pod )
2017-05-30 14:32:43 -04:00
// We can get DeletedFinalStateUnknown instead of *v1.Pod here and we need to handle that correctly.
2017-01-23 04:28:51 -05:00
if ! isPod {
deletedState , ok := obj . ( cache . DeletedFinalStateUnknown )
if ! ok {
2018-11-09 13:49:10 -05:00
klog . Errorf ( "Received unexpected object: %v" , obj )
2017-01-23 04:28:51 -05:00
return
}
pod , ok = deletedState . Obj . ( * v1 . Pod )
if ! ok {
2018-11-09 13:49:10 -05:00
klog . Errorf ( "DeletedFinalStateUnknown contained non-Pod object: %v" , deletedState . Obj )
2017-01-23 04:28:51 -05:00
return
}
}
if nc . taintManager != nil {
nc . taintManager . PodUpdated ( pod , nil )
}
} ,
2016-07-20 16:26:07 -04:00
} )
2017-02-06 13:35:50 -05:00
nc . podInformerSynced = podInformer . Informer ( ) . HasSynced
2016-01-26 22:53:09 -05:00
2017-01-23 04:28:51 -05:00
if nc . runTaintManager {
2018-10-16 12:00:21 -04:00
podLister := podInformer . Lister ( )
podGetter := func ( name , namespace string ) ( * v1 . Pod , error ) { return podLister . Pods ( namespace ) . Get ( name ) }
nodeLister := nodeInformer . Lister ( )
nodeGetter := func ( name string ) ( * v1 . Node , error ) { return nodeLister . Get ( name ) }
nc . taintManager = scheduler . NewNoExecuteTaintManager ( kubeClient , podGetter , nodeGetter )
2017-05-05 06:01:08 -04:00
nodeInformer . Informer ( ) . AddEventHandler ( cache . ResourceEventHandlerFuncs {
2017-10-11 19:36:39 -04:00
AddFunc : nodeutil . CreateAddNodeHandler ( func ( node * v1 . Node ) error {
2017-05-05 06:01:08 -04:00
nc . taintManager . NodeUpdated ( nil , node )
return nil
} ) ,
2017-10-11 19:36:39 -04:00
UpdateFunc : nodeutil . CreateUpdateNodeHandler ( func ( oldNode , newNode * v1 . Node ) error {
2017-05-05 06:01:08 -04:00
nc . taintManager . NodeUpdated ( oldNode , newNode )
return nil
} ) ,
2017-10-11 19:36:39 -04:00
DeleteFunc : nodeutil . CreateDeleteNodeHandler ( func ( node * v1 . Node ) error {
2017-05-05 06:01:08 -04:00
nc . taintManager . NodeUpdated ( node , nil )
return nil
} ) ,
} )
2017-01-23 04:28:51 -05:00
}
2019-02-22 19:09:07 -05:00
klog . Infof ( "Controller will reconcile labels." )
nodeInformer . Informer ( ) . AddEventHandler ( cache . ResourceEventHandlerFuncs {
AddFunc : nodeutil . CreateAddNodeHandler ( func ( node * v1 . Node ) error {
nc . nodeUpdateQueue . Add ( node . Name )
return nil
} ) ,
UpdateFunc : nodeutil . CreateUpdateNodeHandler ( func ( _ , newNode * v1 . Node ) error {
nc . nodeUpdateQueue . Add ( newNode . Name )
return nil
} ) ,
} )
2017-07-19 11:51:19 -04:00
if nc . taintNodeByCondition {
2018-11-09 13:49:10 -05:00
klog . Infof ( "Controller will taint node by condition." )
2017-07-19 11:51:19 -04:00
}
2018-10-01 14:32:56 -04:00
nc . leaseLister = leaseInformer . Lister ( )
2018-10-17 11:09:46 -04:00
if utilfeature . DefaultFeatureGate . Enabled ( features . NodeLease ) {
nc . leaseInformerSynced = leaseInformer . Informer ( ) . HasSynced
} else {
// Always indicate that lease is synced to prevent syncing lease.
nc . leaseInformerSynced = func ( ) bool { return true }
}
2018-10-01 14:32:56 -04:00
2017-02-06 13:35:50 -05:00
nc . nodeLister = nodeInformer . Lister ( )
nc . nodeInformerSynced = nodeInformer . Informer ( ) . HasSynced
2016-01-26 22:53:09 -05:00
2017-02-06 13:35:50 -05:00
nc . daemonSetStore = daemonSetInformer . Lister ( )
nc . daemonSetInformerSynced = daemonSetInformer . Informer ( ) . HasSynced
2016-01-26 22:53:09 -05:00
2016-07-16 14:52:51 -04:00
return nc , nil
2014-10-14 18:45:09 -04:00
}
2017-10-11 19:36:39 -04:00
// Run starts an asynchronous loop that monitors the status of cluster nodes.
func ( nc * Controller ) Run ( stopCh <- chan struct { } ) {
defer utilruntime . HandleCrash ( )
2018-11-09 13:49:10 -05:00
klog . Infof ( "Starting node controller" )
defer klog . Infof ( "Shutting down node controller" )
2017-10-11 19:36:39 -04:00
2018-10-01 14:32:56 -04:00
if ! controller . WaitForCacheSync ( "taint" , stopCh , nc . leaseInformerSynced , nc . nodeInformerSynced , nc . podInformerSynced , nc . daemonSetInformerSynced ) {
2017-10-11 19:36:39 -04:00
return
}
if nc . runTaintManager {
2018-08-31 03:26:19 -04:00
go nc . taintManager . Run ( stopCh )
}
2019-02-22 19:09:07 -05:00
// Close node update queue to cleanup go routine.
defer nc . nodeUpdateQueue . ShutDown ( )
// Start workers to reconcile labels and/or update NoSchedule taint for nodes.
for i := 0 ; i < scheduler . UpdateWorkerSize ; i ++ {
// Thanks to "workqueue", each worker just need to get item from queue, because
// the item is flagged when got from queue: if new event come, the new item will
// be re-queued until "Done", so no more than one worker handle the same item and
// no event missed.
go wait . Until ( nc . doNodeProcessingPassWorker , time . Second , stopCh )
2017-10-11 19:36:39 -04:00
}
if nc . useTaintBasedEvictions {
// Handling taint based evictions. Because we don't want a dedicated logic in TaintManager for NC-originated
// taints and we normally don't rate limit evictions caused by taints, we need to rate limit adding taints.
2018-08-31 03:26:19 -04:00
go wait . Until ( nc . doNoExecuteTaintingPass , scheduler . NodeEvictionPeriod , stopCh )
2017-10-11 19:36:39 -04:00
} else {
// Managing eviction of nodes:
// When we delete pods off a node, if the node was not empty at the time we then
// queue an eviction watcher. If we hit an error, retry deletion.
2018-08-31 03:26:19 -04:00
go wait . Until ( nc . doEvictionPass , scheduler . NodeEvictionPeriod , stopCh )
2017-04-04 09:35:44 -04:00
}
2017-10-11 19:36:39 -04:00
2018-10-01 14:32:56 -04:00
// Incorporate the results of node health signal pushed from kubelet to master.
2017-10-11 19:36:39 -04:00
go wait . Until ( func ( ) {
2018-10-01 14:32:56 -04:00
if err := nc . monitorNodeHealth ( ) ; err != nil {
2018-11-09 13:49:10 -05:00
klog . Errorf ( "Error monitoring node health: %v" , err )
2017-10-11 19:36:39 -04:00
}
2018-08-31 03:26:19 -04:00
} , nc . nodeMonitorPeriod , stopCh )
2017-10-11 19:36:39 -04:00
<- stopCh
2017-04-04 09:35:44 -04:00
}
2019-02-22 19:09:07 -05:00
func ( nc * Controller ) doNodeProcessingPassWorker ( ) {
2018-08-31 03:26:19 -04:00
for {
2018-09-10 20:24:16 -04:00
obj , shutdown := nc . nodeUpdateQueue . Get ( )
// "nodeUpdateQueue" will be shutdown when "stopCh" closed;
// we do not need to re-check "stopCh" again.
if shutdown {
2018-08-31 03:26:19 -04:00
return
}
2018-09-10 20:24:16 -04:00
nodeName := obj . ( string )
2019-02-22 19:09:07 -05:00
if nc . taintNodeByCondition {
if err := nc . doNoScheduleTaintingPass ( nodeName ) ; err != nil {
klog . Errorf ( "Failed to taint NoSchedule on node <%s>, requeue it: %v" , nodeName , err )
// TODO(k82cn): Add nodeName back to the queue
}
}
// TODO: re-evaluate whether there are any labels that need to be
// reconcile in 1.19. Remove this function if it's no longer necessary.
if err := nc . reconcileNodeLabels ( nodeName ) ; err != nil {
klog . Errorf ( "Failed to reconcile labels for node <%s>, requeue it: %v" , nodeName , err )
// TODO(yujuhong): Add nodeName back to the queue
2018-09-10 20:24:16 -04:00
}
nc . nodeUpdateQueue . Done ( nodeName )
2018-08-31 03:26:19 -04:00
}
}
2018-09-10 20:24:16 -04:00
func ( nc * Controller ) doNoScheduleTaintingPass ( nodeName string ) error {
node , err := nc . nodeLister . Get ( nodeName )
if err != nil {
// If node not found, just ignore it.
if apierrors . IsNotFound ( err ) {
return nil
}
return err
}
2017-07-19 11:51:19 -04:00
// Map node's condition to Taints.
2018-05-14 02:55:42 -04:00
var taints [ ] v1 . Taint
2017-07-19 11:51:19 -04:00
for _ , condition := range node . Status . Conditions {
2018-08-22 18:26:46 -04:00
if taintMap , found := nodeConditionToTaintKeyStatusMap [ condition . Type ] ; found {
if taintKey , found := taintMap [ condition . Status ] ; found {
2017-07-19 11:51:19 -04:00
taints = append ( taints , v1 . Taint {
2018-08-22 18:26:46 -04:00
Key : taintKey ,
2017-07-19 11:51:19 -04:00
Effect : v1 . TaintEffectNoSchedule ,
} )
}
}
}
2018-03-14 03:08:17 -04:00
if node . Spec . Unschedulable {
// If unschedulable, append related taint.
taints = append ( taints , v1 . Taint {
2018-09-27 22:37:38 -04:00
Key : schedulerapi . TaintNodeUnschedulable ,
2018-03-14 03:08:17 -04:00
Effect : v1 . TaintEffectNoSchedule ,
} )
}
// Get exist taints of node.
2017-07-19 11:51:19 -04:00
nodeTaints := taintutils . TaintSetFilter ( node . Spec . Taints , func ( t * v1 . Taint ) bool {
2018-08-22 18:26:46 -04:00
// only NoSchedule taints are candidates to be compared with "taints" later
if t . Effect != v1 . TaintEffectNoSchedule {
return false
}
2018-03-14 03:08:17 -04:00
// Find unschedulable taint of node.
2018-09-27 22:37:38 -04:00
if t . Key == schedulerapi . TaintNodeUnschedulable {
2018-03-14 03:08:17 -04:00
return true
}
// Find node condition taints of node.
2017-07-19 11:51:19 -04:00
_ , found := taintKeyToNodeConditionMap [ t . Key ]
return found
} )
taintsToAdd , taintsToDel := taintutils . TaintSetDiff ( taints , nodeTaints )
// If nothing to add not delete, return true directly.
if len ( taintsToAdd ) == 0 && len ( taintsToDel ) == 0 {
return nil
}
2017-10-11 19:36:39 -04:00
if ! nodeutil . SwapNodeControllerTaint ( nc . kubeClient , taintsToAdd , taintsToDel , node ) {
2017-07-19 11:51:19 -04:00
return fmt . Errorf ( "failed to swap taints of node %+v" , node )
}
return nil
}
2017-08-08 19:25:20 -04:00
func ( nc * Controller ) doNoExecuteTaintingPass ( ) {
2017-04-04 09:35:44 -04:00
nc . evictorLock . Lock ( )
defer nc . evictorLock . Unlock ( )
2017-10-11 19:36:39 -04:00
for k := range nc . zoneNoExecuteTainter {
2017-04-04 09:35:44 -04:00
// Function should return 'false' and a time after which it should be retried, or 'true' if it shouldn't (it succeeded).
2017-10-11 19:36:39 -04:00
nc . zoneNoExecuteTainter [ k ] . Try ( func ( value scheduler . TimedValue ) ( bool , time . Duration ) {
2017-04-04 09:35:44 -04:00
node , err := nc . nodeLister . Get ( value . Value )
if apierrors . IsNotFound ( err ) {
2018-11-09 13:49:10 -05:00
klog . Warningf ( "Node %v no longer present in nodeLister!" , value . Value )
2017-04-04 09:35:44 -04:00
return true , 0
} else if err != nil {
2018-11-09 13:49:10 -05:00
klog . Warningf ( "Failed to get Node %v from the nodeLister: %v" , value . Value , err )
2017-04-04 09:35:44 -04:00
// retry in 50 millisecond
return false , 50 * time . Millisecond
}
2019-02-26 14:05:32 -05:00
_ , condition := nodeutil . GetNodeCondition ( & node . Status , v1 . NodeReady )
2017-04-04 09:35:44 -04:00
// Because we want to mimic NodeStatus.Condition["Ready"] we make "unreachable" and "not ready" taints mutually exclusive.
taintToAdd := v1 . Taint { }
oppositeTaint := v1 . Taint { }
if condition . Status == v1 . ConditionFalse {
taintToAdd = * NotReadyTaintTemplate
oppositeTaint = * UnreachableTaintTemplate
} else if condition . Status == v1 . ConditionUnknown {
taintToAdd = * UnreachableTaintTemplate
oppositeTaint = * NotReadyTaintTemplate
} else {
// It seems that the Node is ready again, so there's no need to taint it.
2018-11-09 13:49:10 -05:00
klog . V ( 4 ) . Infof ( "Node %v was in a taint queue, but it's ready now. Ignoring taint request." , value . Value )
2017-04-04 09:35:44 -04:00
return true , 0
}
2019-01-14 22:07:14 -05:00
result := nodeutil . SwapNodeControllerTaint ( nc . kubeClient , [ ] * v1 . Taint { & taintToAdd } , [ ] * v1 . Taint { & oppositeTaint } , node )
if result {
//count the evictionsNumber
zone := utilnode . GetZoneKey ( node )
evictionsNumber . WithLabelValues ( zone ) . Inc ( )
}
return result , 0
2017-04-04 09:35:44 -04:00
} )
}
}
2017-10-11 19:36:39 -04:00
func ( nc * Controller ) doEvictionPass ( ) {
nc . evictorLock . Lock ( )
defer nc . evictorLock . Unlock ( )
for k := range nc . zonePodEvictor {
// Function should return 'false' and a time after which it should be retried, or 'true' if it shouldn't (it succeeded).
nc . zonePodEvictor [ k ] . Try ( func ( value scheduler . TimedValue ) ( bool , time . Duration ) {
node , err := nc . nodeLister . Get ( value . Value )
if apierrors . IsNotFound ( err ) {
2018-11-09 13:49:10 -05:00
klog . Warningf ( "Node %v no longer present in nodeLister!" , value . Value )
2017-10-11 19:36:39 -04:00
} else if err != nil {
2018-11-09 13:49:10 -05:00
klog . Warningf ( "Failed to get Node %v from the nodeLister: %v" , value . Value , err )
2017-10-11 19:36:39 -04:00
}
nodeUID , _ := value . UID . ( string )
remaining , err := nodeutil . DeletePods ( nc . kubeClient , nc . recorder , value . Value , nodeUID , nc . daemonSetStore )
if err != nil {
utilruntime . HandleError ( fmt . Errorf ( "unable to evict node %q: %v" , value . Value , err ) )
return false , 0
}
if remaining {
2018-11-09 13:49:10 -05:00
klog . Infof ( "Pods awaiting deletion due to Controller eviction" )
2017-10-11 19:36:39 -04:00
}
2019-01-14 22:07:14 -05:00
//count the evictionsNumber
if node != nil {
zone := utilnode . GetZoneKey ( node )
evictionsNumber . WithLabelValues ( zone ) . Inc ( )
}
2017-10-11 19:36:39 -04:00
return true , 0
} )
}
}
2016-10-14 15:36:31 -04:00
2018-10-01 14:32:56 -04:00
// monitorNodeHealth verifies node health are constantly updated by kubelet, and
// if not, post "NodeReady==ConditionUnknown". It also evicts all pods if node
// is not ready or not reachable for a long period of time.
func ( nc * Controller ) monitorNodeHealth ( ) error {
2017-10-11 19:36:39 -04:00
// We are listing nodes from local cache as we can tolerate some small delays
// comparing to state from etcd and there is eventual consistency anyway.
nodes , err := nc . nodeLister . List ( labels . Everything ( ) )
if err != nil {
return err
}
added , deleted , newZoneRepresentatives := nc . classifyNodes ( nodes )
2015-10-19 22:25:31 -04:00
2017-10-11 19:36:39 -04:00
for i := range newZoneRepresentatives {
nc . addPodEvictorForNewZone ( newZoneRepresentatives [ i ] )
2017-04-12 15:49:17 -04:00
}
2016-10-14 15:36:31 -04:00
2017-10-11 19:36:39 -04:00
for i := range added {
2018-11-09 13:49:10 -05:00
klog . V ( 1 ) . Infof ( "Controller observed a new Node: %#v" , added [ i ] . Name )
2017-10-11 19:36:39 -04:00
nodeutil . RecordNodeEvent ( nc . recorder , added [ i ] . Name , string ( added [ i ] . UID ) , v1 . EventTypeNormal , "RegisteredNode" , fmt . Sprintf ( "Registered Node %v in Controller" , added [ i ] . Name ) )
nc . knownNodeSet [ added [ i ] . Name ] = added [ i ]
nc . addPodEvictorForNewZone ( added [ i ] )
if nc . useTaintBasedEvictions {
nc . markNodeAsReachable ( added [ i ] )
} else {
nc . cancelPodEviction ( added [ i ] )
2017-01-23 04:28:51 -05:00
}
2017-10-11 19:36:39 -04:00
}
2017-01-23 04:28:51 -05:00
2017-10-11 19:36:39 -04:00
for i := range deleted {
2018-11-09 13:49:10 -05:00
klog . V ( 1 ) . Infof ( "Controller observed a Node deletion: %v" , deleted [ i ] . Name )
2017-10-11 19:36:39 -04:00
nodeutil . RecordNodeEvent ( nc . recorder , deleted [ i ] . Name , string ( deleted [ i ] . UID ) , v1 . EventTypeNormal , "RemovingNode" , fmt . Sprintf ( "Removing Node %v from Controller" , deleted [ i ] . Name ) )
delete ( nc . knownNodeSet , deleted [ i ] . Name )
2015-08-05 09:22:13 -04:00
}
2016-11-18 15:50:17 -05:00
zoneToNodeConditions := map [ string ] [ ] * v1 . NodeCondition { }
2017-02-06 13:35:50 -05:00
for i := range nodes {
2015-08-04 08:44:14 -04:00
var gracePeriod time . Duration
2016-11-18 15:50:17 -05:00
var observedReadyCondition v1 . NodeCondition
var currentReadyCondition * v1 . NodeCondition
2017-08-15 08:14:21 -04:00
node := nodes [ i ] . DeepCopy ( )
2018-10-01 14:32:56 -04:00
if err := wait . PollImmediate ( retrySleepTime , retrySleepTime * scheduler . NodeHealthUpdateRetry , func ( ) ( bool , error ) {
gracePeriod , observedReadyCondition , currentReadyCondition , err = nc . tryUpdateNodeHealth ( node )
2015-08-04 08:44:14 -04:00
if err == nil {
2017-01-05 07:22:35 -05:00
return true , nil
2015-08-04 08:44:14 -04:00
}
name := node . Name
2017-10-25 11:54:32 -04:00
node , err = nc . kubeClient . CoreV1 ( ) . Nodes ( ) . Get ( name , metav1 . GetOptions { } )
2015-08-04 08:44:14 -04:00
if err != nil {
2018-11-09 13:49:10 -05:00
klog . Errorf ( "Failed while getting a Node to retry updating node health. Probably Node %s was deleted." , name )
2017-01-05 07:22:35 -05:00
return false , err
2015-08-04 08:44:14 -04:00
}
2017-01-05 07:22:35 -05:00
return false , nil
} ) ; err != nil {
2018-11-09 13:49:10 -05:00
klog . Errorf ( "Update health of Node '%v' from Controller error: %v. " +
2017-01-05 07:22:35 -05:00
"Skipping - no pods will be evicted." , node . Name , err )
2015-08-04 08:44:14 -04:00
continue
}
2017-01-05 07:22:35 -05:00
2016-07-13 10:57:22 -04:00
// We do not treat a master node as a part of the cluster for network disruption checking.
2016-11-18 15:50:17 -05:00
if ! system . IsMasterNode ( node . Name ) {
2016-07-12 03:38:57 -04:00
zoneToNodeConditions [ utilnode . GetZoneKey ( node ) ] = append ( zoneToNodeConditions [ utilnode . GetZoneKey ( node ) ] , currentReadyCondition )
}
2015-08-04 08:44:14 -04:00
decisionTimestamp := nc . now ( )
2016-05-16 05:20:23 -04:00
if currentReadyCondition != nil {
2015-08-04 08:44:14 -04:00
// Check eviction timeout against decisionTimestamp
2017-02-06 07:58:48 -05:00
if observedReadyCondition . Status == v1 . ConditionFalse {
if nc . useTaintBasedEvictions {
2017-04-04 09:35:44 -04:00
// We want to update the taint straight away if Node is already tainted with the UnreachableTaint
2017-07-06 09:13:13 -04:00
if taintutils . TaintExists ( node . Spec . Taints , UnreachableTaintTemplate ) {
2017-04-04 09:35:44 -04:00
taintToAdd := * NotReadyTaintTemplate
2017-10-11 19:36:39 -04:00
if ! nodeutil . SwapNodeControllerTaint ( nc . kubeClient , [ ] * v1 . Taint { & taintToAdd } , [ ] * v1 . Taint { UnreachableTaintTemplate } , node ) {
2018-11-09 13:49:10 -05:00
klog . Errorf ( "Failed to instantly swap UnreachableTaint to NotReadyTaint. Will try again in the next cycle." )
2017-04-04 09:35:44 -04:00
}
} else if nc . markNodeForTainting ( node ) {
2018-11-09 13:49:10 -05:00
klog . V ( 2 ) . Infof ( "Node %v is NotReady as of %v. Adding it to the Taint queue." ,
2017-02-06 07:58:48 -05:00
node . Name ,
decisionTimestamp ,
)
}
} else {
2018-10-01 14:32:56 -04:00
if decisionTimestamp . After ( nc . nodeHealthMap [ node . Name ] . readyTransitionTimestamp . Add ( nc . podEvictionTimeout ) ) {
2017-02-06 07:58:48 -05:00
if nc . evictPods ( node ) {
2018-11-09 13:49:10 -05:00
klog . V ( 2 ) . Infof ( "Node is NotReady. Adding Pods on Node %s to eviction queue: %v is later than %v + %v" ,
2017-02-06 07:58:48 -05:00
node . Name ,
decisionTimestamp ,
2018-10-01 14:32:56 -04:00
nc . nodeHealthMap [ node . Name ] . readyTransitionTimestamp ,
2017-02-06 07:58:48 -05:00
nc . podEvictionTimeout ,
)
}
}
2015-08-04 08:44:14 -04:00
}
}
2017-02-06 07:58:48 -05:00
if observedReadyCondition . Status == v1 . ConditionUnknown {
if nc . useTaintBasedEvictions {
2017-04-04 09:35:44 -04:00
// We want to update the taint straight away if Node is already tainted with the UnreachableTaint
2017-07-06 09:13:13 -04:00
if taintutils . TaintExists ( node . Spec . Taints , NotReadyTaintTemplate ) {
2017-04-04 09:35:44 -04:00
taintToAdd := * UnreachableTaintTemplate
2017-10-11 19:36:39 -04:00
if ! nodeutil . SwapNodeControllerTaint ( nc . kubeClient , [ ] * v1 . Taint { & taintToAdd } , [ ] * v1 . Taint { NotReadyTaintTemplate } , node ) {
2018-11-09 13:49:10 -05:00
klog . Errorf ( "Failed to instantly swap UnreachableTaint to NotReadyTaint. Will try again in the next cycle." )
2017-04-04 09:35:44 -04:00
}
} else if nc . markNodeForTainting ( node ) {
2018-11-09 13:49:10 -05:00
klog . V ( 2 ) . Infof ( "Node %v is unresponsive as of %v. Adding it to the Taint queue." ,
2017-02-06 07:58:48 -05:00
node . Name ,
decisionTimestamp ,
)
}
} else {
2018-10-01 14:32:56 -04:00
if decisionTimestamp . After ( nc . nodeHealthMap [ node . Name ] . probeTimestamp . Add ( nc . podEvictionTimeout ) ) {
2017-02-06 07:58:48 -05:00
if nc . evictPods ( node ) {
2018-11-09 13:49:10 -05:00
klog . V ( 2 ) . Infof ( "Node is unresponsive. Adding Pods on Node %s to eviction queues: %v is later than %v + %v" ,
2017-02-06 07:58:48 -05:00
node . Name ,
decisionTimestamp ,
2018-10-01 14:32:56 -04:00
nc . nodeHealthMap [ node . Name ] . readyTransitionTimestamp ,
2017-02-06 07:58:48 -05:00
nc . podEvictionTimeout - gracePeriod ,
)
}
}
2015-08-04 08:44:14 -04:00
}
}
2016-11-18 15:50:17 -05:00
if observedReadyCondition . Status == v1 . ConditionTrue {
2017-02-06 07:58:48 -05:00
if nc . useTaintBasedEvictions {
2017-07-19 11:51:19 -04:00
removed , err := nc . markNodeAsReachable ( node )
2017-02-06 07:58:48 -05:00
if err != nil {
2018-11-09 13:49:10 -05:00
klog . Errorf ( "Failed to remove taints from node %v. Will retry in next iteration." , node . Name )
2017-02-06 07:58:48 -05:00
}
if removed {
2018-11-09 13:49:10 -05:00
klog . V ( 2 ) . Infof ( "Node %s is healthy again, removing all taints" , node . Name )
2017-02-06 07:58:48 -05:00
}
} else {
if nc . cancelPodEviction ( node ) {
2018-11-09 13:49:10 -05:00
klog . V ( 2 ) . Infof ( "Node %s is ready again, cancelled pod eviction" , node . Name )
2017-02-06 07:58:48 -05:00
}
2015-08-04 08:44:14 -04:00
}
}
// Report node event.
2016-11-18 15:50:17 -05:00
if currentReadyCondition . Status != v1 . ConditionTrue && observedReadyCondition . Status == v1 . ConditionTrue {
2017-10-11 19:36:39 -04:00
nodeutil . RecordNodeStatusChange ( nc . recorder , node , "NodeNotReady" )
if err = nodeutil . MarkAllPodsNotReady ( nc . kubeClient , node ) ; err != nil {
2016-01-15 02:32:10 -05:00
utilruntime . HandleError ( fmt . Errorf ( "Unable to mark all pods NotReady on node %v: %v" , node . Name , err ) )
2015-11-24 17:46:17 -05:00
}
2015-08-04 08:44:14 -04:00
}
}
}
2017-02-06 13:35:50 -05:00
nc . handleDisruption ( zoneToNodeConditions , nodes )
2016-05-16 05:20:23 -04:00
2016-07-13 10:57:22 -04:00
return nil
}
2018-10-01 14:32:56 -04:00
// tryUpdateNodeHealth checks a given node's conditions and tries to update it. Returns grace period to
2017-07-15 08:22:55 -04:00
// which given node is entitled, state of current and last observed Ready Condition, and an error if it occurred.
2018-10-01 14:32:56 -04:00
func ( nc * Controller ) tryUpdateNodeHealth ( node * v1 . Node ) ( time . Duration , v1 . NodeCondition , * v1 . NodeCondition , error ) {
2015-03-30 08:44:02 -04:00
var err error
var gracePeriod time . Duration
2016-11-18 15:50:17 -05:00
var observedReadyCondition v1 . NodeCondition
2019-02-26 14:05:32 -05:00
_ , currentReadyCondition := nodeutil . GetNodeCondition ( & node . Status , v1 . NodeReady )
2016-05-16 05:20:23 -04:00
if currentReadyCondition == nil {
2015-03-30 08:44:02 -04:00
// If ready condition is nil, then kubelet (or nodecontroller) never posted node status.
2018-10-01 14:32:56 -04:00
// A fake ready condition is created, where LastHeartbeatTime and LastTransitionTime is set
2015-03-30 08:44:02 -04:00
// to node.CreationTimestamp to avoid handle the corner case.
2016-11-18 15:50:17 -05:00
observedReadyCondition = v1 . NodeCondition {
Type : v1 . NodeReady ,
Status : v1 . ConditionUnknown ,
2015-03-27 10:09:51 -04:00
LastHeartbeatTime : node . CreationTimestamp ,
2015-03-30 08:44:02 -04:00
LastTransitionTime : node . CreationTimestamp ,
}
2015-03-31 07:17:12 -04:00
gracePeriod = nc . nodeStartupGracePeriod
2018-10-01 14:32:56 -04:00
if _ , found := nc . nodeHealthMap [ node . Name ] ; found {
nc . nodeHealthMap [ node . Name ] . status = & node . Status
} else {
nc . nodeHealthMap [ node . Name ] = & nodeHealthData {
status : & node . Status ,
probeTimestamp : node . CreationTimestamp ,
readyTransitionTimestamp : node . CreationTimestamp ,
}
2015-03-31 11:15:39 -04:00
}
2015-03-30 08:44:02 -04:00
} else {
// If ready condition is not nil, make a copy of it, since we may modify it in place later.
2016-05-16 05:20:23 -04:00
observedReadyCondition = * currentReadyCondition
2015-03-31 07:17:12 -04:00
gracePeriod = nc . nodeMonitorGracePeriod
2015-03-30 08:44:02 -04:00
}
2018-10-01 14:32:56 -04:00
savedNodeHealth , found := nc . nodeHealthMap [ node . Name ]
2015-03-31 11:15:39 -04:00
// There are following cases to check:
// - both saved and new status have no Ready Condition set - we leave everything as it is,
2017-08-08 19:25:20 -04:00
// - saved status have no Ready Condition, but current one does - Controller was restarted with Node data already present in etcd,
2015-03-31 11:15:39 -04:00
// - saved status have some Ready Condition, but current one does not - it's an error, but we fill it up because that's probably a good thing to do,
// - both saved and current statuses have Ready Conditions and they have the same LastProbeTime - nothing happened on that Node, it may be
// unresponsive, so we leave it as it is,
// - both saved and current statuses have Ready Conditions, they have different LastProbeTimes, but the same Ready Condition State -
// everything's in order, no transition occurred, we update only probeTimestamp,
// - both saved and current statuses have Ready Conditions, different LastProbeTimes and different Ready Condition State -
// Ready Condition changed it state since we last seen it, so we update both probeTimestamp and readyTransitionTimestamp.
// TODO: things to consider:
2015-07-29 17:11:19 -04:00
// - if 'LastProbeTime' have gone back in time its probably an error, currently we ignore it,
2015-03-31 11:15:39 -04:00
// - currently only correct Ready State transition outside of Node Controller is marking it ready by Kubelet, we don't check
// if that's the case, but it does not seem necessary.
2016-11-18 15:50:17 -05:00
var savedCondition * v1 . NodeCondition
2018-10-01 14:32:56 -04:00
var savedLease * coordv1beta1 . Lease
2015-09-29 02:43:04 -04:00
if found {
2019-02-26 14:05:32 -05:00
_ , savedCondition = nodeutil . GetNodeCondition ( savedNodeHealth . status , v1 . NodeReady )
2018-10-01 14:32:56 -04:00
savedLease = savedNodeHealth . lease
2015-09-29 02:43:04 -04:00
}
2019-02-26 14:05:32 -05:00
_ , observedCondition := nodeutil . GetNodeCondition ( & node . Status , v1 . NodeReady )
2015-03-31 11:15:39 -04:00
if ! found {
2018-11-09 13:49:10 -05:00
klog . Warningf ( "Missing timestamp for Node %s. Assuming now as a timestamp." , node . Name )
2018-10-01 14:32:56 -04:00
savedNodeHealth = & nodeHealthData {
status : & node . Status ,
2015-03-31 11:15:39 -04:00
probeTimestamp : nc . now ( ) ,
readyTransitionTimestamp : nc . now ( ) ,
}
} else if savedCondition == nil && observedCondition != nil {
2018-11-09 13:49:10 -05:00
klog . V ( 1 ) . Infof ( "Creating timestamp entry for newly observed Node %s" , node . Name )
2018-10-01 14:32:56 -04:00
savedNodeHealth = & nodeHealthData {
status : & node . Status ,
2015-03-31 11:15:39 -04:00
probeTimestamp : nc . now ( ) ,
readyTransitionTimestamp : nc . now ( ) ,
}
} else if savedCondition != nil && observedCondition == nil {
2018-11-09 13:49:10 -05:00
klog . Errorf ( "ReadyCondition was removed from Status of Node %s" , node . Name )
2015-03-31 11:15:39 -04:00
// TODO: figure out what to do in this case. For now we do the same thing as above.
2018-10-01 14:32:56 -04:00
savedNodeHealth = & nodeHealthData {
status : & node . Status ,
2015-03-31 11:15:39 -04:00
probeTimestamp : nc . now ( ) ,
readyTransitionTimestamp : nc . now ( ) ,
}
2015-03-27 10:09:51 -04:00
} else if savedCondition != nil && observedCondition != nil && savedCondition . LastHeartbeatTime != observedCondition . LastHeartbeatTime {
2016-12-03 13:57:26 -05:00
var transitionTime metav1 . Time
2015-03-31 11:15:39 -04:00
// If ReadyCondition changed since the last time we checked, we update the transition timestamp to "now",
// otherwise we leave it as it is.
if savedCondition . LastTransitionTime != observedCondition . LastTransitionTime {
2018-11-09 13:49:10 -05:00
klog . V ( 3 ) . Infof ( "ReadyCondition for Node %s transitioned from %v to %v" , node . Name , savedCondition , observedCondition )
2015-03-31 11:15:39 -04:00
transitionTime = nc . now ( )
} else {
2018-10-01 14:32:56 -04:00
transitionTime = savedNodeHealth . readyTransitionTimestamp
2015-03-31 11:15:39 -04:00
}
2018-11-09 13:49:10 -05:00
if klog . V ( 5 ) {
klog . V ( 5 ) . Infof ( "Node %s ReadyCondition updated. Updating timestamp: %+v vs %+v." , node . Name , savedNodeHealth . status , node . Status )
2016-02-20 15:07:23 -05:00
} else {
2018-11-09 13:49:10 -05:00
klog . V ( 3 ) . Infof ( "Node %s ReadyCondition updated. Updating timestamp." , node . Name )
2016-02-20 15:07:23 -05:00
}
2018-10-01 14:32:56 -04:00
savedNodeHealth = & nodeHealthData {
status : & node . Status ,
2015-03-31 11:15:39 -04:00
probeTimestamp : nc . now ( ) ,
readyTransitionTimestamp : transitionTime ,
}
}
2018-10-01 14:32:56 -04:00
var observedLease * coordv1beta1 . Lease
if utilfeature . DefaultFeatureGate . Enabled ( features . NodeLease ) {
// Always update the probe time if node lease is renewed.
// Note: If kubelet never posted the node status, but continues renewing the
// heartbeat leases, the node controller will assume the node is healthy and
// take no action.
observedLease , _ = nc . leaseLister . Leases ( v1 . NamespaceNodeLease ) . Get ( node . Name )
if observedLease != nil && ( savedLease == nil || savedLease . Spec . RenewTime . Before ( observedLease . Spec . RenewTime ) ) {
savedNodeHealth . lease = observedLease
savedNodeHealth . probeTimestamp = nc . now ( )
}
}
2018-10-01 14:32:56 -04:00
nc . nodeHealthMap [ node . Name ] = savedNodeHealth
2015-03-31 11:15:39 -04:00
2018-10-01 14:32:56 -04:00
if nc . now ( ) . After ( savedNodeHealth . probeTimestamp . Add ( gracePeriod ) ) {
2018-10-01 14:32:56 -04:00
// NodeReady condition or lease was last set longer ago than gracePeriod, so
// update it to Unknown (regardless of its current value) in the master.
2016-05-16 05:20:23 -04:00
if currentReadyCondition == nil {
2018-11-09 13:49:10 -05:00
klog . V ( 2 ) . Infof ( "node %v is never updated by kubelet" , node . Name )
2016-11-18 15:50:17 -05:00
node . Status . Conditions = append ( node . Status . Conditions , v1 . NodeCondition {
Type : v1 . NodeReady ,
Status : v1 . ConditionUnknown ,
2015-09-11 06:08:09 -04:00
Reason : "NodeStatusNeverUpdated" ,
Message : fmt . Sprintf ( "Kubelet never posted node status." ) ,
2015-03-27 10:09:51 -04:00
LastHeartbeatTime : node . CreationTimestamp ,
2015-03-30 08:44:02 -04:00
LastTransitionTime : nc . now ( ) ,
} )
} else {
2018-11-09 13:49:10 -05:00
klog . V ( 4 ) . Infof ( "node %v hasn't been updated for %+v. Last ready condition is: %+v" ,
2018-10-01 14:32:56 -04:00
node . Name , nc . now ( ) . Time . Sub ( savedNodeHealth . probeTimestamp . Time ) , observedReadyCondition )
2016-11-18 15:50:17 -05:00
if observedReadyCondition . Status != v1 . ConditionUnknown {
currentReadyCondition . Status = v1 . ConditionUnknown
2016-05-16 05:20:23 -04:00
currentReadyCondition . Reason = "NodeStatusUnknown"
2016-11-10 13:09:27 -05:00
currentReadyCondition . Message = "Kubelet stopped posting node status."
2015-03-30 08:44:02 -04:00
// LastProbeTime is the last time we heard from kubelet.
2016-05-16 05:20:23 -04:00
currentReadyCondition . LastHeartbeatTime = observedReadyCondition . LastHeartbeatTime
currentReadyCondition . LastTransitionTime = nc . now ( )
2015-03-30 08:44:02 -04:00
}
}
2015-10-22 15:47:43 -04:00
2016-11-10 13:09:27 -05:00
// remaining node conditions should also be set to Unknown
2017-07-15 08:22:55 -04:00
remainingNodeConditionTypes := [ ] v1 . NodeConditionType {
v1 . NodeMemoryPressure ,
v1 . NodeDiskPressure ,
2018-09-13 20:50:05 -04:00
v1 . NodePIDPressure ,
2017-07-15 08:22:55 -04:00
// We don't change 'NodeNetworkUnavailable' condition, as it's managed on a control plane level.
// v1.NodeNetworkUnavailable,
}
2016-11-10 13:09:27 -05:00
nowTimestamp := nc . now ( )
for _ , nodeConditionType := range remainingNodeConditionTypes {
2019-02-26 14:05:32 -05:00
_ , currentCondition := nodeutil . GetNodeCondition ( & node . Status , nodeConditionType )
2016-11-10 13:09:27 -05:00
if currentCondition == nil {
2018-11-09 13:49:10 -05:00
klog . V ( 2 ) . Infof ( "Condition %v of node %v was never updated by kubelet" , nodeConditionType , node . Name )
2016-11-10 13:09:27 -05:00
node . Status . Conditions = append ( node . Status . Conditions , v1 . NodeCondition {
Type : nodeConditionType ,
Status : v1 . ConditionUnknown ,
Reason : "NodeStatusNeverUpdated" ,
Message : "Kubelet never posted node status." ,
LastHeartbeatTime : node . CreationTimestamp ,
LastTransitionTime : nowTimestamp ,
} )
} else {
2018-11-09 13:49:10 -05:00
klog . V ( 4 ) . Infof ( "node %v hasn't been updated for %+v. Last %v is: %+v" ,
2018-10-01 14:32:56 -04:00
node . Name , nc . now ( ) . Time . Sub ( savedNodeHealth . probeTimestamp . Time ) , nodeConditionType , currentCondition )
2016-11-10 13:09:27 -05:00
if currentCondition . Status != v1 . ConditionUnknown {
currentCondition . Status = v1 . ConditionUnknown
currentCondition . Reason = "NodeStatusUnknown"
currentCondition . Message = "Kubelet stopped posting node status."
currentCondition . LastTransitionTime = nowTimestamp
}
2015-10-22 15:47:43 -04:00
}
}
2019-02-26 14:05:32 -05:00
_ , currentCondition := nodeutil . GetNodeCondition ( & node . Status , v1 . NodeReady )
2017-01-25 08:39:54 -05:00
if ! apiequality . Semantic . DeepEqual ( currentCondition , & observedReadyCondition ) {
2017-10-25 11:54:32 -04:00
if _ , err = nc . kubeClient . CoreV1 ( ) . Nodes ( ) . UpdateStatus ( node ) ; err != nil {
2018-11-09 13:49:10 -05:00
klog . Errorf ( "Error updating node %s: %v" , node . Name , err )
2016-05-16 05:20:23 -04:00
return gracePeriod , observedReadyCondition , currentReadyCondition , err
2015-03-31 11:15:39 -04:00
}
2018-10-01 14:32:56 -04:00
nc . nodeHealthMap [ node . Name ] = & nodeHealthData {
status : & node . Status ,
probeTimestamp : nc . nodeHealthMap [ node . Name ] . probeTimestamp ,
2017-08-08 19:25:20 -04:00
readyTransitionTimestamp : nc . now ( ) ,
2018-10-01 14:32:56 -04:00
lease : observedLease ,
2017-08-08 19:25:20 -04:00
}
return gracePeriod , observedReadyCondition , currentReadyCondition , nil
2015-03-30 08:44:02 -04:00
}
}
2016-05-16 05:20:23 -04:00
return gracePeriod , observedReadyCondition , currentReadyCondition , err
}
2017-10-11 19:36:39 -04:00
func ( nc * Controller ) handleDisruption ( zoneToNodeConditions map [ string ] [ ] * v1 . NodeCondition , nodes [ ] * v1 . Node ) {
newZoneStates := map [ string ] ZoneState { }
allAreFullyDisrupted := true
for k , v := range zoneToNodeConditions {
zoneSize . WithLabelValues ( k ) . Set ( float64 ( len ( v ) ) )
unhealthy , newState := nc . computeZoneStateFunc ( v )
zoneHealth . WithLabelValues ( k ) . Set ( float64 ( 100 * ( len ( v ) - unhealthy ) ) / float64 ( len ( v ) ) )
unhealthyNodes . WithLabelValues ( k ) . Set ( float64 ( unhealthy ) )
if newState != stateFullDisruption {
allAreFullyDisrupted = false
}
newZoneStates [ k ] = newState
if _ , had := nc . zoneStates [ k ] ; ! had {
2018-11-09 13:49:10 -05:00
klog . Errorf ( "Setting initial state for unseen zone: %v" , k )
2017-10-11 19:36:39 -04:00
nc . zoneStates [ k ] = stateInitial
}
}
allWasFullyDisrupted := true
for k , v := range nc . zoneStates {
if _ , have := zoneToNodeConditions [ k ] ; ! have {
zoneSize . WithLabelValues ( k ) . Set ( 0 )
zoneHealth . WithLabelValues ( k ) . Set ( 100 )
unhealthyNodes . WithLabelValues ( k ) . Set ( 0 )
delete ( nc . zoneStates , k )
continue
}
if v != stateFullDisruption {
allWasFullyDisrupted = false
break
}
}
// At least one node was responding in previous pass or in the current pass. Semantics is as follows:
// - if the new state is "partialDisruption" we call a user defined function that returns a new limiter to use,
// - if the new state is "normal" we resume normal operation (go back to default limiter settings),
// - if new state is "fullDisruption" we restore normal eviction rate,
// - unless all zones in the cluster are in "fullDisruption" - in that case we stop all evictions.
if ! allAreFullyDisrupted || ! allWasFullyDisrupted {
// We're switching to full disruption mode
if allAreFullyDisrupted {
2018-11-09 13:49:10 -05:00
klog . V ( 0 ) . Info ( "Controller detected that all Nodes are not-Ready. Entering master disruption mode." )
2017-10-11 19:36:39 -04:00
for i := range nodes {
if nc . useTaintBasedEvictions {
_ , err := nc . markNodeAsReachable ( nodes [ i ] )
if err != nil {
2018-11-09 13:49:10 -05:00
klog . Errorf ( "Failed to remove taints from Node %v" , nodes [ i ] . Name )
2017-10-11 19:36:39 -04:00
}
} else {
nc . cancelPodEviction ( nodes [ i ] )
}
}
// We stop all evictions.
for k := range nc . zoneStates {
if nc . useTaintBasedEvictions {
nc . zoneNoExecuteTainter [ k ] . SwapLimiter ( 0 )
} else {
nc . zonePodEvictor [ k ] . SwapLimiter ( 0 )
}
}
for k := range nc . zoneStates {
nc . zoneStates [ k ] = stateFullDisruption
}
// All rate limiters are updated, so we can return early here.
return
}
// We're exiting full disruption mode
if allWasFullyDisrupted {
2018-11-09 13:49:10 -05:00
klog . V ( 0 ) . Info ( "Controller detected that some Nodes are Ready. Exiting master disruption mode." )
2017-10-11 19:36:39 -04:00
// When exiting disruption mode update probe timestamps on all Nodes.
now := nc . now ( )
for i := range nodes {
2018-10-01 14:32:56 -04:00
v := nc . nodeHealthMap [ nodes [ i ] . Name ]
2017-10-11 19:36:39 -04:00
v . probeTimestamp = now
v . readyTransitionTimestamp = now
2018-10-01 14:32:56 -04:00
nc . nodeHealthMap [ nodes [ i ] . Name ] = v
2017-10-11 19:36:39 -04:00
}
// We reset all rate limiters to settings appropriate for the given state.
for k := range nc . zoneStates {
nc . setLimiterInZone ( k , len ( zoneToNodeConditions [ k ] ) , newZoneStates [ k ] )
nc . zoneStates [ k ] = newZoneStates [ k ]
}
return
}
// We know that there's at least one not-fully disrupted so,
// we can use default behavior for rate limiters
for k , v := range nc . zoneStates {
newState := newZoneStates [ k ]
if v == newState {
continue
}
2018-11-09 13:49:10 -05:00
klog . V ( 0 ) . Infof ( "Controller detected that zone %v is now in state %v." , k , newState )
2017-10-11 19:36:39 -04:00
nc . setLimiterInZone ( k , len ( zoneToNodeConditions [ k ] ) , newState )
nc . zoneStates [ k ] = newState
}
}
}
func ( nc * Controller ) setLimiterInZone ( zone string , zoneSize int , state ZoneState ) {
switch state {
case stateNormal :
if nc . useTaintBasedEvictions {
nc . zoneNoExecuteTainter [ zone ] . SwapLimiter ( nc . evictionLimiterQPS )
} else {
nc . zonePodEvictor [ zone ] . SwapLimiter ( nc . evictionLimiterQPS )
}
case statePartialDisruption :
if nc . useTaintBasedEvictions {
nc . zoneNoExecuteTainter [ zone ] . SwapLimiter (
nc . enterPartialDisruptionFunc ( zoneSize ) )
} else {
nc . zonePodEvictor [ zone ] . SwapLimiter (
nc . enterPartialDisruptionFunc ( zoneSize ) )
}
case stateFullDisruption :
if nc . useTaintBasedEvictions {
nc . zoneNoExecuteTainter [ zone ] . SwapLimiter (
nc . enterFullDisruptionFunc ( zoneSize ) )
} else {
nc . zonePodEvictor [ zone ] . SwapLimiter (
nc . enterFullDisruptionFunc ( zoneSize ) )
}
}
}
2017-06-23 03:38:05 -04:00
// classifyNodes classifies the allNodes to three categories:
// 1. added: the nodes that in 'allNodes', but not in 'knownNodeSet'
// 2. deleted: the nodes that in 'knownNodeSet', but not in 'allNodes'
// 3. newZoneRepresentatives: the nodes that in both 'knownNodeSet' and 'allNodes', but no zone states
2017-08-08 19:25:20 -04:00
func ( nc * Controller ) classifyNodes ( allNodes [ ] * v1 . Node ) ( added , deleted , newZoneRepresentatives [ ] * v1 . Node ) {
2017-06-23 03:38:05 -04:00
for i := range allNodes {
if _ , has := nc . knownNodeSet [ allNodes [ i ] . Name ] ; ! has {
added = append ( added , allNodes [ i ] )
} else {
// Currently, we only consider new zone as updated.
zone := utilnode . GetZoneKey ( allNodes [ i ] )
if _ , found := nc . zoneStates [ zone ] ; ! found {
newZoneRepresentatives = append ( newZoneRepresentatives , allNodes [ i ] )
}
2016-07-12 03:38:57 -04:00
}
}
2017-06-23 03:38:05 -04:00
2016-07-12 03:38:57 -04:00
// If there's a difference between lengths of known Nodes and observed nodes
// we must have removed some Node.
2017-06-23 03:38:05 -04:00
if len ( nc . knownNodeSet ) + len ( added ) != len ( allNodes ) {
2016-11-18 15:50:17 -05:00
knowSetCopy := map [ string ] * v1 . Node { }
2016-07-12 03:38:57 -04:00
for k , v := range nc . knownNodeSet {
knowSetCopy [ k ] = v
}
2017-06-23 03:38:05 -04:00
for i := range allNodes {
delete ( knowSetCopy , allNodes [ i ] . Name )
2016-07-12 03:38:57 -04:00
}
for i := range knowSetCopy {
deleted = append ( deleted , knowSetCopy [ i ] )
}
}
return
}
2017-10-11 19:36:39 -04:00
// HealthyQPSFunc returns the default value for cluster eviction rate - we take
// nodeNum for consistency with ReducedQPSFunc.
func ( nc * Controller ) HealthyQPSFunc ( nodeNum int ) float32 {
return nc . evictionLimiterQPS
}
// ReducedQPSFunc returns the QPS for when a the cluster is large make
// evictions slower, if they're small stop evictions altogether.
func ( nc * Controller ) ReducedQPSFunc ( nodeNum int ) float32 {
if int32 ( nodeNum ) > nc . largeClusterThreshold {
return nc . secondaryEvictionLimiterQPS
}
return 0
}
// addPodEvictorForNewZone checks if new zone appeared, and if so add new evictor.
func ( nc * Controller ) addPodEvictorForNewZone ( node * v1 . Node ) {
2018-03-06 03:18:11 -05:00
nc . evictorLock . Lock ( )
defer nc . evictorLock . Unlock ( )
2017-10-11 19:36:39 -04:00
zone := utilnode . GetZoneKey ( node )
if _ , found := nc . zoneStates [ zone ] ; ! found {
nc . zoneStates [ zone ] = stateInitial
if ! nc . useTaintBasedEvictions {
nc . zonePodEvictor [ zone ] =
scheduler . NewRateLimitedTimedQueue (
flowcontrol . NewTokenBucketRateLimiter ( nc . evictionLimiterQPS , scheduler . EvictionRateLimiterBurst ) )
} else {
nc . zoneNoExecuteTainter [ zone ] =
scheduler . NewRateLimitedTimedQueue (
flowcontrol . NewTokenBucketRateLimiter ( nc . evictionLimiterQPS , scheduler . EvictionRateLimiterBurst ) )
}
// Init the metric for the new zone.
2018-11-09 13:49:10 -05:00
klog . Infof ( "Initializing eviction metric for zone: %v" , zone )
2017-10-11 19:36:39 -04:00
evictionsNumber . WithLabelValues ( zone ) . Add ( 0 )
}
}
2015-08-25 09:47:08 -04:00
// cancelPodEviction removes any queued evictions, typically because the node is available again. It
// returns true if an eviction was queued.
2017-08-08 19:25:20 -04:00
func ( nc * Controller ) cancelPodEviction ( node * v1 . Node ) bool {
2016-07-12 08:29:46 -04:00
zone := utilnode . GetZoneKey ( node )
2015-09-07 09:04:15 -04:00
nc . evictorLock . Lock ( )
defer nc . evictorLock . Unlock ( )
2016-07-12 08:29:46 -04:00
wasDeleting := nc . zonePodEvictor [ zone ] . Remove ( node . Name )
2016-10-28 13:45:04 -04:00
if wasDeleting {
2018-11-09 13:49:10 -05:00
klog . V ( 2 ) . Infof ( "Cancelling pod Eviction on Node: %v" , node . Name )
2015-09-15 17:45:56 -04:00
return true
}
return false
2015-08-25 09:47:08 -04:00
}
2016-07-11 07:23:53 -04:00
// evictPods queues an eviction for the provided node name, and returns false if the node is already
// queued for eviction.
2017-08-08 19:25:20 -04:00
func ( nc * Controller ) evictPods ( node * v1 . Node ) bool {
2016-07-11 07:23:53 -04:00
nc . evictorLock . Lock ( )
defer nc . evictorLock . Unlock ( )
2016-08-13 21:41:20 -04:00
return nc . zonePodEvictor [ utilnode . GetZoneKey ( node ) ] . Add ( node . Name , string ( node . UID ) )
2016-05-16 05:20:23 -04:00
}
2016-08-05 08:50:19 -04:00
2017-08-08 19:25:20 -04:00
func ( nc * Controller ) markNodeForTainting ( node * v1 . Node ) bool {
2017-02-06 07:58:48 -05:00
nc . evictorLock . Lock ( )
defer nc . evictorLock . Unlock ( )
2017-10-11 19:36:39 -04:00
return nc . zoneNoExecuteTainter [ utilnode . GetZoneKey ( node ) ] . Add ( node . Name , string ( node . UID ) )
2017-02-06 07:58:48 -05:00
}
2017-08-08 19:25:20 -04:00
func ( nc * Controller ) markNodeAsReachable ( node * v1 . Node ) ( bool , error ) {
2017-02-06 07:58:48 -05:00
nc . evictorLock . Lock ( )
defer nc . evictorLock . Unlock ( )
2017-08-07 07:29:39 -04:00
err := controller . RemoveTaintOffNode ( nc . kubeClient , node . Name , node , UnreachableTaintTemplate )
2017-02-06 07:58:48 -05:00
if err != nil {
2018-11-09 13:49:10 -05:00
klog . Errorf ( "Failed to remove taint from node %v: %v" , node . Name , err )
2017-02-06 07:58:48 -05:00
return false , err
}
2017-08-07 07:29:39 -04:00
err = controller . RemoveTaintOffNode ( nc . kubeClient , node . Name , node , NotReadyTaintTemplate )
2017-02-06 07:58:48 -05:00
if err != nil {
2018-11-09 13:49:10 -05:00
klog . Errorf ( "Failed to remove taint from node %v: %v" , node . Name , err )
2017-02-06 07:58:48 -05:00
return false , err
}
2017-10-11 19:36:39 -04:00
return nc . zoneNoExecuteTainter [ utilnode . GetZoneKey ( node ) ] . Remove ( node . Name ) , nil
2016-08-05 08:50:19 -04:00
}
2017-07-15 08:22:55 -04:00
// ComputeZoneState returns a slice of NodeReadyConditions for all Nodes in a given zone.
2016-08-05 08:50:19 -04:00
// The zone is considered:
// - fullyDisrupted if there're no Ready Nodes,
// - partiallyDisrupted if at least than nc.unhealthyZoneThreshold percent of Nodes are not Ready,
// - normal otherwise
2017-08-08 19:25:20 -04:00
func ( nc * Controller ) ComputeZoneState ( nodeReadyConditions [ ] * v1 . NodeCondition ) ( int , ZoneState ) {
2016-08-05 08:50:19 -04:00
readyNodes := 0
notReadyNodes := 0
for i := range nodeReadyConditions {
2016-11-18 15:50:17 -05:00
if nodeReadyConditions [ i ] != nil && nodeReadyConditions [ i ] . Status == v1 . ConditionTrue {
2016-08-05 08:50:19 -04:00
readyNodes ++
} else {
notReadyNodes ++
}
}
switch {
case readyNodes == 0 && notReadyNodes > 0 :
2016-08-16 11:08:26 -04:00
return notReadyNodes , stateFullDisruption
2016-08-05 08:50:19 -04:00
case notReadyNodes > 2 && float32 ( notReadyNodes ) / float32 ( notReadyNodes + readyNodes ) >= nc . unhealthyZoneThreshold :
2016-08-16 11:08:26 -04:00
return notReadyNodes , statePartialDisruption
2016-08-05 08:50:19 -04:00
default :
2016-08-16 11:08:26 -04:00
return notReadyNodes , stateNormal
2016-08-05 08:50:19 -04:00
}
}
2018-08-31 03:26:19 -04:00
2019-02-22 19:09:07 -05:00
// reconcileNodeLabels reconciles node labels.
func ( nc * Controller ) reconcileNodeLabels ( nodeName string ) error {
node , err := nc . nodeLister . Get ( nodeName )
if err != nil {
// If node not found, just ignore it.
if apierrors . IsNotFound ( err ) {
return nil
}
return err
}
if node . Labels == nil {
// Nothing to reconcile.
return nil
}
labelsToUpdate := map [ string ] string { }
for _ , r := range labelReconcileInfo {
primaryValue , primaryExists := node . Labels [ r . primaryKey ]
secondaryValue , secondaryExists := node . Labels [ r . secondaryKey ]
if ! primaryExists {
// The primary label key does not exist. This should not happen
// within our supported version skew range, when no external
// components/factors modifying the node object. Ignore this case.
continue
}
if secondaryExists && primaryValue != secondaryValue {
// Secondary label exists, but not consistent with the primary
// label. Need to reconcile.
labelsToUpdate [ r . secondaryKey ] = primaryValue
} else if ! secondaryExists && r . ensureSecondaryExists {
// Apply secondary label based on primary label.
labelsToUpdate [ r . secondaryKey ] = primaryValue
}
}
if len ( labelsToUpdate ) == 0 {
return nil
}
if ! nodeutil . AddOrUpdateLabelsOnNode ( nc . kubeClient , labelsToUpdate , node ) {
return fmt . Errorf ( "failed update labels for node %+v" , node )
}
return nil
}