mirror of
https://github.com/kubernetes/client-go.git
synced 2025-09-02 15:45:09 +00:00
cache: add error handling to informers
When creating an informer, this adds a way to add custom error handling, so that Kubernetes tooling can properly surface the errors to the end user. Fixes https://github.com/kubernetes/client-go/issues/155 Kubernetes-commit: 435b40aa1e5c0ae44e0aeb9aa6dbde79838b3390
This commit is contained in:
committed by
Kubernetes Publisher
parent
8e91b7aa91
commit
ccd5becdff
27
tools/cache/shared_informer_test.go
vendored
27
tools/cache/shared_informer_test.go
vendored
@@ -18,6 +18,7 @@ package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -330,3 +331,29 @@ func TestSharedInformerWatchDisruption(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSharedInformerErrorHandling(t *testing.T) {
|
||||
source := fcache.NewFakeControllerSource()
|
||||
source.Add(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1"}})
|
||||
source.ListError = fmt.Errorf("Access Denied")
|
||||
|
||||
informer := NewSharedInformer(source, &v1.Pod{}, 1*time.Second).(*sharedIndexInformer)
|
||||
|
||||
errCh := make(chan error)
|
||||
_ = informer.SetWatchErrorHandler(func(_ *Reflector, err error) {
|
||||
errCh <- err
|
||||
})
|
||||
|
||||
stop := make(chan struct{})
|
||||
go informer.Run(stop)
|
||||
|
||||
select {
|
||||
case err := <-errCh:
|
||||
if !strings.Contains(err.Error(), "Access Denied") {
|
||||
t.Errorf("Expected 'Access Denied' error. Actual: %v", err)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Errorf("Timeout waiting for error handler call")
|
||||
}
|
||||
close(stop)
|
||||
}
|
||||
|
Reference in New Issue
Block a user