From 2a2b561ab0b4cf0fa575b124e58c9d9c8f2085d2 Mon Sep 17 00:00:00 2001 From: justinsb Date: Mon, 5 Feb 2024 08:50:57 -0500 Subject: [PATCH] Allow http client reuse in dynamic client By using ForConfigAndClient, we are more likely to reuse the http connection. We expose an HTTPClient, but also we default to the manager HTTPClient. --- pkg/patterns/declarative/watch.go | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/pkg/patterns/declarative/watch.go b/pkg/patterns/declarative/watch.go index f41a020a..ec93bb3c 100644 --- a/pkg/patterns/declarative/watch.go +++ b/pkg/patterns/declarative/watch.go @@ -55,6 +55,9 @@ type WatchChildrenOptions struct { // RESTConfig is the configuration for connecting to the cluster. RESTConfig *rest.Config + // HTTPClient is the HTTP client to use for requests. + HTTPClient *http.Client + // LabelMaker is used to build the labels we should watch on. LabelMaker LabelMaker @@ -88,6 +91,21 @@ func WatchChildren(options WatchChildrenOptions) error { return fmt.Errorf("labelMaker is required to scope watches") } + var httpClient *http.Client + if options.HTTPClient != nil { + httpClient = options.HTTPClient + } else { + if options.RESTConfig != nil { + hc, err := rest.HTTPClientFor(options.RESTConfig) + if err != nil { + return err + } + httpClient = hc + } else if options.Manager != nil { + httpClient = options.Manager.GetHTTPClient() + } + } + if options.RESTConfig == nil { if options.Manager != nil { options.RESTConfig = options.Manager.GetConfig() @@ -100,23 +118,19 @@ func WatchChildren(options WatchChildrenOptions) error { if options.Manager != nil { restMapper = options.Manager.GetRESTMapper() } else { - client, err := rest.HTTPClientFor(options.RESTConfig) - if err != nil { - return err - } - rm, err := commonclient.NewDiscoveryRESTMapper(options.RESTConfig, client) + rm, err := commonclient.NewDiscoveryRESTMapper(options.RESTConfig, httpClient) if err != nil { return err } restMapper = rm } - client, err := dynamic.NewForConfig(options.RESTConfig) + dynamicClient, err := dynamic.NewForConfigAndClient(options.RESTConfig, httpClient) if err != nil { return err } - dw, events, err := watch.NewDynamicWatch(restMapper, client) + dw, events, err := watch.NewDynamicWatch(restMapper, dynamicClient) if err != nil { return fmt.Errorf("creating dynamic watch: %v", err) }