From 36d55ef47383e1578f4f8e68fa33d3a693e818e1 Mon Sep 17 00:00:00 2001 From: Tamal Saha Date: Mon, 18 Nov 2024 01:19:54 -0800 Subject: [PATCH] Share transport across clients Signed-off-by: Tamal Saha --- client/delegated.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/client/delegated.go b/client/delegated.go index f8bf7a660..6a4c4eca7 100644 --- a/client/delegated.go +++ b/client/delegated.go @@ -18,6 +18,7 @@ package client import ( "context" + "net/http" "strings" apiutil2 "kmodules.xyz/client-go/client/apiutil" @@ -28,6 +29,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apiserver/pkg/authentication/user" restclient "k8s.io/client-go/rest" + "k8s.io/client-go/transport" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/apiutil" ) @@ -98,7 +100,7 @@ func (d *DelegatingClient) RestConfig() *restclient.Config { return d.config } -func (d *DelegatingClient) Impersonate(u user.Info) (client.Client, error) { +func (d *DelegatingClient) Impersonate(u user.Info) (*restclient.Config, client.Client, error) { config := restclient.CopyConfig(d.config) config.Impersonate = restclient.ImpersonationConfig{ UserName: u.GetName(), @@ -106,16 +108,21 @@ func (d *DelegatingClient) Impersonate(u user.Info) (client.Client, error) { Groups: u.GetGroups(), Extra: u.GetExtra(), } - // share the transport between all clients - httpClient, err := restclient.HTTPClientFor(config) - if err != nil { - return nil, err - } + // share the transport between all clients optionsShallowCopy := d.options - optionsShallowCopy.HTTPClient = httpClient - - return NewClient(config, optionsShallowCopy) + if d.options.HTTPClient != nil { + optionsShallowCopy.HTTPClient = &http.Client{ + Transport: transport.NewImpersonatingRoundTripper(transport.ImpersonationConfig{ + UserName: u.GetName(), + UID: u.GetUID(), + Groups: u.GetGroups(), + Extra: u.GetExtra(), + }, d.options.HTTPClient.Transport), + } + } + cc, err := NewClient(config, optionsShallowCopy) + return config, cc, err } // GroupVersionKindFor returns the GroupVersionKind for the given object.