Skip to content

Commit 7dfe893

Browse files
authored
Merge pull request #1139 from alvaroaleman/error-on-multiple-start
🐛 Controller: Return error when started more than once
2 parents bb54523 + db22614 commit 7dfe893

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

pkg/internal/controller/controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package controller
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
2223
"sync"
2324
"time"
@@ -126,6 +127,9 @@ func (c *Controller) Start(stop <-chan struct{}) error {
126127
// use an IIFE to get proper lock handling
127128
// but lock outside to get proper handling of the queue shutdown
128129
c.mu.Lock()
130+
if c.Started {
131+
return errors.New("controller was started more than once. This is likely to be caused by being added to a manager multiple times")
132+
}
129133

130134
c.Queue = c.MakeQueue()
131135
defer c.Queue.ShutDown() // needs to be outside the iife so that we shutdown after the stop channel is closed

pkg/internal/controller/controller_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,17 @@ var _ = Describe("controller", func() {
162162
close(stopped)
163163
Expect(ctrl.Start(stopped)).To(Equal(err))
164164
})
165+
166+
It("should return an error if it gets started more than once", func() {
167+
// Use a stopped channel so Start doesn't block
168+
stopped := make(chan struct{})
169+
close(stopped)
170+
Expect(ctrl.Start(stopped)).To(BeNil())
171+
err := ctrl.Start(stopped)
172+
Expect(err).NotTo(BeNil())
173+
Expect(err.Error()).To(Equal("controller was started more than once. This is likely to be caused by being added to a manager multiple times"))
174+
})
175+
165176
})
166177

167178
Describe("Watch", func() {

0 commit comments

Comments
 (0)