Merge pull request #61894 from atlassian/misc-cleanups

Automatic merge from submit-queue (batch tested with PRs 61894, 61369). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Use range in loops; misc fixes

**What this PR does / why we need it**:
It is cleaner to use `range` in for loops to iterate over channel until it is closed.

**Release note**:
```release-note
NONE
```
/kind cleanup
This commit is contained in:
Kubernetes Submit Queue 2018-03-30 21:16:01 -07:00 committed by GitHub
commit 7ce753aa73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 30 additions and 57 deletions

View File

@ -32,11 +32,7 @@ func HandleResizing(resize <-chan remotecommand.TerminalSize, resizeFunc func(si
go func() { go func() {
defer runtime.HandleCrash() defer runtime.HandleCrash()
for { for size := range resize {
size, ok := <-resize
if !ok {
return
}
if size.Height < 1 || size.Width < 1 { if size.Height < 1 || size.Width < 1 {
continue continue
} }

View File

@ -1097,35 +1097,28 @@ func (kl *Kubelet) podKiller() {
killing := sets.NewString() killing := sets.NewString()
// guard for the killing set // guard for the killing set
lock := sync.Mutex{} lock := sync.Mutex{}
for { for podPair := range kl.podKillingCh {
select { runningPod := podPair.RunningPod
case podPair, ok := <-kl.podKillingCh: apiPod := podPair.APIPod
if !ok {
return
}
runningPod := podPair.RunningPod lock.Lock()
apiPod := podPair.APIPod exists := killing.Has(string(runningPod.ID))
if !exists {
killing.Insert(string(runningPod.ID))
}
lock.Unlock()
lock.Lock() if !exists {
exists := killing.Has(string(runningPod.ID)) go func(apiPod *v1.Pod, runningPod *kubecontainer.Pod) {
if !exists { glog.V(2).Infof("Killing unwanted pod %q", runningPod.Name)
killing.Insert(string(runningPod.ID)) err := kl.killPod(apiPod, runningPod, nil, nil)
} if err != nil {
lock.Unlock() glog.Errorf("Failed killing the pod %q: %v", runningPod.Name, err)
}
if !exists { lock.Lock()
go func(apiPod *v1.Pod, runningPod *kubecontainer.Pod) { killing.Delete(string(runningPod.ID))
glog.V(2).Infof("Killing unwanted pod %q", runningPod.Name) lock.Unlock()
err := kl.killPod(apiPod, runningPod, nil, nil) }(apiPod, runningPod)
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)
}
} }
} }
} }

View File

@ -45,10 +45,8 @@ func newPodContainerDeletor(runtime kubecontainer.Runtime, containersToKeep int)
buffer := make(chan kubecontainer.ContainerID, containerDeletorBufferLimit) buffer := make(chan kubecontainer.ContainerID, containerDeletorBufferLimit)
go wait.Until(func() { go wait.Until(func() {
for { for {
select { id := <-buffer
case id := <-buffer: runtime.DeleteContainer(id)
runtime.DeleteContainer(id)
}
} }
}, 0, wait.NeverStop) }, 0, wait.NeverStop)

View File

@ -226,7 +226,7 @@ func (c *realRecyclerClient) WatchPod(name, namespace string, stopChannel chan s
var eventWatchChannelClosed bool var eventWatchChannelClosed bool
for { for {
select { select {
case _ = <-stopChannel: case <-stopChannel:
return return
case podEvent, ok := <-podWatch.ResultChan(): case podEvent, ok := <-podWatch.ResultChan():

View File

@ -62,11 +62,7 @@ func (fw *filteredWatch) Stop() {
// loop waits for new values, filters them, and resends them. // loop waits for new values, filters them, and resends them.
func (fw *filteredWatch) loop() { func (fw *filteredWatch) loop() {
defer close(fw.result) defer close(fw.result)
for { for event := range fw.incoming.ResultChan() {
event, ok := <-fw.incoming.ResultChan()
if !ok {
break
}
filtered, keep := fw.f(event) filtered, keep := fw.f(event)
if keep { if keep {
fw.result <- filtered fw.result <- filtered

View File

@ -204,11 +204,7 @@ func (m *Broadcaster) Shutdown() {
func (m *Broadcaster) loop() { func (m *Broadcaster) loop() {
// Deliberately not catching crashes here. Yes, bring down the process if there's a // Deliberately not catching crashes here. Yes, bring down the process if there's a
// bug in watch.Broadcaster. // bug in watch.Broadcaster.
for { for event := range m.incoming {
event, ok := <-m.incoming
if !ok {
break
}
if event.Type == internalRunFunctionMarker { if event.Type == internalRunFunctionMarker {
event.Object.(functionFakeRuntimeObject)() event.Object.(functionFakeRuntimeObject)()
continue continue

View File

@ -445,6 +445,7 @@ func (e *Store) WaitForInitialized(ctx genericapirequest.Context, obj runtime.Ob
} }
} }
case <-ctx.Done(): case <-ctx.Done():
return nil, ctx.Err()
} }
} }
} }
@ -1070,11 +1071,7 @@ func (e *Store) DeleteCollection(ctx genericapirequest.Context, options *metav1.
}) })
defer wg.Done() defer wg.Done()
for { for index := range toProcess {
index, ok := <-toProcess
if !ok {
return
}
accessor, err := meta.Accessor(items[index]) accessor, err := meta.Accessor(items[index])
if err != nil { if err != nil {
errs <- err errs <- err

View File

@ -225,11 +225,7 @@ func (eventBroadcaster *eventBroadcasterImpl) StartEventWatcher(eventHandler fun
watcher := eventBroadcaster.Watch() watcher := eventBroadcaster.Watch()
go func() { go func() {
defer utilruntime.HandleCrash() defer utilruntime.HandleCrash()
for { for watchEvent := range watcher.ResultChan() {
watchEvent, open := <-watcher.ResultChan()
if !open {
return
}
event, ok := watchEvent.Object.(*v1.Event) event, ok := watchEvent.Object.(*v1.Event)
if !ok { if !ok {
// This is all local, so there's no reason this should // This is all local, so there's no reason this should

View File

@ -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) { func waitForEvent(w watch.Interface, expectType watch.EventType, expectObject runtime.Object) (watch.Event, bool) {
stopTimer := time.NewTimer(1 * time.Minute) stopTimer := time.NewTimer(1 * time.Minute)
defer stopTimer.Stop()
for { for {
select { select {
case actual := <-w.ResultChan(): case actual := <-w.ResultChan():