/
target.go
92 lines (74 loc) · 1.7 KB
/
target.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"context"
"github.com/fatih/structs"
"k8s.io/klog/v2"
ofctx "github.com/OpenFunction/functions-framework-go/context"
"github.com/OpenFunction/functions-framework-go/framework"
"github.com/OpenFunction/functions-framework-go/plugin"
)
func main() {
ctx := context.Background()
fwk, err := framework.NewFramework()
if err != nil {
klog.Exit(err)
}
fwk.RegisterPlugins(getLocalPlugins())
if err := fwk.Register(ctx, Target); err != nil {
klog.Exit(err)
}
if err := fwk.Start(ctx); err != nil {
klog.Exit(err)
}
}
func getLocalPlugins() map[string]plugin.Plugin {
localPlugins := map[string]plugin.Plugin{
Name: New(),
}
if len(localPlugins) == 0 {
return nil
} else {
return localPlugins
}
}
func Target(ctx ofctx.Context, in []byte) (ofctx.Out, error) {
klog.Infof("bindings - Data: %s", in)
return ctx.ReturnOnSuccess(), nil
}
// Plugin
const (
Name = "plugin-custom"
Version = "v1"
)
type PluginCustom struct {
PluginName string
PluginVersion string
StateC int64
}
var _ plugin.Plugin = &PluginCustom{}
func New() *PluginCustom {
return &PluginCustom{
StateC: int64(0),
}
}
func (p *PluginCustom) Name() string {
return Name
}
func (p *PluginCustom) Version() string {
return Version
}
func (p *PluginCustom) Init() plugin.Plugin {
return New()
}
func (p *PluginCustom) ExecPreHook(ctx ofctx.RuntimeContext, plugins map[string]plugin.Plugin) error {
p.StateC++
return nil
}
func (p *PluginCustom) ExecPostHook(ctx ofctx.RuntimeContext, plugins map[string]plugin.Plugin) error {
return nil
}
func (p *PluginCustom) Get(fieldName string) (interface{}, bool) {
plgMap := structs.Map(p)
value, ok := plgMap[fieldName]
return value, ok
}