Skip to content

Commit bebd746

Browse files
Adding context timeout for Ansible proxy (#2264)
**Description** Add timeout in the watch feature for Ansible based-operators proxy to avoid appears that the reconcile is stuck and hang when the operator has not the correct permissions to List and Watch the resources. **Motivation for the change:** - #1638 - https://bugzilla.redhat.com/show_bug.cgi?id=1701041 **Note** Also, solved by kubernetes-sigs/controller-runtime#663.
1 parent d48e208 commit bebd746

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# entries is a list of entries to include in
2+
# release notes and/or the migration guide
3+
entries:
4+
- description: >
5+
Added timeout to the Ansible based-operator proxy, which enables error reporting for requests that fail due to RBAC permissions issues to List and Watch the resources.
6+
7+
# kind iss one of:
8+
# - addition
9+
# - change
10+
# - deprecation
11+
# - removal
12+
# - bugfix
13+
kind: "bugfix"

pkg/ansible/proxy/cache_response.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,9 @@ func (c *cacheResponseHandler) getListFromCache(r *requestfactory.RequestInfo, r
272272
k.Kind = k.Kind + "List"
273273
un := unstructured.UnstructuredList{}
274274
un.SetGroupVersionKind(k)
275-
err := c.informerCache.List(context.Background(), &un, clientListOpts...)
275+
ctx, cancel := context.WithTimeout(context.Background(), cacheEstablishmentTimeout)
276+
defer cancel()
277+
err := c.informerCache.List(ctx, &un, clientListOpts...)
276278
if err != nil {
277279
// break here in case resource doesn't exist in cache but exists on APIserver
278280
// This is very unlikely but provides user with expected 404
@@ -287,7 +289,9 @@ func (c *cacheResponseHandler) getObjectFromCache(r *requestfactory.RequestInfo,
287289
un := &unstructured.Unstructured{}
288290
un.SetGroupVersionKind(k)
289291
obj := client.ObjectKey{Namespace: r.Namespace, Name: r.Name}
290-
err := c.informerCache.Get(context.Background(), obj, un)
292+
ctx, cancel := context.WithTimeout(context.Background(), cacheEstablishmentTimeout)
293+
defer cancel()
294+
err := c.informerCache.Get(ctx, obj, un)
291295
if err != nil {
292296
// break here in case resource doesn't exist in cache but exists on APIserver
293297
// This is very unlikely but provides user with expected 404

pkg/ansible/proxy/proxy.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"net/http"
2828
"strings"
2929
"sync"
30+
"time"
3031

3132
"github.com/operator-framework/operator-sdk/pkg/ansible/proxy/controllermap"
3233
"github.com/operator-framework/operator-sdk/pkg/ansible/proxy/kubeconfig"
@@ -44,6 +45,9 @@ import (
4445
"sigs.k8s.io/controller-runtime/pkg/source"
4546
)
4647

48+
// This is the default timeout to wait for the cache to respond
49+
// todo(shawn-hurley): Eventually this should be configurable
50+
const cacheEstablishmentTimeout = 6 * time.Second
4751
const AutoSkipCacheREList = "^/api/.*/pods/.*/exec,^/api/.*/pods/.*/attach"
4852

4953
// RequestLogHandler - log the requests that come through the proxy.
@@ -244,6 +248,8 @@ func addWatchToController(owner kubeconfig.NamespacedOwnerReference, cMap *contr
244248
&handler.EnqueueRequestForOwner{OwnerType: u}, dependentPredicate)
245249
// Store watch in map
246250
if err != nil {
251+
log.Error(err, "Failed to watch child resource",
252+
"kind", resource.GroupVersionKind(), "enqueue_kind", u.GroupVersionKind())
247253
return err
248254
}
249255
case (!useOwnerRef && dataNamespaceScoped) || contents.WatchClusterScopedResources:
@@ -259,6 +265,8 @@ func addWatchToController(owner kubeconfig.NamespacedOwnerReference, cMap *contr
259265
err = contents.Controller.Watch(&source.Kind{Type: resource},
260266
&osdkHandler.EnqueueRequestForAnnotation{Type: typeString}, dependentPredicate)
261267
if err != nil {
268+
log.Error(err, "Failed to watch child resource",
269+
"kind", resource.GroupVersionKind(), "enqueue_kind", u.GroupVersionKind())
262270
return err
263271
}
264272
}

0 commit comments

Comments
 (0)