-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Bug 1701041: Adding timeout on dependent watch establishment #1638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -27,6 +27,7 @@ import ( | |||||
| "net/http" | ||||||
| "strings" | ||||||
| "sync" | ||||||
| "time" | ||||||
|
|
||||||
| "github.com/operator-framework/operator-sdk/pkg/ansible/proxy/controllermap" | ||||||
| "github.com/operator-framework/operator-sdk/pkg/ansible/proxy/kubeconfig" | ||||||
|
|
@@ -39,10 +40,16 @@ import ( | |||||
| "k8s.io/client-go/discovery" | ||||||
| "k8s.io/client-go/rest" | ||||||
| "sigs.k8s.io/controller-runtime/pkg/cache" | ||||||
| "sigs.k8s.io/controller-runtime/pkg/controller" | ||||||
| "sigs.k8s.io/controller-runtime/pkg/handler" | ||||||
| "sigs.k8s.io/controller-runtime/pkg/predicate" | ||||||
| "sigs.k8s.io/controller-runtime/pkg/source" | ||||||
| ) | ||||||
|
|
||||||
| // This is the default timeout to wait for the cache to respond | ||||||
| // TODO: Eventually this should be configurable | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| const cacheEstablishmentTimeout = 6 * time.Second | ||||||
|
|
||||||
| // RequestLogHandler - log the requests that come through the proxy. | ||||||
| func RequestLogHandler(h http.Handler) http.Handler { | ||||||
| return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||||||
|
|
@@ -225,8 +232,9 @@ func addWatchToController(owner kubeconfig.NamespacedOwnerReference, cMap *contr | |||||
| owMap.Store(resource.GroupVersionKind()) | ||||||
| log.Info("Watching child resource", "kind", resource.GroupVersionKind(), "enqueue_kind", u.GroupVersionKind()) | ||||||
| // Store watch in map | ||||||
| err := contents.Controller.Watch(&source.Kind{Type: resource}, &handler.EnqueueRequestForOwner{OwnerType: u}) | ||||||
| err := addWatch(contents.Controller, &source.Kind{Type: resource}, &handler.EnqueueRequestForOwner{OwnerType: u}) | ||||||
| if err != nil { | ||||||
| log.Error(err, "GVK", resource.GroupVersionKind()) | ||||||
| return err | ||||||
| } | ||||||
| case (!useOwnerRef && dataNamespaceScoped) || contents.WatchClusterScopedResources: | ||||||
|
|
@@ -238,15 +246,31 @@ func addWatchToController(owner kubeconfig.NamespacedOwnerReference, cMap *contr | |||||
| awMap.Store(resource.GroupVersionKind()) | ||||||
| typeString := fmt.Sprintf("%v.%v", owner.Kind, ownerGV.Group) | ||||||
| log.Info("Watching child resource", "kind", resource.GroupVersionKind(), "enqueue_annotation_type", typeString) | ||||||
| err = contents.Controller.Watch(&source.Kind{Type: resource}, &osdkHandler.EnqueueRequestForAnnotation{Type: typeString}) | ||||||
| err = addWatch(contents.Controller, &source.Kind{Type: resource}, &osdkHandler.EnqueueRequestForAnnotation{Type: typeString}) | ||||||
| if err != nil { | ||||||
| log.Error(err, "GVK", resource.GroupVersionKind()) | ||||||
| return err | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| func addWatch(c controller.Controller, s source.Source, eh handler.EventHandler, predicates ...predicate.Predicate) error { | ||||||
| errChan := make(chan error, 1) | ||||||
| go func() { | ||||||
| err := c.Watch(s, eh, predicates...) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should work upstream to get a The problem here is that it is not possible to cancel the |
||||||
| errChan <- err | ||||||
| }() | ||||||
|
|
||||||
| select { | ||||||
| case watchErr := <-errChan: | ||||||
| return watchErr | ||||||
| case <-time.After(cacheEstablishmentTimeout): | ||||||
| return fmt.Errorf("timeout establishing watch, commonly permissions of the controller are not sufficent") | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| func removeAuthorizationHeader(h http.Handler) http.Handler { | ||||||
| return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||||||
| req.Header.Del("Authorization") | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than using a channel, a goroutine, and a timer, a more idiomatic approach would be to use
context.WithTimeout(context.Background(), cacheEstablishmentTimeout)And then check the error from the
Get()call to see if it is acontext.DeadlineExceedederror.Ditto for the
informerCache.List()call above.