Skip to content

Commit

Permalink
Merge pull request #3120 from sttts/sttts-roundtripper-wrapper
Browse files Browse the repository at this point in the history
🐛 Implement RoundTripperWrapper everywhere to allow cancellation
  • Loading branch information
kcp-ci-bot committed Apr 22, 2024
2 parents 8f8fe3d + f965ef7 commit 86241b4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
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

0 comments on commit 86241b4

Please sign in to comment.