mirror of
https://github.com/openfaas/faas.git
synced 2025-06-21 00:06:38 +00:00
Publish to multiple topics
Enables publishing to various topics according to annotations on the functions. The function cache is moved up one level so that it can be shared between the scale from zero code and the queue proxy. Unit tests added for new internal methods. Tested e2e with arkade and the newest queue-worker and RC gateway image with two queues and an annotation on one of the functions of com.openfaas.queue. It worked as expected including with multiple namespace support. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
This commit is contained in:
committed by
Alex Ellis
parent
a7c6c39200
commit
2bfca6d848
@ -1,20 +0,0 @@
|
||||
// Copyright (c) OpenFaaS Author(s). All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/openfaas/faas-provider/auth"
|
||||
)
|
||||
|
||||
type BasicAuthInjector struct {
|
||||
Credentials *auth.BasicAuthCredentials
|
||||
}
|
||||
|
||||
func (b BasicAuthInjector) Inject(r *http.Request) {
|
||||
if r != nil && b.Credentials != nil {
|
||||
r.SetBasicAuth(b.Credentials.User, b.Credentials.Password)
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
// Copyright (c) OpenFaaS Author(s). All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_Inject_WithNilRequestAndNilCredentials(t *testing.T) {
|
||||
injector := BasicAuthInjector{}
|
||||
injector.Inject(nil)
|
||||
}
|
||||
|
||||
func Test_Inject_WithRequestButNilCredentials(t *testing.T) {
|
||||
injector := BasicAuthInjector{}
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
injector.Inject(req)
|
||||
}
|
@ -14,6 +14,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/openfaas/faas/gateway/pkg/middleware"
|
||||
"github.com/openfaas/faas/gateway/types"
|
||||
)
|
||||
|
||||
@ -43,7 +44,7 @@ func MakeForwardingProxyHandler(proxy *types.HTTPClientReverseProxy,
|
||||
notifiers []HTTPNotifier,
|
||||
baseURLResolver BaseURLResolver,
|
||||
urlPathTransformer URLPathTransformer,
|
||||
serviceAuthInjector AuthInjector) http.HandlerFunc {
|
||||
serviceAuthInjector middleware.AuthInjector) http.HandlerFunc {
|
||||
|
||||
writeRequestURI := false
|
||||
if _, exists := os.LookupEnv("write_request_uri"); exists {
|
||||
@ -108,7 +109,7 @@ func forwardRequest(w http.ResponseWriter,
|
||||
requestURL string,
|
||||
timeout time.Duration,
|
||||
writeRequestURI bool,
|
||||
serviceAuthInjector AuthInjector) (int, error) {
|
||||
serviceAuthInjector middleware.AuthInjector) (int, error) {
|
||||
|
||||
upstreamReq := buildUpstreamRequest(r, baseURL, requestURL)
|
||||
if upstreamReq.Body != nil {
|
||||
|
@ -6,16 +6,19 @@ package handlers
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/openfaas/faas/gateway/metrics"
|
||||
"github.com/openfaas/faas/gateway/queue"
|
||||
"github.com/openfaas/faas/gateway/scaling"
|
||||
)
|
||||
|
||||
// MakeQueuedProxy accepts work onto a queue
|
||||
func MakeQueuedProxy(metrics metrics.MetricOptions, wildcard bool, canQueueRequests queue.CanQueueRequests, pathTransformer URLPathTransformer) http.HandlerFunc {
|
||||
func MakeQueuedProxy(metrics metrics.MetricOptions, wildcard bool, queuer queue.RequestQueuer, pathTransformer URLPathTransformer, defaultNS string, functionCacher scaling.FunctionCacher, serviceQuery scaling.ServiceQuery) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Body != nil {
|
||||
defer r.Body.Close()
|
||||
@ -24,29 +27,20 @@ func MakeQueuedProxy(metrics metrics.MetricOptions, wildcard bool, canQueueReque
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write([]byte(err.Error()))
|
||||
callbackURL, err := getCallbackURLHeader(r.Header)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
vars := mux.Vars(r)
|
||||
name := vars["name"]
|
||||
|
||||
callbackURLHeader := r.Header.Get("X-Callback-Url")
|
||||
var callbackURL *url.URL
|
||||
|
||||
if len(callbackURLHeader) > 0 {
|
||||
urlVal, urlErr := url.Parse(callbackURLHeader)
|
||||
if urlErr != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
|
||||
w.Write([]byte(urlErr.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
callbackURL = urlVal
|
||||
}
|
||||
queueName, err := getQueueName(name, functionCacher, serviceQuery)
|
||||
|
||||
req := &queue.Request{
|
||||
Function: name,
|
||||
@ -57,15 +51,69 @@ func MakeQueuedProxy(metrics metrics.MetricOptions, wildcard bool, canQueueReque
|
||||
Header: r.Header,
|
||||
Host: r.Host,
|
||||
CallbackURL: callbackURL,
|
||||
QueueName: queueName,
|
||||
}
|
||||
|
||||
if err = canQueueRequests.Queue(req); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
fmt.Println(err)
|
||||
if len(queueName) > 0 {
|
||||
log.Printf("Queueing %s to: %s\n", name, queueName)
|
||||
}
|
||||
|
||||
if err = queuer.Queue(req); err != nil {
|
||||
fmt.Printf("Queue error: %v\n", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
}
|
||||
|
||||
func getQueueName(name string, cache scaling.FunctionCacher, serviceQuery scaling.ServiceQuery) (queueName string, err error) {
|
||||
fn, ns := getNameParts(name)
|
||||
|
||||
query, hit := cache.Get(fn, ns)
|
||||
if !hit {
|
||||
queryResponse, err := serviceQuery.GetReplicas(fn, ns)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
cache.Set(fn, ns, queryResponse)
|
||||
}
|
||||
|
||||
query, _ = cache.Get(fn, ns)
|
||||
|
||||
queueName = ""
|
||||
if query.Annotations != nil {
|
||||
if v := (*query.Annotations)["com.openfaas.queue"]; len(v) > 0 {
|
||||
queueName = v
|
||||
}
|
||||
}
|
||||
return queueName, err
|
||||
}
|
||||
|
||||
func getCallbackURLHeader(header http.Header) (*url.URL, error) {
|
||||
value := header.Get("X-Callback-Url")
|
||||
var callbackURL *url.URL
|
||||
|
||||
if len(value) > 0 {
|
||||
urlVal, err := url.Parse(value)
|
||||
if err != nil {
|
||||
return callbackURL, err
|
||||
}
|
||||
|
||||
callbackURL = urlVal
|
||||
}
|
||||
|
||||
return callbackURL, nil
|
||||
}
|
||||
|
||||
func getNameParts(name string) (fn, ns string) {
|
||||
fn = name
|
||||
ns = ""
|
||||
|
||||
if index := strings.LastIndex(name, "."); index > 0 {
|
||||
fn = name[:index]
|
||||
ns = name[index+1:]
|
||||
}
|
||||
return fn, ns
|
||||
}
|
||||
|
74
gateway/handlers/queue_proxy_test.go
Normal file
74
gateway/handlers/queue_proxy_test.go
Normal file
@ -0,0 +1,74 @@
|
||||
// Copyright (c) Alex Ellis 2017. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_getNameParts(t *testing.T) {
|
||||
fn, ns := getNameParts("figlet.openfaas-fn")
|
||||
wantFn := "figlet"
|
||||
wantNs := "openfaas-fn"
|
||||
|
||||
if fn != wantFn {
|
||||
t.Fatalf("want %s, got %s", wantFn, fn)
|
||||
}
|
||||
if ns != wantNs {
|
||||
t.Fatalf("want %s, got %s", wantNs, ns)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_getNamePartsDualDot(t *testing.T) {
|
||||
fn, ns := getNameParts("dev.figlet.openfaas-fn")
|
||||
wantFn := "dev.figlet"
|
||||
wantNs := "openfaas-fn"
|
||||
|
||||
if fn != wantFn {
|
||||
t.Fatalf("want %s, got %s", wantFn, fn)
|
||||
}
|
||||
if ns != wantNs {
|
||||
t.Fatalf("want %s, got %s", wantNs, ns)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_getNameParts_NoNs(t *testing.T) {
|
||||
fn, ns := getNameParts("figlet")
|
||||
wantFn := "figlet"
|
||||
wantNs := ""
|
||||
|
||||
if fn != wantFn {
|
||||
t.Fatalf("want %s, got %s", wantFn, fn)
|
||||
}
|
||||
if ns != wantNs {
|
||||
t.Fatalf("want %s, got %s", wantNs, ns)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_getCallbackURLHeader(t *testing.T) {
|
||||
want := "http://localhost:8080"
|
||||
header := http.Header{}
|
||||
header.Add("X-Callback-Url", want)
|
||||
|
||||
uri, err := getCallbackURLHeader(header)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if uri.String() != want {
|
||||
t.Fatalf("want %s, but got %s", want, uri.String())
|
||||
}
|
||||
}
|
||||
|
||||
func Test_getCallbackURLHeader_ParseFails(t *testing.T) {
|
||||
want := "ht tp://foo.com"
|
||||
header := http.Header{}
|
||||
header.Add("X-Callback-Url", want)
|
||||
|
||||
_, err := getCallbackURLHeader(header)
|
||||
if err == nil {
|
||||
t.Fatal("wanted a parsing error.")
|
||||
}
|
||||
}
|
@ -24,9 +24,7 @@ func getNamespace(defaultNamespace, fullName string) (string, string) {
|
||||
// be called. If the function is not ready after the configured
|
||||
// amount of attempts / queries then next will not be invoked and a status
|
||||
// will be returned to the client.
|
||||
func MakeScalingHandler(next http.HandlerFunc, config scaling.ScalingConfig, defaultNamespace string) http.HandlerFunc {
|
||||
|
||||
scaler := scaling.NewFunctionScaler(config)
|
||||
func MakeScalingHandler(next http.HandlerFunc, scaler scaling.FunctionScaler, config scaling.ScalingConfig, defaultNamespace string) http.HandlerFunc {
|
||||
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
|
@ -1,7 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import "net/http"
|
||||
|
||||
type AuthInjector interface {
|
||||
Inject(r *http.Request)
|
||||
}
|
Reference in New Issue
Block a user