@@ -13,12 +13,28 @@ import (
1313 `time`
1414)
1515
16+ // Fission is a wrapper on top of simfaas that emulates a part of the
17+ // interface of Fission.
1618type Fission struct {
1719 Platform * Platform
1820 FnFactory func (name string ) * FunctionConfig
21+
22+ // CreateUndefinedFunctions enables, if set to true,
23+ // the automatic creation of a function if it is called.
1924 CreateUndefinedFunctions bool
2025}
2126
27+ func (f * Fission ) Start () error {
28+ return f .Platform .Start ()
29+ }
30+
31+ func (f * Fission ) Close () error {
32+ return f .Platform .Close ()
33+ }
34+
35+ // GetServiceForFunction emulates the mapping of a function to a service
36+ // name/host. Currently it just returns the name of the function as the
37+ // service name.
2238func (f * Fission ) GetServiceForFunction (fnName string ) (string , error ) {
2339 f .createIfUndefined (fnName )
2440 fn , ok := f .Platform .Get (fnName )
@@ -28,7 +44,10 @@ func (f *Fission) GetServiceForFunction(fnName string) (string, error) {
2844 return fn .name , nil
2945}
3046
31- // We assume that url is just the function name
47+ // TapService deploys (or keeps deployed) a function instance for the function.
48+ //
49+ // Note: similar as in GetServiceForFunction, we assume that the service url is
50+ // just the function name.
3251func (f * Fission ) TapService (svcURL string ) error {
3352 if len (svcURL ) == 0 {
3453 return errors .New ("no url provided to tap" )
@@ -48,6 +67,15 @@ func (f *Fission) TapService(svcURL string) error {
4867 return nil
4968}
5069
70+ // Run emulates the execution of a Fission Function.
71+ //
72+ // If the runtime is not nil it will be used to override the runtime
73+ // specified in the config of the function.
74+ func (f * Fission ) Run (fnName string , runtime * time.Duration ) (* ExecutionReport , error ) {
75+ f .createIfUndefined (fnName )
76+ return f .Platform .Run (fnName , runtime )
77+ }
78+
5179func (f * Fission ) Serve () http.Handler {
5280 handler := & RegexpHandler {}
5381 handler .HandleFunc (regexp .MustCompile ("/v2/functions/.*" ), f .HandleFunctionsGet )
@@ -57,7 +85,8 @@ func (f *Fission) Serve() http.Handler {
5785 return handler
5886}
5987
60- // /v2/getServiceForFunction
88+ // HandleGetServiceForFunction emulates the /v2/getServiceForFunction
89+ // Fission endpoint.
6190func (f * Fission ) HandleGetServiceForFunction (w http.ResponseWriter , r * http.Request ) {
6291 bs , err := ioutil .ReadAll (r .Body )
6392 r .Body .Close ()
@@ -81,11 +110,7 @@ func (f *Fission) HandleGetServiceForFunction(w http.ResponseWriter, r *http.Req
81110 w .Write ([]byte (svc ))
82111}
83112
84- func (f * Fission ) Start () error {
85- return f .Platform .Start ()
86- }
87-
88- // /v2/tapService
113+ // HandleTapService emulates the /v2/tapService Fission endpoint.
89114func (f * Fission ) HandleTapService (w http.ResponseWriter , r * http.Request ) {
90115 bs , err := ioutil .ReadAll (r .Body )
91116 defer r .Body .Close ()
@@ -105,19 +130,22 @@ func (f *Fission) HandleTapService(w http.ResponseWriter, r *http.Request) {
105130 w .WriteHeader (http .StatusOK )
106131}
107132
108- // /v2/functions/.*
133+ // HandleFunctionsGet emulates the /v2/functions/.* Fission endpoints.
134+ // Currently it simply returns an empty map.
109135func (f * Fission ) HandleFunctionsGet (w http.ResponseWriter , r * http.Request ) {
110136 w .WriteHeader (http .StatusOK )
111137 w .Write ([]byte ("{}" ))
112138}
113139
114- // /fission-function/
140+ // HandleFunctionRun emulates the /fission-function/.* Fission endpoints.
141+ //
142+ // It checks for the presence of the runtime query parameter,
143+ // which allows you to override the runtime of the function.
115144func (f * Fission ) HandleFunctionRun (w http.ResponseWriter , r * http.Request ) {
116145 // Parse arguments: fnname, runtime
117146 var seconds float64
118147 var err error
119148 if queryRuntime := r .URL .Query ().Get ("runtime" ); len (queryRuntime ) > 0 {
120- // Read query
121149 seconds , err = strconv .ParseFloat (queryRuntime , 64 )
122150 if err != nil {
123151 http .Error (w , err .Error (), http .StatusBadRequest )
@@ -126,22 +154,18 @@ func (f *Fission) HandleFunctionRun(w http.ResponseWriter, r *http.Request) {
126154 }
127155 runtime := time .Duration (seconds * float64 (time .Second ))
128156 fnName := getFunctionNameFromUrl (r .URL )
129- f .createIfUndefined (fnName )
130157
131- // Run function
132- report , err := f .Platform .Run (fnName , & runtime )
158+ report , err := f .Run (fnName , & runtime )
133159 if err != nil {
134160 http .Error (w , err .Error (), http .StatusBadRequest )
135161 return
136162 }
137163
138- // Simulate runtime
139- result , _ := json .Marshal (map [string ]interface {}{
140- "started_at" : report .StartedAt .UnixNano (),
141- "finished_at" : report .FinishedAt .UnixNano (),
142- "coldStart" : report .ColdStart .Nanoseconds (),
143- "runtime" : report .Runtime .Nanoseconds (),
144- })
164+ result , err := json .Marshal (report )
165+ if err != nil {
166+ http .Error (w , err .Error (), http .StatusInternalServerError )
167+ return
168+ }
145169 w .WriteHeader (http .StatusOK )
146170 w .Write (result )
147171}
@@ -162,8 +186,6 @@ type ObjectMeta struct {
162186 // Namespace string `json:"namespace,omitempty" protobuf:"bytes,3,opt,name=namespace"`
163187}
164188
165- var fnUrlRegEx = regexp .MustCompile ("/(.*)$" )
166-
167189func getFunctionNameFromUrl (url * url.URL ) string {
168190 return url .Path [strings .LastIndex (url .Path , "/" )+ 1 :]
169191}
0 commit comments