Enable prefix based routing (#2047)
* Enable prefix based routing Signed-off-by: Harsh Thakur <harshthakur9030@gmail.com> * Optimize checking condition Signed-off-by: Sanket Sudake <sanketsudake@gmail.com> * Disable few tests * disable router modification for now * run code generator * Collect fission dump in CI * Enable all tests back Signed-off-by: Sanket Sudake <sanketsudake@gmail.com> * Remove unwanted code Signed-off-by: Sanket Sudake <sanketsudake@gmail.com> * Add prefix support at more places and couple of todo's Signed-off-by: Sanket Sudake <sanketsudake@gmail.com> * Few more changes Signed-off-by: Sanket Sudake <sanketsudake@gmail.com> * Improve function trim support * Support for prefix based urls in fission function test Signed-off-by: Sanket Sudake <sanketsudake@gmail.com> * multi route for fission function test Signed-off-by: Sanket Sudake <sanketsudake@gmail.com> * Improve validations in trigger creations * Adjust leading / in url from fission Signed-off-by: Sanket Sudake <sanketsudake@gmail.com> * Change suburl to subpath for function test Signed-off-by: Sanket Sudake <sanketsudake@gmail.com> Co-authored-by: Sanket Sudake <sanketsudake@gmail.com>
This commit is contained in:
co-authored by
Sanket Sudake
parent
f01de5c802
commit
aa45703a99
@@ -43,7 +43,7 @@ func Commands() *cobra.Command {
|
||||
flag.PkgSrcChecksum, flag.PkgDeployChecksum, flag.PkgInsecure,
|
||||
flag.FnBuildCmd,
|
||||
|
||||
flag.HtUrl, flag.HtMethod,
|
||||
flag.HtUrl, flag.HtPrefix, flag.HtMethod,
|
||||
|
||||
// flag for newdeploy to use.
|
||||
flag.RunTimeMinCPU, flag.RunTimeMaxCPU, flag.RunTimeMinMemory,
|
||||
@@ -150,6 +150,7 @@ func Commands() *cobra.Command {
|
||||
// for getting log from log database if
|
||||
// we failed to get logs from function pod.
|
||||
flag.FnLogDBType,
|
||||
flag.FnSubPath,
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@ package function
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
@@ -339,13 +338,13 @@ func (opts *CreateSubCommand) run(input cli.Input) error {
|
||||
|
||||
// Allow the user to specify an HTTP trigger while creating a function.
|
||||
triggerUrl := input.String(flagkey.HtUrl)
|
||||
if len(triggerUrl) == 0 {
|
||||
prefix := input.String(flagkey.HtPrefix)
|
||||
if len(triggerUrl) == 0 && len(prefix) == 0 {
|
||||
return nil
|
||||
}
|
||||
if !strings.HasPrefix(triggerUrl, "/") {
|
||||
triggerUrl = fmt.Sprintf("/%s", triggerUrl)
|
||||
if len(prefix) != 0 && len(triggerUrl) > 0 {
|
||||
console.Warn("Prefix will take precedence over URL/RelativeURL")
|
||||
}
|
||||
|
||||
method, err := httptrigger.GetMethod(input.String(flagkey.HtMethod))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error getting HTTP trigger method")
|
||||
@@ -359,6 +358,7 @@ func (opts *CreateSubCommand) run(input cli.Input) error {
|
||||
},
|
||||
Spec: fv1.HTTPTriggerSpec{
|
||||
RelativeURL: triggerUrl,
|
||||
Prefix: &prefix,
|
||||
Method: method,
|
||||
FunctionReference: fv1.FunctionReference{
|
||||
Type: fv1.FunctionReferenceTypeFunctionName,
|
||||
|
||||
@@ -18,7 +18,6 @@ package function
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -62,14 +61,17 @@ func (opts *TestSubCommand) do(input cli.Input) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
routerURL = "127.0.0.1:" + localRouterPort
|
||||
|
||||
fnUri := m.Name
|
||||
if m.Namespace != metav1.NamespaceDefault {
|
||||
fnUri = fmt.Sprintf("%v/%v", m.Namespace, m.Name)
|
||||
fnURL := "http://127.0.0.1:" + localRouterPort + util.UrlForFunction(m.Name, m.Namespace)
|
||||
if input.IsSet(flagkey.FnSubPath) {
|
||||
subPath := input.String(flagkey.FnSubPath)
|
||||
if !strings.HasPrefix(subPath, "/") {
|
||||
fnURL = fnURL + "/" + subPath
|
||||
} else {
|
||||
fnURL = fnURL + subPath
|
||||
}
|
||||
}
|
||||
|
||||
functionUrl, err := url.Parse(fmt.Sprintf("http://%s/fission-function/%s", routerURL, fnUri))
|
||||
functionUrl, err := url.Parse(fnURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -30,10 +30,10 @@ func Commands() *cobra.Command {
|
||||
RunE: wrapper.Wrapper(Create),
|
||||
}
|
||||
wrapper.SetFlags(createCmd, flag.FlagSet{
|
||||
Required: []flag.Flag{flag.HtUrl, flag.HtFnName},
|
||||
Optional: []flag.Flag{flag.HtName, flag.HtMethod, flag.HtIngress,
|
||||
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.HtFnWeight, flag.HtHost, flag.NamespaceFunction, flag.SpecSave, flag.SpecDry, flag.HtPrefix},
|
||||
})
|
||||
|
||||
getCmd := &cobra.Command{
|
||||
@@ -56,7 +56,7 @@ 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.HtIngressTLS, flag.HtFnWeight, flag.HtHost, flag.NamespaceTrigger, flag.HtPrefix},
|
||||
})
|
||||
|
||||
deleteCmd := &cobra.Command{
|
||||
|
||||
@@ -19,6 +19,7 @@ package httptrigger
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@@ -87,10 +88,25 @@ func (opts *CreateSubCommand) complete(input cli.Input) error {
|
||||
}
|
||||
|
||||
triggerUrl := input.String(flagkey.HtUrl)
|
||||
if triggerUrl == "/" {
|
||||
prefix := input.String(flagkey.HtPrefix)
|
||||
|
||||
if triggerUrl == "" && prefix == "" {
|
||||
console.Error("You need to supply either Prefix or URL/RelativeURL")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if triggerUrl != "" && prefix != "" {
|
||||
console.Warn("Prefix will take precedence over URL/RelativeURL")
|
||||
}
|
||||
|
||||
if triggerUrl == "/" || prefix == "/" {
|
||||
return errors.New("url with only root path is not allowed")
|
||||
} else if !strings.HasPrefix(triggerUrl, "/") {
|
||||
triggerUrl = fmt.Sprintf("/%s", triggerUrl)
|
||||
}
|
||||
if triggerUrl != "" && !strings.HasPrefix(triggerUrl, "/") {
|
||||
triggerUrl = "/" + triggerUrl
|
||||
}
|
||||
if prefix != "" && !strings.HasPrefix(prefix, "/") {
|
||||
prefix = "/" + prefix
|
||||
}
|
||||
|
||||
method, err := GetMethod(input.String(flagkey.HtMethod))
|
||||
@@ -149,6 +165,7 @@ func (opts *CreateSubCommand) complete(input cli.Input) error {
|
||||
FunctionReference: *functionRef,
|
||||
CreateIngress: createIngress,
|
||||
IngressConfig: *ingressConfig,
|
||||
Prefix: &prefix,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ package httptrigger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -59,10 +60,26 @@ func (opts *UpdateSubCommand) complete(input cli.Input) error {
|
||||
return errors.Wrap(err, "error getting HTTP trigger")
|
||||
}
|
||||
|
||||
if input.IsSet(flagkey.HtUrl) {
|
||||
ht.Spec.RelativeURL = input.String(flagkey.HtUrl)
|
||||
triggerUrl := input.String(flagkey.HtUrl)
|
||||
prefix := input.String(flagkey.HtPrefix)
|
||||
|
||||
if triggerUrl != "" && prefix != "" {
|
||||
console.Warn("Prefix will take precedence over URL/RelativeURL")
|
||||
}
|
||||
|
||||
if triggerUrl == "/" || prefix == "/" {
|
||||
return errors.New("url with only root path is not allowed")
|
||||
}
|
||||
if triggerUrl != "" && !strings.HasPrefix(triggerUrl, "/") {
|
||||
triggerUrl = "/" + triggerUrl
|
||||
}
|
||||
if prefix != "" && !strings.HasPrefix(prefix, "/") {
|
||||
prefix = "/" + prefix
|
||||
}
|
||||
|
||||
ht.Spec.RelativeURL = triggerUrl
|
||||
ht.Spec.Prefix = &prefix
|
||||
|
||||
if input.IsSet(flagkey.HtMethod) {
|
||||
ht.Spec.Method = input.String(flagkey.HtMethod)
|
||||
}
|
||||
@@ -98,9 +115,15 @@ func (opts *UpdateSubCommand) complete(input cli.Input) error {
|
||||
}
|
||||
|
||||
if input.IsSet(flagkey.HtIngressRule) || input.IsSet(flagkey.HtIngressAnnotation) || input.IsSet(flagkey.HtIngressTLS) {
|
||||
fallbackURL := ""
|
||||
if ht.Spec.Prefix != nil && *ht.Spec.Prefix != "" {
|
||||
fallbackURL = *ht.Spec.Prefix
|
||||
} else {
|
||||
fallbackURL = ht.Spec.RelativeURL
|
||||
}
|
||||
ingress, err := GetIngressConfig(
|
||||
input.StringSlice(flagkey.HtIngressAnnotation), input.String(flagkey.HtIngressRule),
|
||||
input.String(flagkey.HtIngressTLS), ht.Spec.RelativeURL, &ht.Spec.IngressConfig)
|
||||
input.String(flagkey.HtIngressTLS), fallbackURL, &ht.Spec.IngressConfig)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error parsing ingress configuration")
|
||||
}
|
||||
|
||||
@@ -317,10 +317,12 @@ func ShowHTTPTriggers(hts []fv1.HTTPTrigger) {
|
||||
host = trigger.Spec.IngressConfig.Host
|
||||
}
|
||||
path := trigger.Spec.RelativeURL
|
||||
if trigger.Spec.Prefix != nil && *trigger.Spec.Prefix != "" {
|
||||
path = *trigger.Spec.Prefix
|
||||
}
|
||||
if len(trigger.Spec.IngressConfig.Path) > 0 {
|
||||
path = trigger.Spec.IngressConfig.Path
|
||||
}
|
||||
|
||||
var msg []string
|
||||
for k, v := range trigger.Spec.IngressConfig.Annotations {
|
||||
msg = append(msg, fmt.Sprintf("%v: %v", k, v))
|
||||
|
||||
@@ -566,11 +566,6 @@ func (fr *FissionResources) ParseYaml(b []byte, loc *Location) error {
|
||||
return errors.Wrap(err, fmt.Sprintf("Failed to parse %v in %v", tm.Kind, loc))
|
||||
}
|
||||
|
||||
// TODO move to validator
|
||||
if !strings.HasPrefix(v.Spec.RelativeURL, "/") {
|
||||
v.Spec.RelativeURL = fmt.Sprintf("/%s", v.Spec.RelativeURL)
|
||||
}
|
||||
|
||||
m = &v.ObjectMeta
|
||||
fr.HttpTriggers = append(fr.HttpTriggers, v)
|
||||
case "KubernetesWatchTrigger":
|
||||
|
||||
@@ -113,6 +113,7 @@ var (
|
||||
FnConcurrency = Flag{Type: Int, Name: flagkey.FnConcurrency, Aliases: []string{"con"}, Usage: "Maximum number of pods specialized concurrently to serve requests", DefaultValue: 500}
|
||||
FnRequestsPerPod = Flag{Type: Int, Name: flagkey.FnRequestsPerPod, Aliases: []string{"rpp"}, Usage: "Maximum number of concurrent requests that can be served by a specialized pod", DefaultValue: 1}
|
||||
FnOnceOnly = Flag{Type: Bool, Name: flagkey.FnOnceOnly, Aliases: []string{"yolo"}, Usage: "Specifies if specialized pod will serve exactly one request in its lifetime"}
|
||||
FnSubPath = Flag{Type: String, Name: flagkey.FnSubPath, Usage: "Sub Path to check if function internally supports routing"}
|
||||
|
||||
HtName = Flag{Type: String, Name: flagkey.HtName, Usage: "HTTP trigger name"}
|
||||
HtMethod = Flag{Type: String, Name: flagkey.HtMethod, Usage: "HTTP Method: GET|POST|PUT|DELETE|HEAD", DefaultValue: http.MethodGet}
|
||||
@@ -125,6 +126,7 @@ var (
|
||||
HtFnName = Flag{Type: StringSlice, Name: flagkey.HtFnName, Usage: "Name(s) of the function for this trigger. (If 2 functions are supplied with this flag, traffic gets routed to them based on weights supplied with --weight flag.)"}
|
||||
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"}
|
||||
|
||||
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'"}
|
||||
|
||||
@@ -66,6 +66,7 @@ const (
|
||||
FnConcurrency = "concurrency"
|
||||
FnRequestsPerPod = "requestsperpod"
|
||||
FnOnceOnly = "onceonly"
|
||||
FnSubPath = "subpath"
|
||||
|
||||
HtName = resourceName
|
||||
HtMethod = "method"
|
||||
@@ -78,6 +79,7 @@ const (
|
||||
HtFnName = "function"
|
||||
HtFnWeight = "weight"
|
||||
HtFilter = HtFnName
|
||||
HtPrefix = "prefix"
|
||||
|
||||
TtName = resourceName
|
||||
TtCron = "cron"
|
||||
|
||||
@@ -342,3 +342,11 @@ func UpdateMapFromStringSlice(dataMap *map[string]string, params []string) bool
|
||||
}
|
||||
return updated
|
||||
}
|
||||
|
||||
func UrlForFunction(name, namespace string) string {
|
||||
prefix := "/fission-function"
|
||||
if namespace != metav1.NamespaceDefault {
|
||||
prefix = fmt.Sprintf("/fission-function/%s", namespace)
|
||||
}
|
||||
return fmt.Sprintf("%v/%v", prefix, name)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user