@@ -4,11 +4,14 @@ import (
44 "context"
55 "errors"
66 "net/http"
7+ "os"
78
89 cloudevents "github.com/cloudevents/sdk-go/v2"
910 "k8s.io/klog/v2"
1011
1112 ofctx "github.com/OpenFunction/functions-framework-go/context"
13+ "github.com/OpenFunction/functions-framework-go/internal/functions"
14+ "github.com/OpenFunction/functions-framework-go/internal/registry"
1215 "github.com/OpenFunction/functions-framework-go/plugin"
1316 plgExample "github.com/OpenFunction/functions-framework-go/plugin/plugin-example"
1417 "github.com/OpenFunction/functions-framework-go/plugin/skywalking"
@@ -23,6 +26,7 @@ type functionsFrameworkImpl struct {
2326 postPlugins []plugin.Plugin
2427 pluginMap map [string ]plugin.Plugin
2528 runtime runtime.Interface
29+ registry * registry.Registry
2630}
2731
2832// Framework is the interface for the function conversion.
@@ -36,6 +40,9 @@ type Framework interface {
3640func NewFramework () (* functionsFrameworkImpl , error ) {
3741 fwk := & functionsFrameworkImpl {}
3842
43+ // Set the function registry
44+ fwk .registry = registry .Default ()
45+
3946 // Parse OpenFunction FunctionContext
4047 if ctx , err := ofctx .GetRuntimeContext (); err != nil {
4148 klog .Errorf ("failed to parse OpenFunction FunctionContext: %v\n " , err )
@@ -59,17 +66,29 @@ func NewFramework() (*functionsFrameworkImpl, error) {
5966
6067func (fwk * functionsFrameworkImpl ) Register (ctx context.Context , fn interface {}) error {
6168 if fnHTTP , ok := fn .(func (http.ResponseWriter , * http.Request )); ok {
62- if err := fwk .runtime .RegisterHTTPFunction (fwk .funcContext , fwk .prePlugins , fwk .postPlugins , fnHTTP ); err != nil {
69+ rf , err := functions .New (functions .WithFunctionName (fwk .funcContext .GetName ()), functions .WithHTTP (fnHTTP ), functions .WithFunctionPath (fwk .funcContext .GetHttpPattern ()))
70+ if err != nil {
71+ klog .Errorf ("failed to register function: %v" , err )
72+ }
73+ if err := fwk .runtime .RegisterHTTPFunction (fwk .funcContext , fwk .prePlugins , fwk .postPlugins , rf ); err != nil {
6374 klog .Errorf ("failed to register function: %v" , err )
6475 return err
6576 }
6677 } else if fnOpenFunction , ok := fn .(func (ofctx.Context , []byte ) (ofctx.Out , error )); ok {
67- if err := fwk .runtime .RegisterOpenFunction (fwk .funcContext , fwk .prePlugins , fwk .postPlugins , fnOpenFunction ); err != nil {
78+ rf , err := functions .New (functions .WithFunctionName (fwk .funcContext .GetName ()), functions .WithOpenFunction (fnOpenFunction ), functions .WithFunctionPath (fwk .funcContext .GetHttpPattern ()))
79+ if err != nil {
80+ klog .Errorf ("failed to register function: %v" , err )
81+ }
82+ if err := fwk .runtime .RegisterOpenFunction (fwk .funcContext , fwk .prePlugins , fwk .postPlugins , rf ); err != nil {
6883 klog .Errorf ("failed to register function: %v" , err )
6984 return err
7085 }
7186 } else if fnCloudEvent , ok := fn .(func (context.Context , cloudevents.Event ) error ); ok {
72- if err := fwk .runtime .RegisterCloudEventFunction (ctx , fwk .funcContext , fwk .prePlugins , fwk .postPlugins , fnCloudEvent ); err != nil {
87+ rf , err := functions .New (functions .WithFunctionName (fwk .funcContext .GetName ()), functions .WithCloudEvent (fnCloudEvent ), functions .WithFunctionPath (fwk .funcContext .GetHttpPattern ()))
88+ if err != nil {
89+ klog .Errorf ("failed to register function: %v" , err )
90+ }
91+ if err := fwk .runtime .RegisterCloudEventFunction (ctx , fwk .funcContext , fwk .prePlugins , fwk .postPlugins , rf ); err != nil {
7392 klog .Errorf ("failed to register function: %v" , err )
7493 return err
7594 }
@@ -82,6 +101,56 @@ func (fwk *functionsFrameworkImpl) Register(ctx context.Context, fn interface{})
82101}
83102
84103func (fwk * functionsFrameworkImpl ) Start (ctx context.Context ) error {
104+
105+ target := os .Getenv ("FUNCTION_TARGET" )
106+
107+ // if FUNCTION_TARGET is provided
108+ if len (target ) > 0 {
109+ if fn , ok := fwk .registry .GetRegisteredFunction (target ); ok {
110+ klog .Infof ("registering function: %s on path: %s" , target , fn .GetPath ())
111+ switch fn .GetFunctionType () {
112+ case functions .HTTPType :
113+ fwk .Register (ctx , fn .GetHTTPFunction ())
114+ case functions .CloudEventType :
115+ fwk .Register (ctx , fn .GetCloudEventFunction ())
116+ case functions .OpenFunctionType :
117+ fwk .Register (ctx , fn .GetOpenFunctionFunction ())
118+ }
119+ } else {
120+ klog .Errorf ("function not found: %s" , target )
121+ }
122+ } else {
123+ // if FUNCTION_TARGET is not provided but user uses declarative function, by default all registered functions will be deployed.
124+ funcNames := fwk .registry .GetFunctionNames ()
125+ if len (funcNames ) > 1 && fwk .funcContext .GetRuntime () == ofctx .Async {
126+ return errors .New ("only one function is allowed in async runtime" )
127+ } else if len (funcNames ) > 0 {
128+ klog .Info ("no 'FUNCTION_TARGET' is provided, register all the functions in the registry" )
129+ for _ , name := range funcNames {
130+ if rf , ok := fwk .registry .GetRegisteredFunction (name ); ok {
131+ klog .Infof ("registering function: %s on path: %s" , rf .GetName (), rf .GetPath ())
132+ switch rf .GetFunctionType () {
133+ case functions .HTTPType :
134+ if err := fwk .runtime .RegisterHTTPFunction (fwk .funcContext , fwk .prePlugins , fwk .postPlugins , rf ); err != nil {
135+ klog .Errorf ("failed to register function: %v" , err )
136+ return err
137+ }
138+ case functions .CloudEventType :
139+ if err := fwk .runtime .RegisterCloudEventFunction (ctx , fwk .funcContext , fwk .prePlugins , fwk .postPlugins , rf ); err != nil {
140+ klog .Errorf ("failed to register function: %v" , err )
141+ return err
142+ }
143+ case functions .OpenFunctionType :
144+ if err := fwk .runtime .RegisterOpenFunction (fwk .funcContext , fwk .prePlugins , fwk .postPlugins , rf ); err != nil {
145+ klog .Errorf ("failed to register function: %v" , err )
146+ return err
147+ }
148+ }
149+ }
150+ }
151+ }
152+ }
153+
85154 err := fwk .runtime .Start (ctx )
86155 if err != nil {
87156 klog .Error ("failed to start runtime service" )
@@ -136,13 +205,12 @@ func createRuntime(fwk *functionsFrameworkImpl) error {
136205 rt := fwk .funcContext .GetRuntime ()
137206 port := fwk .funcContext .GetPort ()
138207 pattern := fwk .funcContext .GetHttpPattern ()
139-
140208 switch rt {
141209 case ofctx .Knative :
142210 fwk .runtime = knative .NewKnativeRuntime (port , pattern )
143211 return nil
144212 case ofctx .Async :
145- fwk .runtime , err = async .NewAsyncRuntime (port )
213+ fwk .runtime , err = async .NewAsyncRuntime (port , pattern )
146214 if err != nil {
147215 return err
148216 }
0 commit comments