Option to keep or remove prefix when router triggers prefix based function (#2133)

Signed-off-by: Sanket Sudake <sanketsudake@gmail.com>
This commit is contained in:
Sanket Sudake
2021-07-29 18:50:26 +05:30
committed by GitHub
parent 2aaaeee5a7
commit 2292f472c9
9 changed files with 33 additions and 3 deletions
+9 -1
View File
@@ -266,13 +266,17 @@ func (roundTripper *RetryingRoundTripper) RoundTrip(req *http.Request) (*http.Re
// req.URL.Path according to httpTrigger specification.
prefixTrim := ""
functionURL := utils.UrlForFunction(fnMeta.Name, fnMeta.Namespace)
keepPrefix := false
if roundTripper.funcHandler.httpTrigger != nil && roundTripper.funcHandler.httpTrigger.Spec.Prefix != nil && *roundTripper.funcHandler.httpTrigger.Spec.Prefix != "" {
prefixTrim = *roundTripper.funcHandler.httpTrigger.Spec.Prefix
keepPrefix = roundTripper.funcHandler.httpTrigger.Spec.KeepPrefix
} else if strings.HasPrefix(req.URL.Path, functionURL) {
prefixTrim = functionURL
}
if prefixTrim != "" {
req.URL.Path = strings.TrimPrefix(req.URL.Path, prefixTrim)
if !keepPrefix {
req.URL.Path = strings.TrimPrefix(req.URL.Path, prefixTrim)
}
if !strings.HasPrefix(req.URL.Path, "/") {
req.URL.Path = "/" + req.URL.Path
}
@@ -280,6 +284,10 @@ func (roundTripper *RetryingRoundTripper) RoundTrip(req *http.Request) (*http.Re
req.URL.Path = "/"
}
logger.Debug("function invoke url",
zap.String("prefixTrim", prefixTrim),
zap.Bool("keepPrefix", keepPrefix),
zap.String("hitURL", req.URL.Path))
// Overwrite request host with internal host,
// or request will be blocked in some situations
// (e.g. istio-proxy)