-
Notifications
You must be signed in to change notification settings - Fork 407
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
[bugfix] fix transport race conditions in yurthub #683
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,15 +40,16 @@ import ( | |
|
||
// RemoteProxy is an reverse proxy for remote server | ||
type RemoteProxy struct { | ||
checker healthchecker.HealthChecker | ||
reverseProxy *httputil.ReverseProxy | ||
cacheMgr cachemanager.CacheManager | ||
remoteServer *url.URL | ||
filterChain filter.Interface | ||
currentTransport http.RoundTripper | ||
bearerTransport http.RoundTripper | ||
upgradeHandler *proxy.UpgradeAwareHandler | ||
stopCh <-chan struct{} | ||
checker healthchecker.HealthChecker | ||
reverseProxy *httputil.ReverseProxy | ||
cacheMgr cachemanager.CacheManager | ||
remoteServer *url.URL | ||
filterChain filter.Interface | ||
currentTransport http.RoundTripper | ||
bearerTransport http.RoundTripper | ||
upgradeHandler *proxy.UpgradeAwareHandler | ||
bearerUpgradeHandler *proxy.UpgradeAwareHandler | ||
stopCh <-chan struct{} | ||
} | ||
|
||
type responder struct{} | ||
|
@@ -74,22 +75,25 @@ func NewRemoteProxy(remoteServer *url.URL, | |
return nil, fmt.Errorf("could not get bearer transport when init proxy backend(%s)", remoteServer.String()) | ||
} | ||
|
||
upgradeAwareHandler := proxy.NewUpgradeAwareHandler(remoteServer, nil, false, true, &responder{}) | ||
upgradeAwareHandler := proxy.NewUpgradeAwareHandler(remoteServer, currentTransport, false, true, &responder{}) | ||
upgradeAwareHandler.UseRequestLocation = true | ||
bearerUpgradeAwareHandler := proxy.NewUpgradeAwareHandler(remoteServer, bearerTransport, false, true, &responder{}) | ||
bearerUpgradeAwareHandler.UseRequestLocation = true | ||
|
||
proxyBackend := &RemoteProxy{ | ||
checker: healthChecker, | ||
reverseProxy: httputil.NewSingleHostReverseProxy(remoteServer), | ||
cacheMgr: cacheMgr, | ||
remoteServer: remoteServer, | ||
filterChain: filterChain, | ||
currentTransport: currentTransport, | ||
bearerTransport: bearerTransport, | ||
upgradeHandler: upgradeAwareHandler, | ||
stopCh: stopCh, | ||
checker: healthChecker, | ||
reverseProxy: httputil.NewSingleHostReverseProxy(remoteServer), | ||
cacheMgr: cacheMgr, | ||
remoteServer: remoteServer, | ||
filterChain: filterChain, | ||
currentTransport: currentTransport, | ||
bearerTransport: bearerTransport, | ||
upgradeHandler: upgradeAwareHandler, | ||
bearerUpgradeHandler: bearerUpgradeAwareHandler, | ||
stopCh: stopCh, | ||
} | ||
|
||
proxyBackend.reverseProxy.Transport = currentTransport | ||
proxyBackend.reverseProxy.Transport = proxyBackend | ||
proxyBackend.reverseProxy.ModifyResponse = proxyBackend.modifyResponse | ||
proxyBackend.reverseProxy.FlushInterval = -1 | ||
proxyBackend.reverseProxy.ErrorHandler = proxyBackend.errorHandler | ||
|
@@ -106,21 +110,13 @@ func (rp *RemoteProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) { | |
if httpstream.IsUpgradeRequest(req) { | ||
klog.V(5).Infof("get upgrade request %s", req.URL) | ||
if isBearerRequest(req) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned in https://github.com/openyurtio/openyurt/pull/683/files#r769771572, if we group
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As replied above |
||
rp.upgradeHandler.UpgradeTransport = proxy.NewUpgradeRequestRoundTripper(rp.bearerTransport, proxy.MirrorRequest) | ||
rp.bearerUpgradeHandler.ServeHTTP(rw, req) | ||
} else { | ||
rp.upgradeHandler.UpgradeTransport = proxy.NewUpgradeRequestRoundTripper(rp.currentTransport, proxy.MirrorRequest) | ||
rp.upgradeHandler.ServeHTTP(rw, req) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the same as above |
||
} | ||
rp.upgradeHandler.ServeHTTP(rw, req) | ||
return | ||
} | ||
|
||
rp.reverseProxy.Transport = rp.currentTransport | ||
// when edge client(like kube-proxy, flannel, etc) use service account(default InClusterConfig) to access yurthub, | ||
// Authorization header will be set in request. and when edge client(like kubelet) use x509 certificate to access | ||
// yurthub, Authorization header in request will be empty. | ||
if isBearerRequest(req) { | ||
rp.reverseProxy.Transport = rp.bearerTransport | ||
} | ||
rp.reverseProxy.ServeHTTP(rw, req) | ||
} | ||
|
||
|
@@ -223,6 +219,18 @@ func (rp *RemoteProxy) errorHandler(rw http.ResponseWriter, req *http.Request, e | |
rw.WriteHeader(http.StatusBadGateway) | ||
} | ||
|
||
// RoundTrip is used to implement http.RoundTripper for RemoteProxy. | ||
func (rp *RemoteProxy) RoundTrip(req *http.Request) (*http.Response, error) { | ||
// when edge client(like kube-proxy, flannel, etc) use service account(default InClusterConfig) to access yurthub, | ||
// Authorization header will be set in request. and when edge client(like kubelet) use x509 certificate to access | ||
// yurthub, Authorization header in request will be empty. | ||
if isBearerRequest(req) { | ||
return rp.bearerTransport.RoundTrip(req) | ||
} | ||
|
||
return rp.currentTransport.RoundTrip(req) | ||
} | ||
|
||
func isBearerRequest(req *http.Request) bool { | ||
auth := strings.TrimSpace(req.Header.Get("Authorization")) | ||
if auth != "" { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The transport passed to NewUpgradeAwareHandler is assigned to UpgradeAwareHandler.Transport. It's used to handle non-upgrade request. So, it doesn't matter here. I think we should assign currentTransport and bearerTransport to UpgradeAwareHandler.UpgradeTransport.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Congrool From the code: https://github.com/kubernetes/apimachinery/blob/v0.18.8/pkg/util/proxy/upgradeaware.go#L398-L400
when UpgradeTransport is nil, UpgradeAwareHandler will use Transport to handle upgrade request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can group
upgradeAwareHandler
andbearerUpgradeAwareHandler
into one.upgradeAwareHandler
andbearerUpgradeAwareHandler
here are used to distinguish bearer and non-bearer requests and use different transport for them. TheRemoteProxy
is exactly what we need:I think we can remove
bearerUpgradeAwareHandler
and useproxyBackend
as the transport param inNewUpgradeAwareHandler
.The codes should look like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DrmagicE A good idea, i will modify it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DrmagicE I have checked the codes of
NewUpgradeAwareHandler
, and only*http.Transport
ashttp. RoundTripper
is supported, so use proxyBackend ashttp. RoundTripper
can not work.maybe we need to leave the current changed unchanged.
the code link is here: https://github.com/kubernetes/apimachinery/blob/master/pkg/util/net/http.go#L215-L239
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rambohe-ch Oh, what a pity. I got it.