Skip to content

Commit 8f49757

Browse files
committed
pkg/listwatch: Change to accept single instance of rvs
1 parent 7bbd816 commit 8f49757

File tree

2 files changed

+13
-27
lines changed

2 files changed

+13
-27
lines changed

pkg/listwatch/listwatch.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,14 @@ func (mlw multiListerWatcher) List(options metav1.ListOptions) (runtime.Object,
169169
// It returns a watch.Interface that combines the output from the
170170
// watch.Interface of every cache.ListerWatcher into a single result chan.
171171
func (mlw multiListerWatcher) Watch(options metav1.ListOptions) (watch.Interface, error) {
172-
resourceVersions := make([]string, len(mlw))
172+
var resourceVersions string
173173
// Allow resource versions to be "".
174174
if options.ResourceVersion != "" {
175175
rvs := strings.Split(options.ResourceVersion, "/")
176-
if len(rvs) != 1 {
177-
return nil, fmt.Errorf("expected resource version to have 1 parts to match the number of ListerWatchers, got %d", len(rvs))
176+
if len(rvs) > 1 {
177+
return nil, fmt.Errorf("expected resource version to have 1 part, got %d", len(rvs))
178178
}
179-
resourceVersions = rvs
179+
resourceVersions = options.ResourceVersion
180180
}
181181
return newMultiWatch(mlw, resourceVersions, options)
182182
}
@@ -190,9 +190,8 @@ type multiWatch struct {
190190
}
191191

192192
// newMultiWatch returns a new multiWatch or an error if one of the underlying
193-
// Watch funcs errored. The length of []cache.ListerWatcher and []string must
194-
// match.
195-
func newMultiWatch(lws []cache.ListerWatcher, resourceVersions []string, options metav1.ListOptions) (*multiWatch, error) {
193+
// Watch funcs errored.
194+
func newMultiWatch(lws []cache.ListerWatcher, resourceVersions string, options metav1.ListOptions) (*multiWatch, error) {
196195
var (
197196
result = make(chan watch.Event)
198197
stopped = make(chan struct{})
@@ -204,7 +203,7 @@ func newMultiWatch(lws []cache.ListerWatcher, resourceVersions []string, options
204203

205204
for _, lw := range lws {
206205
o := options.DeepCopy()
207-
o.ResourceVersion = resourceVersions[0]
206+
o.ResourceVersion = resourceVersions
208207
w, err := lw.Watch(*o)
209208
if err != nil {
210209
return nil, err

pkg/listwatch/listwatch_test.go

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ import (
2626

2727
var _ watch.Interface = &multiWatch{}
2828

29-
func setupMultiWatch(n int, t *testing.T, rvs ...string) ([]*watch.FakeWatcher, *multiWatch) {
30-
// Default resource versions to the correct length if none were passed.
31-
if len(rvs) == 0 {
32-
rvs = make([]string, n)
33-
}
29+
func setupMultiWatch(n int, t *testing.T, rvs string) ([]*watch.FakeWatcher, *multiWatch) {
3430
ws := make([]*watch.FakeWatcher, n)
3531
lws := make([]cache.ListerWatcher, n)
3632
for i := range ws {
@@ -48,28 +44,19 @@ func setupMultiWatch(n int, t *testing.T, rvs ...string) ([]*watch.FakeWatcher,
4844
}
4945

5046
func TestNewMultiWatch(t *testing.T) {
51-
func() {
52-
defer func() {
53-
if r := recover(); r == nil {
54-
t.Error("expected newMultiWatch to panic when number of resource versions is less than ListerWatchers")
55-
}
56-
}()
57-
// Create a multiWatch from 2 ListerWatchers but only pass 1 resource version.
58-
_, _ = setupMultiWatch(2, t, "1")
59-
}()
6047
func() {
6148
defer func() {
6249
if r := recover(); r != nil {
6350
t.Errorf("newMultiWatch should not panic when number of resource versions matches ListerWatchers; got: %v", r)
6451
}
6552
}()
66-
// Create a multiWatch from 2 ListerWatchers and pass 2 resource versions.
67-
_, _ = setupMultiWatch(2, t, "1", "2")
53+
// Create a multiWatch from 1 ListerWatchers and pass 1 resource versions.
54+
_, _ = setupMultiWatch(1, t, "1")
6855
}()
6956
}
7057

7158
func TestMultiWatchResultChan(t *testing.T) {
72-
ws, m := setupMultiWatch(10, t)
59+
ws, m := setupMultiWatch(10, t, "10")
7360
defer m.Stop()
7461
var events []watch.Event
7562
var wg sync.WaitGroup
@@ -97,7 +84,7 @@ func TestMultiWatchResultChan(t *testing.T) {
9784
}
9885

9986
func TestMultiWatchStop(t *testing.T) {
100-
ws, m := setupMultiWatch(10, t)
87+
ws, m := setupMultiWatch(10, t, "10")
10188
m.Stop()
10289
var stopped int
10390
for _, w := range ws {
@@ -149,7 +136,7 @@ func TestRacyMultiWatch(t *testing.T) {
149136

150137
mw, err := newMultiWatch(
151138
[]cache.ListerWatcher{lw},
152-
[]string{"foo"},
139+
"foo",
153140
metav1.ListOptions{},
154141
)
155142
if err != nil {

0 commit comments

Comments
 (0)