From 2292f472c9a259273780f3623013945a7722e866 Mon Sep 17 00:00:00 2001 From: Sanket Sudake Date: Thu, 29 Jul 2021 18:50:26 +0530 Subject: [PATCH] Option to keep or remove prefix when router triggers prefix based function (#2133) Signed-off-by: Sanket Sudake --- crds/v1/fission.io_httptriggers.yaml | 3 +++ pkg/apis/core/v1/types.go | 5 +++++ pkg/apis/core/v1/zz_generated.swagger_doc_generated.go | 1 + pkg/fission-cli/cmd/httptrigger/command.go | 6 ++++-- pkg/fission-cli/cmd/httptrigger/create.go | 5 +++++ pkg/fission-cli/cmd/httptrigger/update.go | 4 ++++ pkg/fission-cli/flag/flag.go | 1 + pkg/fission-cli/flag/key/key.go | 1 + pkg/router/functionHandler.go | 10 +++++++++- 9 files changed, 33 insertions(+), 3 deletions(-) diff --git a/crds/v1/fission.io_httptriggers.yaml b/crds/v1/fission.io_httptriggers.yaml index 7b51c986..9d8adb9a 100644 --- a/crds/v1/fission.io_httptriggers.yaml +++ b/crds/v1/fission.io_httptriggers.yaml @@ -76,6 +76,9 @@ spec: description: TLS is for user to specify a Secret that contains TLS key and certificate. The domain name in the key and crt must match the value of Host field. type: string type: object + keepPrefix: + description: When function is exposed with Prefix based path, keepPrefix decides whether to keep or trim prefix in URL while invoking function. + type: boolean method: description: Use Methods instead of Method. This field is going to be deprecated in a future release HTTP method to access a function. type: string diff --git a/pkg/apis/core/v1/types.go b/pkg/apis/core/v1/types.go index b0db4418..4d32225c 100644 --- a/pkg/apis/core/v1/types.go +++ b/pkg/apis/core/v1/types.go @@ -664,6 +664,11 @@ type ( // +optional Prefix *string `json:"prefix,omitempty"` + // When function is exposed with Prefix based path, + // keepPrefix decides whether to keep or trim prefix in URL while invoking function. + // +optional + KeepPrefix bool `json:"keepPrefix,omitempty"` + // Use Methods instead of Method. This field is going to be deprecated in a future release // HTTP method to access a function. // +optional diff --git a/pkg/apis/core/v1/zz_generated.swagger_doc_generated.go b/pkg/apis/core/v1/zz_generated.swagger_doc_generated.go index 27e85bab..808a410a 100644 --- a/pkg/apis/core/v1/zz_generated.swagger_doc_generated.go +++ b/pkg/apis/core/v1/zz_generated.swagger_doc_generated.go @@ -236,6 +236,7 @@ var map_HTTPTriggerSpec = map[string]string{ "host": "Deprecated: the original idea of this field is not for setting Ingress. Since we have IngressConfig now, remove Host after couple releases.", "relativeurl": "RelativeURL is the exposed URL for external client to access a function with.", "prefix": "Prefix with which functions are exposed. NOTE: Prefix takes precedence over URL/RelativeURL. Note that it does not treat slashes specially (\"/foobar/\" will be matched by the prefix \"/foobar\").", + "keepPrefix": "When function is exposed with Prefix based path, keepPrefix decides whether to keep or trim prefix in URL while invoking function.", "method": "Use Methods instead of Method. This field is going to be deprecated in a future release HTTP method to access a function.", "methods": "HTTP methods to access a function", "functionref": "FunctionReference is a reference to the target function.", diff --git a/pkg/fission-cli/cmd/httptrigger/command.go b/pkg/fission-cli/cmd/httptrigger/command.go index c98977e3..006d6a29 100644 --- a/pkg/fission-cli/cmd/httptrigger/command.go +++ b/pkg/fission-cli/cmd/httptrigger/command.go @@ -33,7 +33,8 @@ func Commands() *cobra.Command { Required: []flag.Flag{flag.HtFnName}, Optional: []flag.Flag{flag.HtUrl, flag.HtName, flag.HtMethod, flag.HtIngress, flag.HtIngressRule, flag.HtIngressAnnotation, flag.HtIngressTLS, - flag.HtFnWeight, flag.HtHost, flag.NamespaceFunction, flag.SpecSave, flag.SpecDry, flag.HtPrefix}, + flag.HtFnWeight, flag.HtHost, flag.NamespaceFunction, flag.SpecSave, flag.SpecDry, + flag.HtPrefix, flag.HtKeepPrefix}, }) getCmd := &cobra.Command{ @@ -56,7 +57,8 @@ func Commands() *cobra.Command { Required: []flag.Flag{flag.HtName}, Optional: []flag.Flag{flag.HtUrl, flag.HtFnName, flag.HtMethod, flag.HtIngress, flag.HtIngressRule, flag.HtIngressAnnotation, - flag.HtIngressTLS, flag.HtFnWeight, flag.HtHost, flag.NamespaceTrigger, flag.HtPrefix}, + flag.HtIngressTLS, flag.HtFnWeight, flag.HtHost, flag.NamespaceTrigger, + flag.HtPrefix, flag.HtKeepPrefix}, }) deleteCmd := &cobra.Command{ diff --git a/pkg/fission-cli/cmd/httptrigger/create.go b/pkg/fission-cli/cmd/httptrigger/create.go index 4b0b07bb..d274c1ba 100644 --- a/pkg/fission-cli/cmd/httptrigger/create.go +++ b/pkg/fission-cli/cmd/httptrigger/create.go @@ -179,9 +179,14 @@ func (opts *CreateSubCommand) complete(input cli.Input) error { CreateIngress: createIngress, IngressConfig: *ingressConfig, Prefix: &prefix, + KeepPrefix: input.Bool(flagkey.HtKeepPrefix), }, } + if input.IsSet(flagkey.HtKeepPrefix) { + opts.trigger.Spec.KeepPrefix = input.Bool(flagkey.HtKeepPrefix) + } + return nil } diff --git a/pkg/fission-cli/cmd/httptrigger/update.go b/pkg/fission-cli/cmd/httptrigger/update.go index d8009753..81671ee7 100644 --- a/pkg/fission-cli/cmd/httptrigger/update.go +++ b/pkg/fission-cli/cmd/httptrigger/update.go @@ -80,6 +80,10 @@ func (opts *UpdateSubCommand) complete(input cli.Input) error { ht.Spec.RelativeURL = triggerUrl ht.Spec.Prefix = &prefix + if input.IsSet(flagkey.HtKeepPrefix) { + ht.Spec.KeepPrefix = input.Bool(flagkey.HtKeepPrefix) + } + methods := input.StringSlice(flagkey.HtMethod) if len(methods) > 0 { for _, method := range methods { diff --git a/pkg/fission-cli/flag/flag.go b/pkg/fission-cli/flag/flag.go index 0237553c..30db02e4 100644 --- a/pkg/fission-cli/flag/flag.go +++ b/pkg/fission-cli/flag/flag.go @@ -134,6 +134,7 @@ var ( HtFnWeight = Flag{Type: IntSlice, Name: flagkey.HtFnWeight, Usage: "Weight for each function supplied with --function flag, in the same order. Used for canary deployment"} HtFnFilter = Flag{Type: String, Name: flagkey.HtFilter, Usage: "Name of the function for trigger(s)"} HtPrefix = Flag{Type: String, Name: flagkey.HtPrefix, Usage: "Prefix with which functions are exposed. NOTE: Prefix takes precedence over URL/RelativeURL"} + HtKeepPrefix = Flag{Type: Bool, Name: flagkey.HtKeepPrefix, Usage: "Keep the prefix in the URL while forwarding request to the function"} TtName = Flag{Type: String, Name: flagkey.TtName, Usage: "Time Trigger name"} TtCron = Flag{Type: String, Name: flagkey.TtCron, Usage: "Time trigger cron spec with each asterisk representing respectively second, minute, hour, the day of the month, month and day of the week. Also supports readable formats like '@every 5m', '@hourly'"} diff --git a/pkg/fission-cli/flag/key/key.go b/pkg/fission-cli/flag/key/key.go index 11b34640..d4b3541e 100644 --- a/pkg/fission-cli/flag/key/key.go +++ b/pkg/fission-cli/flag/key/key.go @@ -87,6 +87,7 @@ const ( HtFnWeight = "weight" HtFilter = HtFnName HtPrefix = "prefix" + HtKeepPrefix = "keepprefix" TtName = resourceName TtCron = "cron" diff --git a/pkg/router/functionHandler.go b/pkg/router/functionHandler.go index 4210a9f9..efffa700 100644 --- a/pkg/router/functionHandler.go +++ b/pkg/router/functionHandler.go @@ -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)