1
0
mirror of https://github.com/rancher/norman.git synced 2025-08-31 14:51:57 +00:00

Add timeouts to rest clients

This commit is contained in:
Darren Shepherd
2018-04-02 15:45:10 -07:00
parent 9f2b71df50
commit 510ed570d2
4 changed files with 57 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"github.com/pkg/errors"
"github.com/rancher/norman/restwatch"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -199,7 +200,12 @@ func (p *ObjectClient) List(opts metav1.ListOptions) (runtime.Object, error) {
}
func (p *ObjectClient) Watch(opts metav1.ListOptions) (watch.Interface, error) {
r, err := p.restClient.Get().
restClient := p.restClient
if watchClient, ok := restClient.(restwatch.WatchClient); ok {
restClient = watchClient.WatchClient()
}
r, err := restClient.Get().
Prefix(p.getAPIPrefix(), p.gvk.Group, p.gvk.Version).
Prefix("watch").
NamespaceIfScoped(p.ns, p.resource.Namespaced).

View File

@@ -8,6 +8,7 @@ import (
"github.com/rancher/norman/clientbase"
"github.com/rancher/norman/controller"
"github.com/rancher/norman/restwatch"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
)
@@ -33,7 +34,7 @@ func NewForConfig(config rest.Config) (Interface, error) {
config.NegotiatedSerializer = configConfig.NegotiatedSerializer
}
restClient, err := rest.UnversionedRESTClientFor(&config)
restClient, err := restwatch.UnversionedRESTClientFor(&config)
if err != nil {
return nil, err
}

43
restwatch/rest.go Normal file
View File

@@ -0,0 +1,43 @@
package restwatch
import (
"time"
"k8s.io/client-go/rest"
)
type WatchClient interface {
WatchClient() rest.Interface
}
func UnversionedRESTClientFor(config *rest.Config) (rest.Interface, error) {
client, err := rest.UnversionedRESTClientFor(config)
if err != nil {
return nil, err
}
if config.Timeout == 0 {
return client, err
}
newConfig := *config
newConfig.Timeout = time.Hour
watchClient, err := rest.UnversionedRESTClientFor(&newConfig)
if err != nil {
return nil, err
}
return &clientWithWatch{
RESTClient: client,
watchClient: watchClient,
}, nil
}
type clientWithWatch struct {
*rest.RESTClient
watchClient *rest.RESTClient
}
func (c *clientWithWatch) WatchClient() rest.Interface {
return c.watchClient
}

View File

@@ -6,6 +6,7 @@ import (
"strings"
"time"
"github.com/rancher/norman/restwatch"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/values"
@@ -187,6 +188,10 @@ func (p *Store) Watch(apiContext *types.APIContext, schema *types.Schema, opt *t
return nil, err
}
if watchClient, ok := k8sClient.(restwatch.WatchClient); ok {
k8sClient = watchClient.WatchClient()
}
timeout := int64(60 * 60)
req := p.common(namespace, k8sClient.Get())
req.VersionedParams(&metav1.ListOptions{