From 13b268b3781dec70d3e7a0bdf4eb7ba65d198c99 Mon Sep 17 00:00:00 2001 From: Yu-Ju Hong Date: Thu, 27 Aug 2015 18:07:57 -0700 Subject: [PATCH] kubelet: define the minimum housekeeping period Before, kubelet performs global cleanup tasks every iteration. After the PR #13003, kubelet performs the tasks on every sync internval (10 seconds). This PR decouples the housekeeping period with the sync internval to ensure that kubelet cleans up promptly, while not too often (no more than once every minimum housekeeping period). --- pkg/kubelet/kubelet.go | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index 093c3279abe..9d18e0b3bb5 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -90,6 +90,10 @@ const ( // system default DNS resolver configuration ResolvConfDefault = "/etc/resolv.conf" + + // Minimum period for performing global cleanup tasks, i.e., housekeeping + // will not be performed more than once per housekeepingMinimumPeriod. + housekeepingMinimumPeriod = time.Second * 2 ) var ( @@ -1779,23 +1783,37 @@ func (kl *Kubelet) canAdmitPod(pods []*api.Pod, pod *api.Pod) (bool, string, str // state every sync-frequency seconds. Never returns. func (kl *Kubelet) syncLoop(updates <-chan PodUpdate, handler SyncHandler) { glog.Info("Starting kubelet main sync loop.") + var housekeepingTimestamp time.Time for { + if !kl.containerRuntimeUp() { + time.Sleep(5 * time.Second) + glog.Infof("Skipping pod synchronization, container runtime is not up.") + continue + } + if !kl.doneNetworkConfigure() { + time.Sleep(5 * time.Second) + glog.Infof("Skipping pod synchronization, network is not configured") + continue + } + // We don't want to perform housekeeping too often, so we set a minimum + // period for it. Housekeeping would be performed at least once every + // kl.resyncInterval, and *no* more than once every + // housekeepingMinimumPeriod. + // TODO (#13418): Investigate whether we can/should spawn a dedicated + // goroutine for housekeeping + if housekeepingTimestamp.IsZero() || time.Since(housekeepingTimestamp) > housekeepingMinimumPeriod { + glog.V(4).Infof("SyncLoop (housekeeping)") + if err := handler.HandlePodCleanups(); err != nil { + glog.Errorf("Failed cleaning pods: %v", err) + } + housekeepingTimestamp = time.Now() + } kl.syncLoopIteration(updates, handler) } } func (kl *Kubelet) syncLoopIteration(updates <-chan PodUpdate, handler SyncHandler) { kl.syncLoopMonitor.Store(time.Now()) - if !kl.containerRuntimeUp() { - time.Sleep(5 * time.Second) - glog.Infof("Skipping pod synchronization, container runtime is not up.") - return - } - if !kl.doneNetworkConfigure() { - time.Sleep(5 * time.Second) - glog.Infof("Skipping pod synchronization, network is not configured") - return - } select { case u, ok := <-updates: if !ok { @@ -1820,11 +1838,7 @@ func (kl *Kubelet) syncLoopIteration(updates <-chan PodUpdate, handler SyncHandl // Periodically syncs all the pods and performs cleanup tasks. glog.V(4).Infof("SyncLoop (periodic sync)") handler.HandlePodSyncs(kl.podManager.GetPods()) - if err := handler.HandlePodCleanups(); err != nil { - glog.Errorf("Failed cleaning pods: %v", err) - } } - kl.syncLoopMonitor.Store(time.Now()) }