@@ -19,6 +19,7 @@ package cluster
1919import (
2020 "context"
2121 "errors"
22+ "net/http"
2223 "time"
2324
2425 "github.com/go-logr/logr"
@@ -105,6 +106,11 @@ type Options struct {
105106 // will only hold objects from the desired namespace.
106107 Namespace string
107108
109+ // HTTPClient is the http client that will be used to create the default
110+ // Cache and Client. If not set the rest.HTTPClientFor function will be used
111+ // to create the http client.
112+ HTTPClient * http.Client
113+
108114 // NewCache is the function that will create the cache to be used
109115 // by the manager. If not set this will use the default new cache function.
110116 NewCache cache.NewCacheFunc
@@ -153,7 +159,7 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) {
153159 for _ , opt := range opts {
154160 opt (& options )
155161 }
156- options = setOptionsDefaults (options )
162+ options = setOptionsDefaults (options , config )
157163
158164 // Create the mapper provider
159165 mapper , err := options .MapperProvider (config )
@@ -206,31 +212,47 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) {
206212}
207213
208214// setOptionsDefaults set default values for Options fields.
209- func setOptionsDefaults (options Options ) Options {
215+ func setOptionsDefaults (options Options , config * rest.Config ) Options {
216+ if options .HTTPClient == nil {
217+ httpClient , err := rest .HTTPClientFor (config )
218+ if err != nil {
219+ panic (err )
220+ }
221+ options .HTTPClient = httpClient
222+ }
223+
210224 // Use the Kubernetes client-go scheme if none is specified
211225 if options .Scheme == nil {
212226 options .Scheme = scheme .Scheme
213227 }
214228
215229 if options .MapperProvider == nil {
216230 options .MapperProvider = func (c * rest.Config ) (meta.RESTMapper , error ) {
217- return apiutil .NewDynamicRESTMapper ( c )
231+ return apiutil .NewDynamicRESTMapperForConfigAndClient ( c , options . HTTPClient )
218232 }
219233 }
220234
221235 // Allow users to define how to create a new client
222236 if options .NewClient == nil {
223- options .NewClient = DefaultNewClient
237+ options .NewClient = func (cache cache.Cache , config * rest.Config , clientOptions client.Options , uncachedObjects ... client.Object ) (client.Client , error ) {
238+ return ClientBuilderWithOptions (ClientOptions {
239+ HTTPClient : options .HTTPClient ,
240+ })(cache , config , clientOptions , uncachedObjects ... )
241+ }
224242 }
225243
226244 // Allow newCache to be mocked
227245 if options .NewCache == nil {
228- options .NewCache = cache .New
246+ options .NewCache = func (config * rest.Config , opts cache.Options ) (cache.Cache , error ) {
247+ return cache .NewForConfigAndClient (config , options .HTTPClient , opts )
248+ }
229249 }
230250
231251 // Allow newRecorderProvider to be mocked
232252 if options .newRecorderProvider == nil {
233- options .newRecorderProvider = intrec .NewProvider
253+ options .newRecorderProvider = func (config * rest.Config , scheme * runtime.Scheme , logger logr.Logger , makeBroadcaster intrec.EventBroadcasterProducer ) (* intrec.Provider , error ) {
254+ return intrec .NewProviderForConfigAndClient (config , options .HTTPClient , scheme , logger , makeBroadcaster )
255+ }
234256 }
235257
236258 // This is duplicated with pkg/manager, we need it here to provide
@@ -260,6 +282,7 @@ type NewClientFunc func(cache cache.Cache, config *rest.Config, options client.O
260282type ClientOptions struct {
261283 UncachedObjects []client.Object
262284 CacheUnstructured bool
285+ HTTPClient * http.Client
263286}
264287
265288// DefaultNewClient creates the default caching client, that will never cache Unstructured.
@@ -273,7 +296,7 @@ func ClientBuilderWithOptions(options ClientOptions) NewClientFunc {
273296 return func (cache cache.Cache , config * rest.Config , clientOpts client.Options , uncachedObjects ... client.Object ) (client.Client , error ) {
274297 options .UncachedObjects = append (options .UncachedObjects , uncachedObjects ... )
275298
276- c , err := client .New (config , clientOpts )
299+ c , err := client .NewForConfigAndClient (config , options . HTTPClient , clientOpts )
277300 if err != nil {
278301 return nil , err
279302 }
0 commit comments