Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Implement RoundTripperWrapper everywhere to allow cancellation #3120

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pkg/cache/client/round_tripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ func (c *ShardRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)
return c.delegate.RoundTrip(req)
}

func (c *ShardRoundTripper) WrappedRoundTripper() http.RoundTripper {
return c.delegate
}

// generatePath formats the request path to target the specified shard.
func generatePath(originalPath string, shard clientshard.Name) (string, error) {
// if the originalPath already has the shard then the path was already modified and no change needed
Expand Down Expand Up @@ -151,6 +155,10 @@ func (c *DefaultShardRoundTripper) RoundTrip(req *http.Request) (*http.Response,
return c.delegate.RoundTrip(req)
}

func (c *DefaultShardRoundTripper) WrappedRoundTripper() http.RoundTripper {
return c.delegate
}

// WithShardNameFromObjectRoundTripper wraps an existing config with ShardNameFromObjectRoundTripper.
//
// Note: it is the caller responsibility to make a copy of the rest config.
Expand Down Expand Up @@ -221,6 +229,10 @@ func (c *ShardNameFromObjectRoundTripper) RoundTrip(req *http.Request) (*http.Re
return c.delegate.RoundTrip(req)
}

func (c *ShardNameFromObjectRoundTripper) WrappedRoundTripper() http.RoundTripper {
return c.delegate
}

// WithCacheServiceRoundTripper wraps an existing config's with CacheServiceRoundTripper.
func WithCacheServiceRoundTripper(cfg *rest.Config) *rest.Config {
cfg.Wrap(func(rt http.RoundTripper) http.RoundTripper {
Expand Down Expand Up @@ -260,3 +272,7 @@ func (c *CacheServiceRoundTripper) RoundTrip(req *http.Request) (*http.Response,
}
return c.delegate.RoundTrip(req)
}

func (c *CacheServiceRoundTripper) WrappedRoundTripper() http.RoundTripper {
return c.delegate
}
4 changes: 4 additions & 0 deletions pkg/metadata/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (t *metadataTransport) RoundTrip(req *http.Request) (*http.Response, error)
return t.RoundTripper.RoundTrip(req)
}

func (t *metadataTransport) WrappedRoundTripper() http.RoundTripper {
return t.RoundTripper
}

func partialType(req *http.Request) (string, error) {
// strip off /clusters/<lcluster>
baseReq := *req
Expand Down
44 changes: 27 additions & 17 deletions pkg/server/bootstrap/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,33 +226,43 @@ func apiExportIdentityProvider(config *rest.Config, localShardKubeClusterClient
}
}

type roundTripperFunc func(*http.Request) (*http.Response, error)
type roundTripperFunc struct {
delegate http.RoundTripper
fn func(*http.Request) (*http.Response, error)
}

var _ http.RoundTripper = roundTripperFunc(nil)
var _ http.RoundTripper = roundTripperFunc{}

func (rt roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return rt(r)
return rt.fn(r)
}

func (rt roundTripperFunc) WrappedRoundTripper() http.RoundTripper {
return rt.delegate
}

// injectKcpIdentities injects the KCP identities into the request URLs.
func injectKcpIdentities(ids *identities) func(rt http.RoundTripper) http.RoundTripper {
return func(rt http.RoundTripper) http.RoundTripper {
return roundTripperFunc(func(origReq *http.Request) (*http.Response, error) {
urlPath, err := decorateWildcardPathsWithResourceIdentities(origReq.URL.Path, ids)
if err != nil {
return nil, err
}
if urlPath == origReq.URL.Path {
return rt.RoundTrip(origReq)
}
return roundTripperFunc{
delegate: rt,
fn: func(origReq *http.Request) (*http.Response, error) {
urlPath, err := decorateWildcardPathsWithResourceIdentities(origReq.URL.Path, ids)
if err != nil {
return nil, err
}
if urlPath == origReq.URL.Path {
return rt.RoundTrip(origReq)
}

req := *origReq // shallow copy
req.URL = &url.URL{}
*req.URL = *origReq.URL
req.URL.Path = urlPath
req := *origReq // shallow copy
req.URL = &url.URL{}
*req.URL = *origReq.URL
req.URL.Path = urlPath

return rt.RoundTrip(&req)
})
return rt.RoundTrip(&req)
},
}
}
}

Expand Down
Loading