Skip to content

Commit

Permalink
Share transport across clients (#608)
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha authored Nov 18, 2024
1 parent 864f373 commit 54d0f57
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 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 @@ -98,24 +100,29 @@ 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(),
UID: u.GetUID(),
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.
Expand Down

0 comments on commit 54d0f57

Please sign in to comment.