Skip to content

Commit

Permalink
bugfix: do not send pool-scoped resource list/watch request to pool-c…
Browse files Browse the repository at this point in the history
…oordinator (#1134)

Signed-off-by: Congrool <chpzhangyifei@zju.edu.cn>

Signed-off-by: Congrool <chpzhangyifei@zju.edu.cn>
  • Loading branch information
Congrool authored Jan 11, 2023
1 parent a5cd6e3 commit f77c971
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
10 changes: 10 additions & 0 deletions pkg/yurthub/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/openyurtio/openyurt/pkg/yurthub/cachemanager"
"github.com/openyurtio/openyurt/pkg/yurthub/healthchecker"
"github.com/openyurtio/openyurt/pkg/yurthub/poolcoordinator"
coordinatorconstants "github.com/openyurtio/openyurt/pkg/yurthub/poolcoordinator/constants"
"github.com/openyurtio/openyurt/pkg/yurthub/proxy/local"
"github.com/openyurtio/openyurt/pkg/yurthub/proxy/pool"
"github.com/openyurtio/openyurt/pkg/yurthub/proxy/remote"
Expand Down Expand Up @@ -217,6 +218,15 @@ func (p *yurtReverseProxy) eventHandler(rw http.ResponseWriter, req *http.Reques
}

func (p *yurtReverseProxy) poolScopedResouceHandler(rw http.ResponseWriter, req *http.Request) {
agent, ok := hubutil.ClientComponentFrom(req.Context())
if ok && agent == coordinatorconstants.DefaultPoolScopedUserAgent {
// list/watch request from leader-yurthub
// It should always be proxied to cloud APIServer to get latest resource, which will
// be cached into pool cache.
p.loadBalancer.ServeHTTP(rw, req)
return
}

if p.isCoordinatorReady() && p.poolProxy != nil {
p.poolProxy.ServeHTTP(rw, req)
} else if p.cloudHealthChecker.IsHealthy() {
Expand Down
14 changes: 13 additions & 1 deletion pkg/yurthub/proxy/remote/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ func (lb *loadBalancer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
return
}
klog.V(3).Infof("picked backend %s by %s for request %s", rp.Name(), lb.algo.Name(), hubutil.ReqString(req))
if util.IsPoolScopedResouceListWatchRequest(req) {
// If pool-scoped resource request is from leader-yurthub, it should always be sent to the cloud APIServer.
// Thus we do not need to start a check routine for it. But for other requests, we need to periodically check
// the pool-coordinator status, and switch the traffic to pool-coordinator if it is ready.
if util.IsPoolScopedResouceListWatchRequest(req) && !isRequestFromLeaderYurthub(req) {
// We get here possibly because the pool-coordinator is not ready.
// We should cancel the watch request when pool-coordinator becomes ready.
klog.Infof("pool-coordinator is not ready, we use cloud APIServer to temporarily handle the req: %s", hubutil.ReqString(req))
Expand Down Expand Up @@ -420,3 +423,12 @@ func isRequestOfNodeAndPod(reqCtx context.Context) bool {
return (reqInfo.Resource == "nodes" && reqInfo.APIGroup == "" && reqInfo.APIVersion == "v1") ||
(reqInfo.Resource == "pods" && reqInfo.APIGroup == "" && reqInfo.APIVersion == "v1")
}

func isRequestFromLeaderYurthub(req *http.Request) bool {
ctx := req.Context()
agent, ok := hubutil.ClientComponentFrom(ctx)
if !ok {
return false
}
return agent == coordinatorconstants.DefaultPoolScopedUserAgent
}

0 comments on commit f77c971

Please sign in to comment.