From c23a8a85cce80a1015797e9c76aae709d9910791 Mon Sep 17 00:00:00 2001 From: Mikhail Mazurskiy Date: Thu, 29 Mar 2018 22:55:25 +1100 Subject: [PATCH] Use range in loops; misc fixes --- pkg/kubelet/container/resize.go | 6 +-- pkg/kubelet/kubelet_pods.go | 47 ++++++++----------- pkg/kubelet/pod_container_deletor.go | 6 +-- .../util/recyclerclient/recycler_client.go | 2 +- .../k8s.io/apimachinery/pkg/watch/filter.go | 6 +-- .../src/k8s.io/apimachinery/pkg/watch/mux.go | 6 +-- .../pkg/registry/generic/registry/store.go | 7 +-- .../k8s.io/client-go/tools/record/event.go | 6 +-- test/e2e/apimachinery/watch.go | 1 + 9 files changed, 30 insertions(+), 57 deletions(-) diff --git a/pkg/kubelet/container/resize.go b/pkg/kubelet/container/resize.go index d7b75eedec0..34e40a95b46 100644 --- a/pkg/kubelet/container/resize.go +++ b/pkg/kubelet/container/resize.go @@ -32,11 +32,7 @@ func HandleResizing(resize <-chan remotecommand.TerminalSize, resizeFunc func(si go func() { defer runtime.HandleCrash() - for { - size, ok := <-resize - if !ok { - return - } + for size := range resize { if size.Height < 1 || size.Width < 1 { continue } diff --git a/pkg/kubelet/kubelet_pods.go b/pkg/kubelet/kubelet_pods.go index 1b96da8169f..713d062109b 100644 --- a/pkg/kubelet/kubelet_pods.go +++ b/pkg/kubelet/kubelet_pods.go @@ -1099,35 +1099,28 @@ func (kl *Kubelet) podKiller() { killing := sets.NewString() // guard for the killing set lock := sync.Mutex{} - for { - select { - case podPair, ok := <-kl.podKillingCh: - if !ok { - return - } + for podPair := range kl.podKillingCh { + runningPod := podPair.RunningPod + apiPod := podPair.APIPod - runningPod := podPair.RunningPod - apiPod := podPair.APIPod + lock.Lock() + exists := killing.Has(string(runningPod.ID)) + if !exists { + killing.Insert(string(runningPod.ID)) + } + lock.Unlock() - lock.Lock() - exists := killing.Has(string(runningPod.ID)) - if !exists { - killing.Insert(string(runningPod.ID)) - } - lock.Unlock() - - if !exists { - go func(apiPod *v1.Pod, runningPod *kubecontainer.Pod) { - glog.V(2).Infof("Killing unwanted pod %q", runningPod.Name) - err := kl.killPod(apiPod, runningPod, nil, nil) - if err != nil { - glog.Errorf("Failed killing the pod %q: %v", runningPod.Name, err) - } - lock.Lock() - killing.Delete(string(runningPod.ID)) - lock.Unlock() - }(apiPod, runningPod) - } + if !exists { + go func(apiPod *v1.Pod, runningPod *kubecontainer.Pod) { + glog.V(2).Infof("Killing unwanted pod %q", runningPod.Name) + err := kl.killPod(apiPod, runningPod, nil, nil) + if err != nil { + glog.Errorf("Failed killing the pod %q: %v", runningPod.Name, err) + } + lock.Lock() + killing.Delete(string(runningPod.ID)) + lock.Unlock() + }(apiPod, runningPod) } } } diff --git a/pkg/kubelet/pod_container_deletor.go b/pkg/kubelet/pod_container_deletor.go index caf00b05c13..bd47cf5de35 100644 --- a/pkg/kubelet/pod_container_deletor.go +++ b/pkg/kubelet/pod_container_deletor.go @@ -45,10 +45,8 @@ func newPodContainerDeletor(runtime kubecontainer.Runtime, containersToKeep int) buffer := make(chan kubecontainer.ContainerID, containerDeletorBufferLimit) go wait.Until(func() { for { - select { - case id := <-buffer: - runtime.DeleteContainer(id) - } + id := <-buffer + runtime.DeleteContainer(id) } }, 0, wait.NeverStop) diff --git a/pkg/volume/util/recyclerclient/recycler_client.go b/pkg/volume/util/recyclerclient/recycler_client.go index 1af6465c6e7..b3b75acf849 100644 --- a/pkg/volume/util/recyclerclient/recycler_client.go +++ b/pkg/volume/util/recyclerclient/recycler_client.go @@ -226,7 +226,7 @@ func (c *realRecyclerClient) WatchPod(name, namespace string, stopChannel chan s var eventWatchChannelClosed bool for { select { - case _ = <-stopChannel: + case <-stopChannel: return case podEvent, ok := <-podWatch.ResultChan(): diff --git a/staging/src/k8s.io/apimachinery/pkg/watch/filter.go b/staging/src/k8s.io/apimachinery/pkg/watch/filter.go index 3ca27f22c5f..22c9449f59c 100644 --- a/staging/src/k8s.io/apimachinery/pkg/watch/filter.go +++ b/staging/src/k8s.io/apimachinery/pkg/watch/filter.go @@ -62,11 +62,7 @@ func (fw *filteredWatch) Stop() { // loop waits for new values, filters them, and resends them. func (fw *filteredWatch) loop() { defer close(fw.result) - for { - event, ok := <-fw.incoming.ResultChan() - if !ok { - break - } + for event := range fw.incoming.ResultChan() { filtered, keep := fw.f(event) if keep { fw.result <- filtered diff --git a/staging/src/k8s.io/apimachinery/pkg/watch/mux.go b/staging/src/k8s.io/apimachinery/pkg/watch/mux.go index a65088c1cf8..0ac8dc4ef9e 100644 --- a/staging/src/k8s.io/apimachinery/pkg/watch/mux.go +++ b/staging/src/k8s.io/apimachinery/pkg/watch/mux.go @@ -204,11 +204,7 @@ func (m *Broadcaster) Shutdown() { func (m *Broadcaster) loop() { // Deliberately not catching crashes here. Yes, bring down the process if there's a // bug in watch.Broadcaster. - for { - event, ok := <-m.incoming - if !ok { - break - } + for event := range m.incoming { if event.Type == internalRunFunctionMarker { event.Object.(functionFakeRuntimeObject)() continue diff --git a/staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store.go b/staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store.go index d7e449c2551..9901f320f0e 100644 --- a/staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store.go +++ b/staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store.go @@ -445,6 +445,7 @@ func (e *Store) WaitForInitialized(ctx genericapirequest.Context, obj runtime.Ob } } case <-ctx.Done(): + return nil, ctx.Err() } } } @@ -1070,11 +1071,7 @@ func (e *Store) DeleteCollection(ctx genericapirequest.Context, options *metav1. }) defer wg.Done() - for { - index, ok := <-toProcess - if !ok { - return - } + for index := range toProcess { accessor, err := meta.Accessor(items[index]) if err != nil { errs <- err diff --git a/staging/src/k8s.io/client-go/tools/record/event.go b/staging/src/k8s.io/client-go/tools/record/event.go index b5ec44650f5..cc665d74e63 100644 --- a/staging/src/k8s.io/client-go/tools/record/event.go +++ b/staging/src/k8s.io/client-go/tools/record/event.go @@ -225,11 +225,7 @@ func (eventBroadcaster *eventBroadcasterImpl) StartEventWatcher(eventHandler fun watcher := eventBroadcaster.Watch() go func() { defer utilruntime.HandleCrash() - for { - watchEvent, open := <-watcher.ResultChan() - if !open { - return - } + for watchEvent := range watcher.ResultChan() { event, ok := watchEvent.Object.(*v1.Event) if !ok { // This is all local, so there's no reason this should diff --git a/test/e2e/apimachinery/watch.go b/test/e2e/apimachinery/watch.go index b8e178d6dae..339abbe7238 100644 --- a/test/e2e/apimachinery/watch.go +++ b/test/e2e/apimachinery/watch.go @@ -192,6 +192,7 @@ func expectNoEvent(w watch.Interface, eventType watch.EventType, object runtime. func waitForEvent(w watch.Interface, expectType watch.EventType, expectObject runtime.Object) (watch.Event, bool) { stopTimer := time.NewTimer(1 * time.Minute) + defer stopTimer.Stop() for { select { case actual := <-w.ResultChan():