mirror of
https://github.com/rancher/norman.git
synced 2025-09-04 00:34:39 +00:00
Add timeouts to rest clients
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"github.com/rancher/norman/restwatch"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"k8s.io/apimachinery/pkg/api/meta"
|
"k8s.io/apimachinery/pkg/api/meta"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
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) {
|
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(p.getAPIPrefix(), p.gvk.Group, p.gvk.Version).
|
||||||
Prefix("watch").
|
Prefix("watch").
|
||||||
NamespaceIfScoped(p.ns, p.resource.Namespaced).
|
NamespaceIfScoped(p.ns, p.resource.Namespaced).
|
||||||
|
@@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/rancher/norman/clientbase"
|
"github.com/rancher/norman/clientbase"
|
||||||
"github.com/rancher/norman/controller"
|
"github.com/rancher/norman/controller"
|
||||||
|
"github.com/rancher/norman/restwatch"
|
||||||
"k8s.io/client-go/dynamic"
|
"k8s.io/client-go/dynamic"
|
||||||
"k8s.io/client-go/rest"
|
"k8s.io/client-go/rest"
|
||||||
)
|
)
|
||||||
@@ -33,7 +34,7 @@ func NewForConfig(config rest.Config) (Interface, error) {
|
|||||||
config.NegotiatedSerializer = configConfig.NegotiatedSerializer
|
config.NegotiatedSerializer = configConfig.NegotiatedSerializer
|
||||||
}
|
}
|
||||||
|
|
||||||
restClient, err := rest.UnversionedRESTClientFor(&config)
|
restClient, err := restwatch.UnversionedRESTClientFor(&config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
43
restwatch/rest.go
Normal file
43
restwatch/rest.go
Normal 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
|
||||||
|
}
|
@@ -6,6 +6,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/rancher/norman/restwatch"
|
||||||
"github.com/rancher/norman/types"
|
"github.com/rancher/norman/types"
|
||||||
"github.com/rancher/norman/types/convert"
|
"github.com/rancher/norman/types/convert"
|
||||||
"github.com/rancher/norman/types/values"
|
"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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if watchClient, ok := k8sClient.(restwatch.WatchClient); ok {
|
||||||
|
k8sClient = watchClient.WatchClient()
|
||||||
|
}
|
||||||
|
|
||||||
timeout := int64(60 * 60)
|
timeout := int64(60 * 60)
|
||||||
req := p.common(namespace, k8sClient.Get())
|
req := p.common(namespace, k8sClient.Get())
|
||||||
req.VersionedParams(&metav1.ListOptions{
|
req.VersionedParams(&metav1.ListOptions{
|
||||||
|
Reference in New Issue
Block a user