From c33f321acdea848cb79e2489fc7a71f8d759d99b Mon Sep 17 00:00:00 2001 From: Michael Taufen Date: Mon, 25 Jun 2018 16:55:38 -0700 Subject: [PATCH] port setNodeOODCondition to Setter abstraction --- pkg/kubelet/kubelet_node_status.go | 35 +------------------------ pkg/kubelet/nodestatus/BUILD | 1 + pkg/kubelet/nodestatus/setters.go | 41 ++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 34 deletions(-) diff --git a/pkg/kubelet/kubelet_node_status.go b/pkg/kubelet/kubelet_node_status.go index 23400c25e1f..269a58d282d 100644 --- a/pkg/kubelet/kubelet_node_status.go +++ b/pkg/kubelet/kubelet_node_status.go @@ -892,39 +892,6 @@ func (kl *Kubelet) setNodeDiskPressureCondition(node *v1.Node) { } } -// Set OODCondition for the node. -func (kl *Kubelet) setNodeOODCondition(node *v1.Node) { - currentTime := metav1.NewTime(kl.clock.Now()) - var nodeOODCondition *v1.NodeCondition - - // Check if NodeOutOfDisk condition already exists and if it does, just pick it up for update. - for i := range node.Status.Conditions { - if node.Status.Conditions[i].Type == v1.NodeOutOfDisk { - nodeOODCondition = &node.Status.Conditions[i] - } - } - - newOODCondition := nodeOODCondition == nil - if newOODCondition { - nodeOODCondition = &v1.NodeCondition{} - } - if nodeOODCondition.Status != v1.ConditionFalse { - nodeOODCondition.Type = v1.NodeOutOfDisk - nodeOODCondition.Status = v1.ConditionFalse - nodeOODCondition.Reason = "KubeletHasSufficientDisk" - nodeOODCondition.Message = "kubelet has sufficient disk space available" - nodeOODCondition.LastTransitionTime = currentTime - kl.recordNodeStatusEvent(v1.EventTypeNormal, "NodeHasSufficientDisk") - } - - // Update the heartbeat time irrespective of all the conditions. - nodeOODCondition.LastHeartbeatTime = currentTime - - if newOODCondition { - node.Status.Conditions = append(node.Status.Conditions, *nodeOODCondition) - } -} - // record if node schedulable change. func (kl *Kubelet) recordNodeSchedulableEvent(node *v1.Node) { kl.lastNodeUnschedulableLock.Lock() @@ -990,7 +957,7 @@ func (kl *Kubelet) defaultNodeStatusFuncs() []func(*v1.Node) error { return []func(*v1.Node) error{ nodestatus.NodeAddress(kl.nodeIP, kl.nodeIPValidator, kl.hostname, kl.externalCloudProvider, kl.cloud, nodeAddressesFunc), withoutError(kl.setNodeStatusInfo), - withoutError(kl.setNodeOODCondition), + nodestatus.OutOfDiskCondition(kl.clock.Now, kl.recordNodeStatusEvent), withoutError(kl.setNodeMemoryPressureCondition), withoutError(kl.setNodeDiskPressureCondition), withoutError(kl.setNodePIDPressureCondition), diff --git a/pkg/kubelet/nodestatus/BUILD b/pkg/kubelet/nodestatus/BUILD index b7ac0ca3407..1322361f631 100644 --- a/pkg/kubelet/nodestatus/BUILD +++ b/pkg/kubelet/nodestatus/BUILD @@ -9,6 +9,7 @@ go_library( "//pkg/cloudprovider:go_default_library", "//pkg/kubelet/apis:go_default_library", "//staging/src/k8s.io/api/core/v1:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/util/net:go_default_library", "//vendor/github.com/golang/glog:go_default_library", ], diff --git a/pkg/kubelet/nodestatus/setters.go b/pkg/kubelet/nodestatus/setters.go index 3d84c5e18aa..0639f2be717 100644 --- a/pkg/kubelet/nodestatus/setters.go +++ b/pkg/kubelet/nodestatus/setters.go @@ -19,8 +19,10 @@ package nodestatus import ( "fmt" "net" + "time" "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" utilnet "k8s.io/apimachinery/pkg/util/net" "k8s.io/kubernetes/pkg/cloudprovider" kubeletapis "k8s.io/kubernetes/pkg/kubelet/apis" @@ -139,3 +141,42 @@ func NodeAddress(nodeIP net.IP, // typically Kubelet.nodeIP return nil } } + +// OutOfDiskCondition returns a Setter that updates the v1.NodeOutOfDisk condition on the node. +// TODO(#65658): remove this condition +func OutOfDiskCondition(nowFunc func() time.Time, // typically Kubelet.clock.Now + recordEventFunc func(eventType, event string), // typically Kubelet.recordNodeStatusEvent +) Setter { + return func(node *v1.Node) error { + currentTime := metav1.NewTime(nowFunc()) + var nodeOODCondition *v1.NodeCondition + + // Check if NodeOutOfDisk condition already exists and if it does, just pick it up for update. + for i := range node.Status.Conditions { + if node.Status.Conditions[i].Type == v1.NodeOutOfDisk { + nodeOODCondition = &node.Status.Conditions[i] + } + } + + newOODCondition := nodeOODCondition == nil + if newOODCondition { + nodeOODCondition = &v1.NodeCondition{} + } + if nodeOODCondition.Status != v1.ConditionFalse { + nodeOODCondition.Type = v1.NodeOutOfDisk + nodeOODCondition.Status = v1.ConditionFalse + nodeOODCondition.Reason = "KubeletHasSufficientDisk" + nodeOODCondition.Message = "kubelet has sufficient disk space available" + nodeOODCondition.LastTransitionTime = currentTime + recordEventFunc(v1.EventTypeNormal, "NodeHasSufficientDisk") + } + + // Update the heartbeat time irrespective of all the conditions. + nodeOODCondition.LastHeartbeatTime = currentTime + + if newOODCondition { + node.Status.Conditions = append(node.Status.Conditions, *nodeOODCondition) + } + return nil + } +}