Add "internal" routes for all functions

These routes allow us to call any function without explicitly defining
a route for them.  TODO: if we can serve these routes from a separate
instance of router (controlled by a commandline flag or env var), we'd
be able to keep this instance of the router "private" by not assigning
it a loadbalancer/nodeport or other externally-visible service.
This commit is contained in:
Soam Vasani
2016-12-09 23:34:46 -08:00
parent e49af8ca78
commit 3a6d5bb070
2 changed files with 52 additions and 3 deletions
+25
View File
@@ -0,0 +1,25 @@
/*
Copyright 2016 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 fission
import (
"fmt"
)
func UrlForFunction(m *Metadata) string {
return fmt.Sprintf("/fission-function/%v/%v", m.Name, m.Uid)
}
+27 -3
View File
@@ -35,6 +35,7 @@ type HTTPTriggerSet struct {
controller *controllerClient.Client
poolmgr *poolmgrClient.Client
triggers []fission.HTTPTrigger
functions []fission.Function
}
func makeHTTPTriggerSet(fmap *functionServiceMap, controller *controllerClient.Client, poolmgr *poolmgrClient.Client) *HTTPTriggerSet {
@@ -49,12 +50,14 @@ func makeHTTPTriggerSet(fmap *functionServiceMap, controller *controllerClient.C
func (ts *HTTPTriggerSet) subscribeRouter(mr *mutableRouter) {
ts.mutableRouter = mr
mr.updateRouter(ts.getRouterFromTriggers())
mr.updateRouter(ts.getRouter())
go ts.watchTriggers()
}
func (ts *HTTPTriggerSet) getRouterFromTriggers() *mux.Router {
func (ts *HTTPTriggerSet) getRouter() *mux.Router {
muxRouter := mux.NewRouter()
// HTTP triggers setup by the user
for _, trigger := range ts.triggers {
fh := &functionHandler{
fmap: ts.functionServiceMap,
@@ -63,6 +66,18 @@ func (ts *HTTPTriggerSet) getRouterFromTriggers() *mux.Router {
}
muxRouter.HandleFunc(trigger.UrlPattern, fh.handler)
}
// Internal triggers for each function
for _, function := range ts.functions {
fh := &functionHandler{
fmap: ts.functionServiceMap,
Function: function.Metadata,
poolmgr: ts.poolmgr,
}
muxRouter.HandleFunc(fission.UrlForFunction(&function.Metadata),
fh.handler)
}
return muxRouter
}
@@ -102,9 +117,18 @@ func (ts *HTTPTriggerSet) watchTriggers() {
if failureCount >= maxFailures {
log.Fatalf("Failed to connect to controller after %v retries: %v", failureCount, err)
}
time.Sleep(time.Duration(pollSleepSec) * time.Second)
continue
}
ts.triggers = triggers
ts.mutableRouter.updateRouter(ts.getRouterFromTriggers())
functions, err := ts.controller.FunctionList()
if err != nil {
log.Fatalf("Failed to get function list")
}
ts.functions = functions
ts.mutableRouter.updateRouter(ts.getRouter())
time.Sleep(time.Duration(pollSleepSec) * time.Second)
}
}