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:
Nick Santos
2020-01-17 12:46:08 -05:00
committed by Kubernetes Publisher
parent 8e91b7aa91
commit ccd5becdff
5 changed files with 114 additions and 23 deletions

View File

@@ -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)
}