Skip to content

Commit b07f7e8

Browse files
ncdcfabianvf
andcommitted
UPSTREAM: <carry>: support preexisting http.Clients
Co-authored-by: Fabian von Feilitzsch <[email protected]>
1 parent 02fb225 commit b07f7e8

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

pkg/client/apiutil/apimachinery.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package apiutil
2121

2222
import (
2323
"fmt"
24+
"net/http"
2425
"reflect"
2526
"sync"
2627

@@ -122,6 +123,13 @@ func RESTClientForGVK(gvk schema.GroupVersionKind, isUnstructured bool, baseConf
122123
return rest.RESTClientFor(createRestConfig(gvk, isUnstructured, baseConfig, codecs))
123124
}
124125

126+
// RESTClientForGVKAndClient constructs a new rest.Interface capable of accessing the resource associated
127+
// wwith the give GroupVersionKind. The REST client will be configured to use provided http.Client, and the
128+
// negotiated serializer from baseConfig, if set.
129+
func RESTClientForGVKAndClient(gvk schema.GroupVersionKind, client *http.Client, isUnstructured bool, baseConfig *rest.Config, codecs serializer.CodecFactory) (rest.Interface, error) {
130+
return rest.RESTClientForConfigAndClient(createRestConfig(gvk, isUnstructured, baseConfig, codecs), client)
131+
}
132+
125133
// serializerWithDecodedGVK is a CodecFactory that overrides the DecoderToVersion of a WithoutConversionCodecFactory
126134
// in order to avoid clearing the GVK from the decoded object.
127135
//

pkg/client/client.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package client
1919
import (
2020
"context"
2121
"fmt"
22+
"net/http"
2223
"strings"
2324

2425
"k8s.io/apimachinery/pkg/api/meta"
@@ -57,6 +58,9 @@ type Options struct {
5758
// Mapper, if provided, will be used to map GroupVersionKinds to Resources
5859
Mapper meta.RESTMapper
5960

61+
// HTTPClient, if provided, will be used by all constructed clients to talk to the apiserver
62+
HTTPClient *http.Client
63+
6064
// Opts is used to configure the warning handler responsible for
6165
// surfacing and handling warnings messages sent by the API server.
6266
Opts WarningHandlerOptions
@@ -81,6 +85,14 @@ func newClient(config *rest.Config, options Options) (*client, error) {
8185
return nil, fmt.Errorf("must provide non-nil rest.Config to client.New")
8286
}
8387

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+
8496
if !options.Opts.SuppressWarnings {
8597
// surface warnings
8698
logger := log.Log.WithName("KubeAPIWarningLogger")
@@ -113,16 +125,17 @@ func newClient(config *rest.Config, options Options) (*client, error) {
113125
}
114126

115127
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),
120133

121134
structuredResourceByType: make(map[schema.GroupVersionKind]*resourceMeta),
122135
unstructuredResourceByType: make(map[schema.GroupVersionKind]*resourceMeta),
123136
}
124137

125-
rawMetaClient, err := metadata.NewForConfig(config)
138+
rawMetaClient, err := metadata.NewForConfigAndClient(config, options.HTTPClient)
126139
if err != nil {
127140
return nil, fmt.Errorf("unable to construct metadata-only client for use as part of client: %w", err)
128141
}

pkg/client/client_cache.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package client
1818

1919
import (
20+
"net/http"
2021
"strings"
2122
"sync"
2223

@@ -35,6 +36,9 @@ type clientCache struct {
3536
// config is the rest.Config to talk to an apiserver
3637
config *rest.Config
3738

39+
// httpClient is the http.Client to talk to an apiserver
40+
httpClient *http.Client
41+
3842
// scheme maps go structs to GroupVersionKinds
3943
scheme *runtime.Scheme
4044

@@ -59,7 +63,7 @@ func (c *clientCache) newResource(gvk schema.GroupVersionKind, isList, isUnstruc
5963
gvk.Kind = gvk.Kind[:len(gvk.Kind)-4]
6064
}
6165

62-
client, err := apiutil.RESTClientForGVK(gvk, isUnstructured, c.config, c.codecs)
66+
client, err := apiutil.RESTClientForGVKAndClient(gvk, c.httpClient, isUnstructured, c.config, c.codecs)
6367
if err != nil {
6468
return nil, err
6569
}

0 commit comments

Comments
 (0)