From ad5289f2ef8451a16549a5ee01fb2750f75e089d Mon Sep 17 00:00:00 2001 From: Rahul Bhati Date: Wed, 10 Mar 2021 14:52:23 +0530 Subject: [PATCH] Add function in payload instead of just metadata (#1919) Passing information of function from router to executor is more efficient than calling the K8S API. This change does that instead of passing only metadata and then executor calling the K8S API again. --- pkg/executor/api.go | 20 ++++---------------- pkg/executor/client/client.go | 4 ++-- pkg/executor/executor_test.go | 2 +- pkg/router/functionHandler.go | 2 +- 4 files changed, 8 insertions(+), 20 deletions(-) diff --git a/pkg/executor/api.go b/pkg/executor/api.go index b52777f7e..33afc89a0 100644 --- a/pkg/executor/api.go +++ b/pkg/executor/api.go @@ -28,8 +28,6 @@ import ( "github.com/pkg/errors" "go.opencensus.io/plugin/ochttp" "go.uber.org/zap" - k8serrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" fv1 "github.com/fission/fission/pkg/apis/core/v1" ferror "github.com/fission/fission/pkg/error" @@ -44,23 +42,13 @@ func (executor *Executor) getServiceForFunctionAPI(w http.ResponseWriter, r *htt } // get function metadata - m := metav1.ObjectMeta{} - err = json.Unmarshal(body, &m) + fn := &fv1.Function{} + err = json.Unmarshal(body, &fn) if err != nil { http.Error(w, "Failed to parse request", http.StatusBadRequest) return } - fn, err := executor.fissionClient.CoreV1().Functions(m.Namespace).Get(m.Name, metav1.GetOptions{}) - if err != nil { - if k8serrors.IsNotFound(err) { - http.Error(w, "Failed to find function", http.StatusNotFound) - } else { - http.Error(w, "Failed to get function", http.StatusInternalServerError) - } - return - } - t := fn.Spec.InvokeStrategy.ExecutionStrategy.ExecutorType et, exists := executor.executorTypes[t] if !exists { @@ -88,7 +76,7 @@ func (executor *Executor) getServiceForFunctionAPI(w http.ResponseWriter, r *htt code, msg := ferror.GetHTTPError(err) executor.logger.Error("error getting service for function", zap.Error(err), - zap.String("function", m.Name), + zap.String("function", fn.ObjectMeta.Name), zap.String("fission_http_error", msg)) http.Error(w, msg, code) return @@ -98,7 +86,7 @@ func (executor *Executor) getServiceForFunctionAPI(w http.ResponseWriter, r *htt if err != nil { executor.logger.Error( "error writing HTTP response", - zap.String("function", m.Name), + zap.String("function", fn.ObjectMeta.Name), zap.Error(err), ) } diff --git a/pkg/executor/client/client.go b/pkg/executor/client/client.go index 0d7e260ad..06e6828a4 100644 --- a/pkg/executor/client/client.go +++ b/pkg/executor/client/client.go @@ -70,10 +70,10 @@ func MakeClient(logger *zap.Logger, executorURL string) *Client { } // GetServiceForFunction returns the service name for a given function. -func (c *Client) GetServiceForFunction(ctx context.Context, metadata *metav1.ObjectMeta) (string, error) { +func (c *Client) GetServiceForFunction(ctx context.Context, fn *fv1.Function) (string, error) { executorURL := c.executorURL + "/v2/getServiceForFunction" - body, err := json.Marshal(metadata) + body, err := json.Marshal(fn) if err != nil { return "", errors.Wrap(err, "could not marshal request body for getting service for function") } diff --git a/pkg/executor/executor_test.go b/pkg/executor/executor_test.go index cc9bdce3f..b8ca3abae 100644 --- a/pkg/executor/executor_test.go +++ b/pkg/executor/executor_test.go @@ -256,7 +256,7 @@ func TestExecutor(t *testing.T) { // the main test: get a service for a given function t1 := time.Now() - svc, err := poolmgrClient.GetServiceForFunction(context.Background(), &f.ObjectMeta) + svc, err := poolmgrClient.GetServiceForFunction(context.Background(), f) if err != nil { log.Panicf("failed to get func svc: %v", err) } diff --git a/pkg/router/functionHandler.go b/pkg/router/functionHandler.go index 055d1f7d2..50993a21c 100644 --- a/pkg/router/functionHandler.go +++ b/pkg/router/functionHandler.go @@ -528,7 +528,7 @@ func (fh functionHandler) getServiceEntryFromExecutor() (*url.URL, error) { ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() - service, err := fh.executor.GetServiceForFunction(ctx, &fh.function.ObjectMeta) + service, err := fh.executor.GetServiceForFunction(ctx, fh.function) if err != nil { statusCode, errMsg := ferror.GetHTTPError(err) fh.logger.Error("error from GetServiceForFunction",