Skip to content

Commit f3dc506

Browse files
authored
Merge pull request #377 from justinsb/more_httpclient_sharing
Allow http client reuse in dynamic client
2 parents 8a932a2 + 26c55d2 commit f3dc506

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

pkg/patterns/declarative/watch.go

+22-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package declarative
1919
import (
2020
"context"
2121
"fmt"
22+
"net/http"
2223
"sync"
2324

2425
"k8s.io/apimachinery/pkg/api/meta"
@@ -55,6 +56,9 @@ type WatchChildrenOptions struct {
5556
// RESTConfig is the configuration for connecting to the cluster.
5657
RESTConfig *rest.Config
5758

59+
// HTTPClient is the HTTP client to use for requests.
60+
HTTPClient *http.Client
61+
5862
// LabelMaker is used to build the labels we should watch on.
5963
LabelMaker LabelMaker
6064

@@ -88,6 +92,21 @@ func WatchChildren(options WatchChildrenOptions) error {
8892
return fmt.Errorf("labelMaker is required to scope watches")
8993
}
9094

95+
var httpClient *http.Client
96+
if options.HTTPClient != nil {
97+
httpClient = options.HTTPClient
98+
} else {
99+
if options.RESTConfig != nil {
100+
hc, err := rest.HTTPClientFor(options.RESTConfig)
101+
if err != nil {
102+
return err
103+
}
104+
httpClient = hc
105+
} else if options.Manager != nil {
106+
httpClient = options.Manager.GetHTTPClient()
107+
}
108+
}
109+
91110
if options.RESTConfig == nil {
92111
if options.Manager != nil {
93112
options.RESTConfig = options.Manager.GetConfig()
@@ -100,23 +119,19 @@ func WatchChildren(options WatchChildrenOptions) error {
100119
if options.Manager != nil {
101120
restMapper = options.Manager.GetRESTMapper()
102121
} else {
103-
client, err := rest.HTTPClientFor(options.RESTConfig)
104-
if err != nil {
105-
return err
106-
}
107-
rm, err := commonclient.NewDiscoveryRESTMapper(options.RESTConfig, client)
122+
rm, err := commonclient.NewDiscoveryRESTMapper(options.RESTConfig, httpClient)
108123
if err != nil {
109124
return err
110125
}
111126
restMapper = rm
112127
}
113128

114-
client, err := dynamic.NewForConfig(options.RESTConfig)
129+
dynamicClient, err := dynamic.NewForConfigAndClient(options.RESTConfig, httpClient)
115130
if err != nil {
116131
return err
117132
}
118133

119-
dw, events, err := watch.NewDynamicWatch(restMapper, client)
134+
dw, events, err := watch.NewDynamicWatch(restMapper, dynamicClient)
120135
if err != nil {
121136
return fmt.Errorf("creating dynamic watch: %v", err)
122137
}

0 commit comments

Comments
 (0)