@@ -19,6 +19,7 @@ package declarative
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "net/http"
22
23
"sync"
23
24
24
25
"k8s.io/apimachinery/pkg/api/meta"
@@ -55,6 +56,9 @@ type WatchChildrenOptions struct {
55
56
// RESTConfig is the configuration for connecting to the cluster.
56
57
RESTConfig * rest.Config
57
58
59
+ // HTTPClient is the HTTP client to use for requests.
60
+ HTTPClient * http.Client
61
+
58
62
// LabelMaker is used to build the labels we should watch on.
59
63
LabelMaker LabelMaker
60
64
@@ -88,6 +92,21 @@ func WatchChildren(options WatchChildrenOptions) error {
88
92
return fmt .Errorf ("labelMaker is required to scope watches" )
89
93
}
90
94
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
+
91
110
if options .RESTConfig == nil {
92
111
if options .Manager != nil {
93
112
options .RESTConfig = options .Manager .GetConfig ()
@@ -100,23 +119,19 @@ func WatchChildren(options WatchChildrenOptions) error {
100
119
if options .Manager != nil {
101
120
restMapper = options .Manager .GetRESTMapper ()
102
121
} 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 )
108
123
if err != nil {
109
124
return err
110
125
}
111
126
restMapper = rm
112
127
}
113
128
114
- client , err := dynamic .NewForConfig (options .RESTConfig )
129
+ dynamicClient , err := dynamic .NewForConfigAndClient (options .RESTConfig , httpClient )
115
130
if err != nil {
116
131
return err
117
132
}
118
133
119
- dw , events , err := watch .NewDynamicWatch (restMapper , client )
134
+ dw , events , err := watch .NewDynamicWatch (restMapper , dynamicClient )
120
135
if err != nil {
121
136
return fmt .Errorf ("creating dynamic watch: %v" , err )
122
137
}
0 commit comments