1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
|
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cache
import (
"errors"
"fmt"
"sort"
"sync"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
testingclock "k8s.io/utils/clock/testing"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"
)
func TestInitialEventsEndBookmarkTicker(t *testing.T) {
assertNoEvents := func(t *testing.T, c <-chan time.Time) {
select {
case e := <-c:
t.Errorf("Unexpected: %#v event received, expected no events", e)
default:
return
}
}
t.Run("testing NoopInitialEventsEndBookmarkTicker", func(t *testing.T) {
clock := testingclock.NewFakeClock(time.Now())
target := newInitialEventsEndBookmarkTickerInternal("testName", clock, clock.Now(), time.Second, false)
clock.Step(30 * time.Second)
assertNoEvents(t, target.C())
actualWarning := target.produceWarningIfExpired()
require.Empty(t, actualWarning, "didn't expect any warning")
// validate if the other methods don't produce panic
target.warnIfExpired()
target.observeLastEventTimeStamp(clock.Now())
// make sure that after calling the other methods
// nothing hasn't changed
actualWarning = target.produceWarningIfExpired()
require.Empty(t, actualWarning, "didn't expect any warning")
assertNoEvents(t, target.C())
target.Stop()
})
t.Run("testing InitialEventsEndBookmarkTicker backed by a fake clock", func(t *testing.T) {
clock := testingclock.NewFakeClock(time.Now())
target := newInitialEventsEndBookmarkTickerInternal("testName", clock, clock.Now(), time.Second, true)
clock.Step(500 * time.Millisecond)
assertNoEvents(t, target.C())
clock.Step(500 * time.Millisecond)
<-target.C()
actualWarning := target.produceWarningIfExpired()
require.Equal(t, errors.New("testName: awaiting required bookmark event for initial events stream, no events received for 1s"), actualWarning)
clock.Step(time.Second)
<-target.C()
actualWarning = target.produceWarningIfExpired()
require.Equal(t, errors.New("testName: awaiting required bookmark event for initial events stream, no events received for 2s"), actualWarning)
target.observeLastEventTimeStamp(clock.Now())
clock.Step(500 * time.Millisecond)
assertNoEvents(t, target.C())
clock.Step(500 * time.Millisecond)
<-target.C()
actualWarning = target.produceWarningIfExpired()
require.Equal(t, errors.New("testName: hasn't received required bookmark event marking the end of initial events stream, received last event 1s ago"), actualWarning)
clock.Step(time.Second)
<-target.C()
actualWarning = target.produceWarningIfExpired()
require.Equal(t, errors.New("testName: hasn't received required bookmark event marking the end of initial events stream, received last event 2s ago"), actualWarning)
target.Stop()
assertNoEvents(t, target.C())
})
}
func TestWatchList(t *testing.T) {
scenarios := []struct {
name string
disableUseWatchList bool
// closes listWatcher after sending the specified number of watch events
closeAfterWatchEvents int
// closes listWatcher after getting the specified number of watch requests
closeAfterWatchRequests int
// closes listWatcher after getting the specified number of list requests
closeAfterListRequests int
// stops Watcher after sending the specified number of watch events
stopAfterWatchEvents int
watchOptionsPredicate func(options metav1.ListOptions) error
watchEvents []watch.Event
podList *v1.PodList
expectedRequestOptions []metav1.ListOptions
expectedWatchRequests int
expectedListRequests int
expectedStoreContent []v1.Pod
expectedError error
}{
{
name: "the reflector won't be synced if the bookmark event has been received",
watchEvents: []watch.Event{{Type: watch.Added, Object: makePod("p1", "1")}},
closeAfterWatchEvents: 1,
expectedWatchRequests: 1,
expectedRequestOptions: []metav1.ListOptions{{
SendInitialEvents: pointer.Bool(true),
AllowWatchBookmarks: true,
ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan,
TimeoutSeconds: pointer.Int64(1),
}},
},
{
name: "the reflector uses the old LIST/WATCH semantics if the UseWatchList is turned off",
disableUseWatchList: true,
closeAfterWatchRequests: 1,
podList: &v1.PodList{
ListMeta: metav1.ListMeta{ResourceVersion: "1"},
Items: []v1.Pod{*makePod("p1", "1")},
},
expectedWatchRequests: 1,
expectedListRequests: 1,
expectedRequestOptions: []metav1.ListOptions{
{
ResourceVersion: "0",
Limit: 500,
},
{
AllowWatchBookmarks: true,
ResourceVersion: "1",
TimeoutSeconds: pointer.Int64(1),
}},
expectedStoreContent: []v1.Pod{*makePod("p1", "1")},
},
{
name: "returning any other error than apierrors.NewInvalid forces fallback",
watchOptionsPredicate: func(options metav1.ListOptions) error {
if options.SendInitialEvents != nil && *options.SendInitialEvents {
return fmt.Errorf("dummy error")
}
return nil
},
podList: &v1.PodList{
ListMeta: metav1.ListMeta{ResourceVersion: "1"},
Items: []v1.Pod{*makePod("p1", "1")},
},
closeAfterWatchEvents: 1,
watchEvents: []watch.Event{{Type: watch.Added, Object: makePod("p2", "2")}},
expectedWatchRequests: 2,
expectedListRequests: 1,
expectedStoreContent: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2")},
expectedRequestOptions: []metav1.ListOptions{
{
SendInitialEvents: pointer.Bool(true),
AllowWatchBookmarks: true,
ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan,
TimeoutSeconds: pointer.Int64(1),
},
{
ResourceVersion: "0",
Limit: 500,
},
{
AllowWatchBookmarks: true,
ResourceVersion: "1",
TimeoutSeconds: pointer.Int64(1),
},
},
},
{
name: "the reflector can fall back to old LIST/WATCH semantics when a server doesn't support streaming",
watchOptionsPredicate: func(options metav1.ListOptions) error {
if options.SendInitialEvents != nil && *options.SendInitialEvents {
return apierrors.NewInvalid(schema.GroupKind{}, "streaming is not allowed", nil)
}
return nil
},
podList: &v1.PodList{
ListMeta: metav1.ListMeta{ResourceVersion: "1"},
Items: []v1.Pod{*makePod("p1", "1")},
},
closeAfterWatchEvents: 1,
watchEvents: []watch.Event{{Type: watch.Added, Object: makePod("p2", "2")}},
expectedWatchRequests: 2,
expectedListRequests: 1,
expectedStoreContent: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2")},
expectedRequestOptions: []metav1.ListOptions{
{
SendInitialEvents: pointer.Bool(true),
AllowWatchBookmarks: true,
ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan,
TimeoutSeconds: pointer.Int64(1),
},
{
ResourceVersion: "0",
Limit: 500,
},
{
AllowWatchBookmarks: true,
ResourceVersion: "1",
TimeoutSeconds: pointer.Int64(1),
},
},
},
{
name: "prove that the reflector is synced after receiving a bookmark event",
closeAfterWatchEvents: 3,
watchEvents: []watch.Event{
{Type: watch.Added, Object: makePod("p1", "1")},
{Type: watch.Added, Object: makePod("p2", "2")},
{Type: watch.Bookmark, Object: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "2",
Annotations: map[string]string{metav1.InitialEventsAnnotationKey: "true"},
},
}},
},
expectedWatchRequests: 1,
expectedRequestOptions: []metav1.ListOptions{{
SendInitialEvents: pointer.Bool(true),
AllowWatchBookmarks: true,
ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan,
TimeoutSeconds: pointer.Int64(1),
}},
expectedStoreContent: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2")},
},
{
name: "check if Updates and Deletes events are propagated during streaming (until the bookmark is received)",
closeAfterWatchEvents: 6,
watchEvents: []watch.Event{
{Type: watch.Added, Object: makePod("p1", "1")},
{Type: watch.Added, Object: makePod("p2", "2")},
{Type: watch.Modified, Object: func() runtime.Object {
p1 := makePod("p1", "3")
p1.Spec.ActiveDeadlineSeconds = pointer.Int64(12)
return p1
}()},
{Type: watch.Added, Object: makePod("p3", "4")},
{Type: watch.Deleted, Object: makePod("p3", "5")},
{Type: watch.Bookmark, Object: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "5",
Annotations: map[string]string{metav1.InitialEventsAnnotationKey: "true"},
},
}},
},
expectedWatchRequests: 1,
expectedRequestOptions: []metav1.ListOptions{{
SendInitialEvents: pointer.Bool(true),
AllowWatchBookmarks: true,
ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan,
TimeoutSeconds: pointer.Int64(1),
}},
expectedStoreContent: []v1.Pod{
*makePod("p2", "2"),
func() v1.Pod {
p1 := *makePod("p1", "3")
p1.Spec.ActiveDeadlineSeconds = pointer.Int64(12)
return p1
}(),
},
},
{
name: "checks if the reflector retries 429",
watchOptionsPredicate: func() func(options metav1.ListOptions) error {
counter := 1
return func(options metav1.ListOptions) error {
if counter < 3 {
counter++
return apierrors.NewTooManyRequests("busy, check again later", 1)
}
return nil
}
}(),
closeAfterWatchEvents: 2,
watchEvents: []watch.Event{
{Type: watch.Added, Object: makePod("p1", "1")},
{Type: watch.Bookmark, Object: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "2",
Annotations: map[string]string{metav1.InitialEventsAnnotationKey: "true"},
},
}},
},
expectedWatchRequests: 3,
expectedRequestOptions: []metav1.ListOptions{
{
SendInitialEvents: pointer.Bool(true),
AllowWatchBookmarks: true,
ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan,
TimeoutSeconds: pointer.Int64(1),
},
{
SendInitialEvents: pointer.Bool(true),
AllowWatchBookmarks: true,
ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan,
TimeoutSeconds: pointer.Int64(1),
},
{
SendInitialEvents: pointer.Bool(true),
AllowWatchBookmarks: true,
ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan,
TimeoutSeconds: pointer.Int64(1),
},
},
expectedStoreContent: []v1.Pod{*makePod("p1", "1")},
},
{
name: "check if stopping a watcher before sync results in creating a new watch-list request",
stopAfterWatchEvents: 1,
closeAfterWatchEvents: 3,
watchEvents: []watch.Event{
{Type: watch.Added, Object: makePod("p1", "1")},
// second request
{Type: watch.Added, Object: makePod("p1", "1")},
{Type: watch.Bookmark, Object: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "1",
Annotations: map[string]string{metav1.InitialEventsAnnotationKey: "true"},
},
}},
},
expectedWatchRequests: 2,
expectedRequestOptions: []metav1.ListOptions{
{
SendInitialEvents: pointer.Bool(true),
AllowWatchBookmarks: true,
ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan,
TimeoutSeconds: pointer.Int64(1),
},
{
SendInitialEvents: pointer.Bool(true),
AllowWatchBookmarks: true,
ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan,
TimeoutSeconds: pointer.Int64(1),
},
},
expectedStoreContent: []v1.Pod{*makePod("p1", "1")},
},
{
name: "stopping a watcher after synchronization results in creating a new watch request",
stopAfterWatchEvents: 4,
closeAfterWatchEvents: 5,
watchEvents: []watch.Event{
{Type: watch.Added, Object: makePod("p1", "1")},
{Type: watch.Added, Object: makePod("p2", "2")},
{Type: watch.Bookmark, Object: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "2",
Annotations: map[string]string{metav1.InitialEventsAnnotationKey: "true"},
},
}},
{Type: watch.Added, Object: makePod("p3", "3")},
// second request
{Type: watch.Added, Object: makePod("p4", "4")},
},
expectedWatchRequests: 2,
expectedRequestOptions: []metav1.ListOptions{
{
SendInitialEvents: pointer.Bool(true),
AllowWatchBookmarks: true,
ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan,
TimeoutSeconds: pointer.Int64(1),
},
{
AllowWatchBookmarks: true,
ResourceVersion: "3",
TimeoutSeconds: pointer.Int64(1),
},
},
expectedStoreContent: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2"), *makePod("p3", "3"), *makePod("p4", "4")},
},
{
name: "expiring an established watcher results in returning an error from the reflector",
watchOptionsPredicate: func() func(options metav1.ListOptions) error {
counter := 0
return func(options metav1.ListOptions) error {
counter++
if counter == 2 {
return apierrors.NewResourceExpired("rv already expired")
}
return nil
}
}(),
stopAfterWatchEvents: 3,
watchEvents: []watch.Event{
{Type: watch.Added, Object: makePod("p1", "1")},
{Type: watch.Bookmark, Object: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "2",
Annotations: map[string]string{metav1.InitialEventsAnnotationKey: "true"},
},
}},
{Type: watch.Added, Object: makePod("p3", "3")},
},
expectedWatchRequests: 2,
expectedRequestOptions: []metav1.ListOptions{
{
SendInitialEvents: pointer.Bool(true),
AllowWatchBookmarks: true,
ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan,
TimeoutSeconds: pointer.Int64(1),
},
{
AllowWatchBookmarks: true,
ResourceVersion: "3",
TimeoutSeconds: pointer.Int64(1),
},
},
expectedStoreContent: []v1.Pod{*makePod("p1", "1"), *makePod("p3", "3")},
expectedError: apierrors.NewResourceExpired("rv already expired"),
},
{
name: "prove that the reflector is checking the value of the initialEventsEnd annotation",
closeAfterWatchEvents: 3,
watchEvents: []watch.Event{
{Type: watch.Added, Object: makePod("p1", "1")},
{Type: watch.Added, Object: makePod("p2", "2")},
{Type: watch.Bookmark, Object: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
ResourceVersion: "2",
Annotations: map[string]string{metav1.InitialEventsAnnotationKey: "false"},
},
}},
},
expectedWatchRequests: 1,
expectedRequestOptions: []metav1.ListOptions{{
SendInitialEvents: pointer.Bool(true),
AllowWatchBookmarks: true,
ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan,
TimeoutSeconds: pointer.Int64(1),
}},
},
}
for _, s := range scenarios {
t.Run(s.name, func(t *testing.T) {
scenario := s // capture as local variable
listWatcher, store, reflector, stopCh := testData()
go func() {
for i, e := range scenario.watchEvents {
listWatcher.fakeWatcher.Action(e.Type, e.Object)
if i+1 == scenario.stopAfterWatchEvents {
listWatcher.StopAndRecreateWatch()
continue
}
if i+1 == scenario.closeAfterWatchEvents {
close(stopCh)
}
}
}()
listWatcher.watchOptionsPredicate = scenario.watchOptionsPredicate
listWatcher.closeAfterWatchRequests = scenario.closeAfterWatchRequests
listWatcher.customListResponse = scenario.podList
listWatcher.closeAfterListRequests = scenario.closeAfterListRequests
if scenario.disableUseWatchList {
reflector.UseWatchList = ptr.To(false)
}
err := reflector.ListAndWatch(stopCh)
if scenario.expectedError != nil && err == nil {
t.Fatalf("expected error %q, got nil", scenario.expectedError)
}
if scenario.expectedError == nil && err != nil {
t.Fatalf("unexpected error: %v", err)
}
if scenario.expectedError != nil && err.Error() != scenario.expectedError.Error() {
t.Fatalf("expected error %q, got %q", scenario.expectedError, err.Error())
}
verifyWatchCounter(t, listWatcher, scenario.expectedWatchRequests)
verifyListCounter(t, listWatcher, scenario.expectedListRequests)
verifyRequestOptions(t, listWatcher, scenario.expectedRequestOptions)
verifyStore(t, store, scenario.expectedStoreContent)
})
}
}
func verifyRequestOptions(t *testing.T, lw *fakeListWatcher, expectedRequestOptions []metav1.ListOptions) {
if len(lw.requestOptions) != len(expectedRequestOptions) {
t.Fatalf("expected to receive exactly %v requests, got %v", len(expectedRequestOptions), len(lw.requestOptions))
}
for index, expectedRequestOption := range expectedRequestOptions {
actualRequestOption := lw.requestOptions[index]
if actualRequestOption.TimeoutSeconds == nil && expectedRequestOption.TimeoutSeconds != nil {
t.Fatalf("expected the request to specify TimeoutSeconds option but it didn't, actual = %#v, expected = %#v", actualRequestOption, expectedRequestOption)
}
if actualRequestOption.TimeoutSeconds != nil && expectedRequestOption.TimeoutSeconds == nil {
t.Fatalf("unexpected TimeoutSeconds option specified, actual = %#v, expected = %#v", actualRequestOption, expectedRequestOption)
}
// ignore actual values
actualRequestOption.TimeoutSeconds = nil
expectedRequestOption.TimeoutSeconds = nil
if !cmp.Equal(actualRequestOption, expectedRequestOption) {
t.Fatalf("expected %#v, got %#v", expectedRequestOption, actualRequestOption)
}
}
}
func verifyListCounter(t *testing.T, lw *fakeListWatcher, expectedListCounter int) {
if lw.listCounter != expectedListCounter {
t.Fatalf("unexpected number of LIST requests, got: %v, expected: %v", lw.listCounter, expectedListCounter)
}
}
func verifyWatchCounter(t *testing.T, lw *fakeListWatcher, expectedWatchCounter int) {
if lw.watchCounter != expectedWatchCounter {
t.Fatalf("unexpected number of WATCH requests, got: %v, expected: %v", lw.watchCounter, expectedWatchCounter)
}
}
type byName []v1.Pod
func (a byName) Len() int { return len(a) }
func (a byName) Less(i, j int) bool { return a[i].Name < a[j].Name }
func (a byName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func verifyStore(t *testing.T, s Store, expectedPods []v1.Pod) {
rawPods := s.List()
actualPods := []v1.Pod{}
for _, p := range rawPods {
actualPods = append(actualPods, *p.(*v1.Pod))
}
sort.Sort(byName(actualPods))
sort.Sort(byName(expectedPods))
if !cmp.Equal(actualPods, expectedPods, cmpopts.EquateEmpty()) {
t.Fatalf("unexpected store content, diff: %s", cmp.Diff(actualPods, expectedPods))
}
}
func makePod(name, rv string) *v1.Pod {
return &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: name, ResourceVersion: rv, UID: types.UID(name)}}
}
func testData() (*fakeListWatcher, Store, *Reflector, chan struct{}) {
s := NewStore(MetaNamespaceKeyFunc)
stopCh := make(chan struct{})
lw := &fakeListWatcher{
fakeWatcher: watch.NewFake(),
stop: func() {
close(stopCh)
},
}
r := NewReflector(lw, &v1.Pod{}, s, 0)
r.UseWatchList = ptr.To(true)
return lw, s, r, stopCh
}
type fakeListWatcher struct {
lock sync.Mutex
fakeWatcher *watch.FakeWatcher
listCounter int
watchCounter int
closeAfterWatchRequests int
closeAfterListRequests int
stop func()
requestOptions []metav1.ListOptions
customListResponse *v1.PodList
watchOptionsPredicate func(options metav1.ListOptions) error
}
func (lw *fakeListWatcher) List(options metav1.ListOptions) (runtime.Object, error) {
lw.listCounter++
lw.requestOptions = append(lw.requestOptions, options)
if lw.listCounter == lw.closeAfterListRequests {
lw.stop()
}
if lw.customListResponse != nil {
return lw.customListResponse, nil
}
return nil, fmt.Errorf("not implemented")
}
func (lw *fakeListWatcher) Watch(options metav1.ListOptions) (watch.Interface, error) {
lw.watchCounter++
lw.requestOptions = append(lw.requestOptions, options)
if lw.watchCounter == lw.closeAfterWatchRequests {
lw.stop()
}
if lw.watchOptionsPredicate != nil {
if err := lw.watchOptionsPredicate(options); err != nil {
return nil, err
}
}
lw.lock.Lock()
defer lw.lock.Unlock()
return lw.fakeWatcher, nil
}
func (lw *fakeListWatcher) StopAndRecreateWatch() {
lw.lock.Lock()
defer lw.lock.Unlock()
lw.fakeWatcher.Stop()
lw.fakeWatcher = watch.NewFake()
}
|