Skip to content

Commit

Permalink
Share transport across clients
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Nov 18, 2024
1 parent 864f373 commit 9f659a5
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions client/delegated.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package client

import (
"context"
"net/http"
"strings"

apiutil2 "kmodules.xyz/client-go/client/apiutil"
Expand All @@ -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"
)
Expand Down Expand Up @@ -106,15 +108,19 @@ 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

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),
}
}
return NewClient(config, optionsShallowCopy)
}

Expand Down

0 comments on commit 9f659a5

Please sign in to comment.