Set full URL path to request header (#1266)
This commit is contained in:
@@ -27,13 +27,9 @@ import (
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/fission/fission/pkg/types"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/satori/go.uuid"
|
||||
"go.opencensus.io/plugin/ochttp"
|
||||
"go.uber.org/zap"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -45,6 +41,7 @@ import (
|
||||
executorClient "github.com/fission/fission/pkg/executor/client"
|
||||
"github.com/fission/fission/pkg/redis"
|
||||
"github.com/fission/fission/pkg/throttler"
|
||||
"github.com/fission/fission/pkg/types"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -290,6 +287,8 @@ func (roundTripper RetryingRoundTripper) RoundTrip(req *http.Request) (*http.Res
|
||||
|
||||
overhead := time.Since(startTime)
|
||||
|
||||
roundTripper.logger.Debug("request headers", zap.Any("headers", req.Header))
|
||||
|
||||
// forward the request to the function service
|
||||
resp, err = ocRoundTripper.RoundTrip(req)
|
||||
if err == nil {
|
||||
@@ -407,25 +406,13 @@ func (fh *functionHandler) tapService(serviceUrl *url.URL) {
|
||||
}
|
||||
|
||||
func (fh functionHandler) handler(responseWriter http.ResponseWriter, request *http.Request) {
|
||||
// retrieve url params and add them to request header
|
||||
vars := mux.Vars(request)
|
||||
for k, v := range vars {
|
||||
request.Header.Set(fmt.Sprintf("X-Fission-Params-%v", k), v)
|
||||
}
|
||||
|
||||
var reqUID string
|
||||
if len(fh.recorderName) > 0 {
|
||||
UID := strings.ToLower(uuid.NewV4().String())
|
||||
reqUID = "REQ" + UID
|
||||
request.Header.Set("X-Fission-ReqUID", reqUID)
|
||||
fh.logger.Debug("record request", zap.String("request_id", reqUID))
|
||||
}
|
||||
|
||||
if fh.httpTrigger != nil && fh.httpTrigger.Spec.FunctionReference.Type == types.FunctionReferenceTypeFunctionWeights {
|
||||
// canary deployment. need to determine the function to send request to now
|
||||
fnMetadata := getCanaryBackend(fh.functionMetadataMap, fh.fnWeightDistributionList)
|
||||
if fnMetadata == nil {
|
||||
fh.logger.Error("could not get canary backend", zap.String("request_id", reqUID))
|
||||
fh.logger.Error("could not get canary backend",
|
||||
zap.Any("metadataMap", fh.functionMetadataMap),
|
||||
zap.Any("distributionList", fh.fnWeightDistributionList))
|
||||
// TODO : write error to responseWrite and return response
|
||||
return
|
||||
}
|
||||
@@ -433,8 +420,14 @@ func (fh functionHandler) handler(responseWriter http.ResponseWriter, request *h
|
||||
fh.logger.Debug("chosen function backend's metadata", zap.Any("metadata", fh.function))
|
||||
}
|
||||
|
||||
// set record id
|
||||
setRecordRequestIDHeader(fh.recorderName, request)
|
||||
|
||||
// url path
|
||||
setPathInfoToHeader(request)
|
||||
|
||||
// system params
|
||||
MetadataToHeaders(HEADERS_FISSION_FUNCTION_PREFIX, fh.function, request)
|
||||
setFunctionMetadataToHeader(fh.function, request)
|
||||
|
||||
director := func(req *http.Request) {
|
||||
if _, ok := req.Header["User-Agent"]; !ok {
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
Copyright 2019 The Fission Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package router
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
HEADERS_FISSION_FUNCTION_PREFIX = "Fission-Function"
|
||||
)
|
||||
|
||||
// setFunctionMetadataToHeaders set function metadatas to request header
|
||||
func setFunctionMetadataToHeader(meta *metav1.ObjectMeta, request *http.Request) {
|
||||
request.Header.Set(fmt.Sprintf("X-%s-Uid", HEADERS_FISSION_FUNCTION_PREFIX), string(meta.UID))
|
||||
request.Header.Set(fmt.Sprintf("X-%s-Name", HEADERS_FISSION_FUNCTION_PREFIX), meta.Name)
|
||||
request.Header.Set(fmt.Sprintf("X-%s-Namespace", HEADERS_FISSION_FUNCTION_PREFIX), meta.Namespace)
|
||||
request.Header.Set(fmt.Sprintf("X-%s-ResourceVersion", HEADERS_FISSION_FUNCTION_PREFIX), meta.ResourceVersion)
|
||||
}
|
||||
|
||||
// setPathInfoToHeaders set URL path params and full URL path to request header
|
||||
func setPathInfoToHeader(request *http.Request) {
|
||||
// retrieve url params and add them to request header
|
||||
vars := mux.Vars(request)
|
||||
for k, v := range vars {
|
||||
request.Header.Set(fmt.Sprintf("X-Fission-Params-%v", k), v)
|
||||
}
|
||||
request.Header.Set("X-Fission-Full-Url", request.URL.String())
|
||||
}
|
||||
|
||||
// setRecordRequestIDHeader set record ID to request header
|
||||
func setRecordRequestIDHeader(recorderName string, request *http.Request) {
|
||||
if len(recorderName) > 0 {
|
||||
reqUID := "REQ" + strings.ToLower(uuid.NewV4().String())
|
||||
request.Header.Set("X-Fission-ReqUID", reqUID)
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
)
|
||||
|
||||
const (
|
||||
HEADERS_FISSION_FUNCTION_PREFIX = "Fission-Function"
|
||||
)
|
||||
|
||||
func MetadataToHeaders(prefix string, meta *metav1.ObjectMeta, request *http.Request) {
|
||||
request.Header.Set(fmt.Sprintf("X-%s-Uid", prefix), string(meta.UID))
|
||||
request.Header.Set(fmt.Sprintf("X-%s-Name", prefix), meta.Name)
|
||||
request.Header.Set(fmt.Sprintf("X-%s-Namespace", prefix), meta.Namespace)
|
||||
request.Header.Set(fmt.Sprintf("X-%s-ResourceVersion", prefix), meta.ResourceVersion)
|
||||
}
|
||||
|
||||
func HeadersToMetadata(prefix string, headers http.Header) *metav1.ObjectMeta {
|
||||
return &metav1.ObjectMeta{
|
||||
Name: headers.Get(fmt.Sprintf("X-%s-Name", prefix)),
|
||||
UID: types.UID(headers.Get(fmt.Sprintf("X-%s-Uid", prefix))),
|
||||
Namespace: headers.Get(fmt.Sprintf("X-%s-Namespace", prefix)),
|
||||
ResourceVersion: headers.Get(fmt.Sprintf("X-%s-ResourceVersion", prefix)),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user