Skip to content

Commit cb68c32

Browse files
committed
samples of OpenFunction v0.6.0
Signed-off-by: laminar <[email protected]>
1 parent 1f21c19 commit cb68c32

File tree

72 files changed

+594
-737
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+594
-737
lines changed

README.md

-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
# OpenFunction function samples
22

3-
For samples of the latest stable version, please visit here:
4-
5-
-> [v0.5.0 samples](https://github.com/openFunction/samples/tree/release-0.5)
6-
73
## Function samples
84

95
The following are samples of OpenFunction function.

functions-framework/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,28 @@ The processing in the plugin is as follows:
3434
plugin-custom
3535

3636
```go
37-
func (p *PluginCustom) ExecPreHook(ctx ofctx.Context, plugins map[string]plugin.Plugin) error {
37+
func (p *PluginCustom) ExecPreHook(ctx ofctx.RuntimeContext, plugins map[string]plugin.Plugin) error {
3838
p.stateC = 3
3939
p.StateC = 3
4040
return nil
4141
}
4242

43-
func (p *PluginCustom) ExecPostHook(ctx ofctx.Context, plugins map[string]plugin.Plugin) error {
43+
func (p *PluginCustom) ExecPostHook(ctx ofctx.RuntimeContext, plugins map[string]plugin.Plugin) error {
4444
return nil
4545
}
4646
```
4747

4848
plugin-example
4949

5050
```go
51-
func (p *PluginExample) ExecPreHook(ctx ofctx.Context, plugins map[string]plugin.Plugin) error {
51+
func (p *PluginExample) ExecPreHook(ctx ofctx.RuntimeContext, plugins map[string]plugin.Plugin) error {
5252
r := preHookLogic(ctx.Ctx)
5353
p.stateA = 1
5454
p.stateB = r
5555
return nil
5656
}
5757

58-
func (p *PluginExample) ExecPostHook(ctx ofctx.Context, plugins map[string]plugin.Plugin) error {
58+
func (p *PluginExample) ExecPostHook(ctx ofctx.RuntimeContext, plugins map[string]plugin.Plugin) error {
5959
// Get data from another plugin via Plugin.Get()
6060
plgName := "plugin-custom"
6161
keyName := "StateC"

functions-framework/golang/Async/bindings/README.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ Prepare a context as follows, name it `function.json`. (You can refer to [OpenFu
2323
"inputs": {
2424
"cron": {
2525
"uri": "cron_input",
26-
"type": "bindings",
27-
"component": "cron_input"
26+
"componentType": "bindings.cron",
27+
"componentName": "cron_input"
2828
}
2929
},
3030
"outputs": {},
@@ -37,7 +37,8 @@ Prepare a context as follows, name it `function.json`. (You can refer to [OpenFu
3737
Create an environment variable `FUNC_CONTEXT` and assign the above context to it.
3838

3939
```shell
40-
export FUNC_CONTEXT='{"name":"bindings","version":"v1","requestID":"a0f2ad8d-5062-4812-91e9-95416489fb01","port":"50002","inputs":{"cron":{"uri":"cron_input","type":"bindings","component":"cron_input"}},"outputs":{},"runtime":"Async","prePlugins":["plugin-custom","plugin-example"],"postPlugins":["plugin-custom","plugin-example"]}'
40+
export FUNC_CONTEXT='{"name":"bindings","version":"v1","requestID":"a0f2ad8d-5062-4812-91e9-95416489fb01","port":"50002","inputs":{"cron":{"uri":"cron_input","componentType":"bindings.cron","componentName":"cron_input"}},"outputs":{},"runtime":"Async","prePlugins":["plugin-custom","plugin-example"],"postPlugins":["plugin-custom","plugin-example"]}'
41+
export CONTEXT_MODE='self-host'
4142
```
4243

4344
### Run
@@ -52,7 +53,7 @@ dapr run --app-id bindings_grpc \
5253
--app-port 50002 \
5354
--dapr-grpc-port 50001 \
5455
--components-path ../../components \
55-
go run ./main.go ./plugin.go
56+
go run ./main.go
5657
```
5758

5859
## Bindings with output
@@ -100,20 +101,20 @@ In this example, the proxy address of Dapr will be used as the target of output.
100101
"inputs": {
101102
"cron": {
102103
"uri": "cron_input",
103-
"type": "bindings",
104-
"component": "cron_input"
104+
"componentType": "bindings.cron",
105+
"componentName": "cron_input"
105106
}
106107
},
107108
"outputs": {
108109
"echo": {
109110
"uri": "echo",
110111
"operation": "create",
111-
"component": "echo",
112+
"componentName": "echo",
112113
"metadata": {
113114
"path": "echo",
114115
"Content-Type": "application/json; charset=utf-8"
115116
},
116-
"type": "bindings"
117+
"componentType": "bindings.http"
117118
}
118119
},
119120
"runtime": "Async",
@@ -125,7 +126,8 @@ In this example, the proxy address of Dapr will be used as the target of output.
125126
Create an environment variable `FUNC_CONTEXT` and assign the above context to it.
126127

127128
```shell
128-
export FUNC_CONTEXT='{"name":"bindings","version":"v1","requestID":"a0f2ad8d-5062-4812-91e9-95416489fb01","port":"50002","inputs":{"cron":{"uri":"cron_input","type":"bindings","component":"cron_input"}},"outputs":{"echo":{"uri":"echo","operation":"create","component":"echo","metadata":{"path":"echo","Content-Type":"application/json; charset=utf-8"},"type":"bindings"}},"runtime":"Async","prePlugins":["plugin-custom","plugin-example"],"postPlugins":["plugin-custom","plugin-example"]}'
129+
export FUNC_CONTEXT='{"name":"bindings","version":"v1","requestID":"a0f2ad8d-5062-4812-91e9-95416489fb01","port":"50002","inputs":{"cron":{"uri":"cron_input","componentType":"bindings.cron","componentName":"cron_input"}},"outputs":{"echo":{"uri":"echo","operation":"create","componentName":"echo","metadata":{"path":"echo","Content-Type":"application/json; charset=utf-8"},"componentType":"bindings.http"}},"runtime":"Async","prePlugins":["plugin-custom","plugin-example"],"postPlugins":["plugin-custom","plugin-example"]}'
130+
export CONTEXT_MODE='self-host'
129131
```
130132

131133
### Run
@@ -139,7 +141,7 @@ dapr run --app-id bindings_grpc \
139141
--app-port 50002 \
140142
--dapr-grpc-port 50001 \
141143
--components-path ../../components \
142-
go run ./main.go ./plugin.go
144+
go run ./main.go
143145
```
144146

145147

Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module main.go
22

3-
go 1.15
3+
go 1.16
44

55
require (
6-
github.com/OpenFunction/functions-framework-go v0.1.2-0.20220109033919-0fb66d159417
6+
github.com/OpenFunction/functions-framework-go v0.2.0
77
github.com/fatih/structs v1.1.0
88
k8s.io/klog/v2 v2.40.1
99
)

functions-framework/golang/Async/bindings/with-output/main.go

+14
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"context"
55

66
"github.com/OpenFunction/functions-framework-go/framework"
7+
"github.com/OpenFunction/functions-framework-go/plugin"
78
"k8s.io/klog/v2"
89
"main.go/userfunction"
10+
pluginCustom "main.go/userfunction/plugins/plugin-custom"
911
)
1012

1113
func main() {
@@ -22,3 +24,15 @@ func main() {
2224
klog.Exit(err)
2325
}
2426
}
27+
28+
func getLocalPlugins() map[string]plugin.Plugin {
29+
localPlugins := map[string]plugin.Plugin{
30+
pluginCustom.Name: pluginCustom.New(),
31+
}
32+
33+
if len(localPlugins) == 0 {
34+
return nil
35+
} else {
36+
return localPlugins
37+
}
38+
}

functions-framework/golang/Async/bindings/with-output/plugin.go

-21
This file was deleted.

functions-framework/golang/Async/bindings/with-output/userfunction/plugins/plugin-custom/plugin-custom.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ func (p *PluginCustom) Version() string {
3232
return Version
3333
}
3434

35-
func (p *PluginCustom) ExecPreHook(ctx ofctx.Context, plugins map[string]plugin.Plugin) error {
35+
func (p *PluginCustom) Init() plugin.Plugin {
36+
return New()
37+
}
38+
39+
func (p *PluginCustom) ExecPreHook(ctx ofctx.RuntimeContext, plugins map[string]plugin.Plugin) error {
3640
p.stateC = 3
3741
p.StateC = 3
3842
return nil
3943
}
4044

41-
func (p *PluginCustom) ExecPostHook(ctx ofctx.Context, plugins map[string]plugin.Plugin) error {
45+
func (p *PluginCustom) ExecPostHook(ctx ofctx.RuntimeContext, plugins map[string]plugin.Plugin) error {
4246
return nil
4347
}
4448

Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module main.go
22

3-
go 1.15
3+
go 1.16
44

55
require (
6-
github.com/OpenFunction/functions-framework-go v0.1.2-0.20220109033919-0fb66d159417
6+
github.com/OpenFunction/functions-framework-go v0.2.0
77
github.com/fatih/structs v1.1.0
88
k8s.io/klog/v2 v2.40.1
99
)

functions-framework/golang/Async/bindings/without-output/main.go

+14
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"context"
55

66
"github.com/OpenFunction/functions-framework-go/framework"
7+
"github.com/OpenFunction/functions-framework-go/plugin"
78
"k8s.io/klog/v2"
89
"main.go/userfunction"
10+
pluginCustom "main.go/userfunction/plugins/plugin-custom"
911
)
1012

1113
func main() {
@@ -22,3 +24,15 @@ func main() {
2224
klog.Exit(err)
2325
}
2426
}
27+
28+
func getLocalPlugins() map[string]plugin.Plugin {
29+
localPlugins := map[string]plugin.Plugin{
30+
pluginCustom.Name: pluginCustom.New(),
31+
}
32+
33+
if len(localPlugins) == 0 {
34+
return nil
35+
} else {
36+
return localPlugins
37+
}
38+
}

functions-framework/golang/Async/bindings/without-output/plugin.go

-21
This file was deleted.

functions-framework/golang/Async/bindings/without-output/userfunction/plugins/plugin-custom/plugin-custom.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ func (p *PluginCustom) Version() string {
3232
return Version
3333
}
3434

35-
func (p *PluginCustom) ExecPreHook(ctx ofctx.Context, plugins map[string]plugin.Plugin) error {
35+
func (p *PluginCustom) Init() plugin.Plugin {
36+
return New()
37+
}
38+
39+
func (p *PluginCustom) ExecPreHook(ctx ofctx.RuntimeContext, plugins map[string]plugin.Plugin) error {
3640
p.stateC = 3
3741
p.StateC = 3
3842
return nil
3943
}
4044

41-
func (p *PluginCustom) ExecPostHook(ctx ofctx.Context, plugins map[string]plugin.Plugin) error {
45+
func (p *PluginCustom) ExecPostHook(ctx ofctx.RuntimeContext, plugins map[string]plugin.Plugin) error {
4246
return nil
4347
}
4448

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: dapr.io/v1alpha1
2+
kind: Component
3+
metadata:
4+
name: msg
5+
spec:
6+
type: pubsub.kafka
7+
version: v1
8+
metadata:
9+
- name: brokers
10+
value: "kafka-server-kafka-brokers:9092"
11+
- name: consumerGroup
12+
value: "group1"
13+
- name: authRequired
14+
value: "false"

0 commit comments

Comments
 (0)