@@ -19,6 +19,7 @@ package client
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "net/http"
22
23
"strings"
23
24
24
25
"k8s.io/apimachinery/pkg/api/meta"
@@ -57,6 +58,9 @@ type Options struct {
57
58
// Mapper, if provided, will be used to map GroupVersionKinds to Resources
58
59
Mapper meta.RESTMapper
59
60
61
+ // HTTPClient, if provided, will be used by all constructed clients to talk to the apiserver
62
+ HTTPClient * http.Client
63
+
60
64
// Opts is used to configure the warning handler responsible for
61
65
// surfacing and handling warnings messages sent by the API server.
62
66
Opts WarningHandlerOptions
@@ -81,6 +85,14 @@ func newClient(config *rest.Config, options Options) (*client, error) {
81
85
return nil , fmt .Errorf ("must provide non-nil rest.Config to client.New" )
82
86
}
83
87
88
+ if options .HTTPClient == nil {
89
+ httpClient , err := rest .HTTPClientFor (config )
90
+ if err != nil {
91
+ return nil , fmt .Errorf ("error creating HTTPClient from config: %w" , err )
92
+ }
93
+ options .HTTPClient = httpClient
94
+ }
95
+
84
96
if ! options .Opts .SuppressWarnings {
85
97
// surface warnings
86
98
logger := log .Log .WithName ("KubeAPIWarningLogger" )
@@ -113,16 +125,17 @@ func newClient(config *rest.Config, options Options) (*client, error) {
113
125
}
114
126
115
127
clientcache := & clientCache {
116
- config : config ,
117
- scheme : options .Scheme ,
118
- mapper : options .Mapper ,
119
- codecs : serializer .NewCodecFactory (options .Scheme ),
128
+ config : config ,
129
+ httpClient : options .HTTPClient ,
130
+ scheme : options .Scheme ,
131
+ mapper : options .Mapper ,
132
+ codecs : serializer .NewCodecFactory (options .Scheme ),
120
133
121
134
structuredResourceByType : make (map [schema.GroupVersionKind ]* resourceMeta ),
122
135
unstructuredResourceByType : make (map [schema.GroupVersionKind ]* resourceMeta ),
123
136
}
124
137
125
- rawMetaClient , err := metadata .NewForConfig (config )
138
+ rawMetaClient , err := metadata .NewForConfigAndClient (config , options . HTTPClient )
126
139
if err != nil {
127
140
return nil , fmt .Errorf ("unable to construct metadata-only client for use as part of client: %w" , err )
128
141
}
0 commit comments