Skip to content

Commit

Permalink
fix watch request
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancandevloper authored and zhujf1989 committed Jul 26, 2023
1 parent e938b01 commit 8cebc09
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions pkg/apiserver/cubeapi/resourcemanage/handle/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"strings"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -291,11 +292,13 @@ func (h *ProxyHandler) ProxyHandle(c *gin.Context) {
Condition: condition,
ConverterContext: &converterContext,
}
requestProxy := &httputil.ReverseProxy{Director: director, Transport: transport, ModifyResponse: filter.filterResponse, ErrorHandler: errorHandler}

needModifyResponse := needModifyResponse(proxyUrl, c)
// trim auth token here
c.Request.Header.Del(constants.AuthorizationHeader)

requestProxy := &httputil.ReverseProxy{Director: director, Transport: transport, ModifyResponse: nil, ErrorHandler: errorHandler}
if needModifyResponse {
requestProxy.ModifyResponse = filter.filterResponse
}
requestProxy.ServeHTTP(c.Writer, c.Request)
}

Expand Down Expand Up @@ -327,3 +330,36 @@ func parseQueryParams(c *gin.Context) *filter.Condition {
}
return &condition
}

// if the request has a watch, it should not be filtered
func needModifyResponse(proxyUrl string, c *gin.Context) bool {
modifyResponse := true
// According to the rules of k8s, determine whether the request has a watch.
// When there is a watch, it is a long connection request.
// At this time, the returned data should not be filtered
// watch is divided into two cases, one is watch=true in the query parameter, and the other is watch in the path, such as /api/v1/watch/namespaces/default/pods
// 1. watch=true in the query parameter
watch := c.Query("watch")
isWatch, _ := strconv.ParseBool(watch)
if isWatch {
modifyResponse = false
}
// 2. watch in the path
if len(proxyUrl) > 0 {
path := proxyUrl
if strings.HasPrefix(path, "/") {
path = path[1:]
}
split := strings.Split(path, "/")
if split[0] == "api" {
if split[2] == "watch" {
modifyResponse = false
}
} else if split[0] == "apis" {
if split[3] == "watch" {
modifyResponse = false
}
}
}
return modifyResponse
}

0 comments on commit 8cebc09

Please sign in to comment.