-
Notifications
You must be signed in to change notification settings - Fork 45
Support TinyGo SDK for extension toolkit #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 41 commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
73a23a2
Add tinygo lang
mathetake 11ee7a8
allow abi 0.1.0
mathetake d6f8eba
Fix e2e
mathetake ebb1b11
Fix e2e
mathetake 9dce679
allow abi 0.1.0
mathetake fbc011b
Fix e2e
mathetake f53036f
Fix e2e
mathetake f4f46d3
fix e2e on mac
mathetake e3c77b0
fix e2e
mathetake 29743bb
fix e2e
mathetake 98e0f9e
fix e2e
mathetake e568706
Clean up e2e
mathetake 54a9e7a
Run tinygo with 1.17.0
mathetake 98ebe99
Merge branch 'tinygo-extension' of github.com:tetratelabs/getenvoy in…
mathetake 3292cfd
Update tinygo builder
mathetake e046f90
Fix e2e
mathetake f4df444
Merge branch 'master' of github.com:tetratelabs/getenvoy into tinygo-…
mathetake 62d5aa7
merge origin/master
mathetake 25e93d8
Update TinyGo version
mathetake 7afb2c2
Fix e2e
mathetake dc70c79
merge origin/master
mathetake d5e430c
review: fix styles
mathetake 20e83c0
review: fix styles part 2
mathetake 93b40f6
improve access logger example: reading config
mathetake f250078
improve http example: reading config
mathetake 482909d
improve network example: reading config
mathetake 2f146fd
Fix unittests
mathetake 6f2e72c
Improve comments and namings
mathetake 2a300e9
Remove left 'exec'
mathetake b1b223d
Remove unnecessary rm in clean command
mathetake bbb29b3
Reivew: use enumed OnPluginStart, host.GetCounterMetric
mathetake f75cff7
Add go mod download
mathetake f960379
Re: fix entrypoint
mathetake 23e8fbc
Revert: add go.sum
mathetake 7443281
Revert debug code
mathetake 45a02f3
Add .gitignore, and set extension name in go.mod
mathetake b9024b0
default output to build/extension.wasm
mathetake 436d507
Update SDK: fix error
mathetake b485f05
Fix permission on host
mathetake 5ce9aa6
Fix unittest
mathetake b3c33e1
Fix comment
mathetake 52b7944
Fix permission, showcase config in access logger
mathetake 9715e55
Update SDK
mathetake File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
data/extension/init/templates/tinygo/envoy.access_loggers/default/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /build/ |
8 changes: 8 additions & 0 deletions
8
data/extension/init/templates/tinygo/envoy.access_loggers/default/go.mod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| module {{ .Extension.Name }} | ||
|
|
||
| go 1.15 | ||
|
|
||
| require ( | ||
| github.com/stretchr/testify v1.6.1 | ||
| github.com/tetratelabs/proxy-wasm-go-sdk v0.0.16-0.20210312030126-362d11ef7757 | ||
| ) |
13 changes: 13 additions & 0 deletions
13
data/extension/init/templates/tinygo/envoy.access_loggers/default/go.sum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
| github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
| github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
| github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
| github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= | ||
| github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
| github.com/tetratelabs/proxy-wasm-go-sdk v0.0.16-0.20210312030126-362d11ef7757 h1:97z8twAIBLEll7HkPHVaMCLs/+OyzCJyazAFTCiHKhA= | ||
| github.com/tetratelabs/proxy-wasm-go-sdk v0.0.16-0.20210312030126-362d11ef7757/go.mod h1:y1ZQT4bQEBnR8Do4nSOzb3roczzPvcAp8UrF6NEYWNY= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
| gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | ||
| gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
46 changes: 46 additions & 0 deletions
46
data/extension/init/templates/tinygo/envoy.access_loggers/default/main.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package main | ||
mathetake marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm" | ||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types" | ||
| ) | ||
|
|
||
| func main() { | ||
| proxywasm.SetNewRootContext(newAccessLogger) | ||
| } | ||
|
|
||
| type accessLogger struct { | ||
| // You'd better embed the default root context | ||
| // so that you don't need to reimplement all the methods by yourself. | ||
| proxywasm.DefaultRootContext | ||
| logMessage string | ||
| } | ||
|
|
||
| func newAccessLogger(contextID uint32) proxywasm.RootContext { | ||
| return &accessLogger{} | ||
| } | ||
|
|
||
| // Override proxywasm.DefaultRootContext | ||
| func (l *accessLogger) OnPluginStart(configurationSize int) types.OnPluginStartStatus { | ||
| // Read plugin configuration provided in Envoy configuration. | ||
| data, err := proxywasm.GetPluginConfiguration(configurationSize) | ||
| if err != nil && err != types.ErrorStatusNotFound { | ||
| proxywasm.LogCriticalf("failed to load config: %v", err) | ||
| return types.OnPluginStartStatusFailed | ||
| } | ||
| l.logMessage = string(data) | ||
| return types.OnPluginStartStatusOK | ||
| } | ||
|
|
||
| // Override proxywasm.DefaultRootContext | ||
| func (l *accessLogger) OnLog() { | ||
| hdr, err := proxywasm.GetHttpRequestHeader(":path") | ||
| if err != nil { | ||
| proxywasm.LogCritical(err.Error()) | ||
| return | ||
| } | ||
|
|
||
| proxywasm.LogInfof("OnLog: :path = %s", hdr) | ||
| proxywasm.LogInfof("message = %s", l.logMessage) | ||
|
|
||
| } | ||
27 changes: 27 additions & 0 deletions
27
data/extension/init/templates/tinygo/envoy.access_loggers/default/main_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxytest" | ||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types" | ||
| ) | ||
|
|
||
| func TestAccessLogger_OnLog(t *testing.T) { | ||
| opt := proxytest.NewEmulatorOption(). | ||
| WithNewRootContext(newAccessLogger) | ||
| host := proxytest.NewHostEmulator(opt) | ||
| // Release the host emulation lock so that other test cases can insert their own host emulation. | ||
| defer host.Done() | ||
|
|
||
mathetake marked this conversation as resolved.
Show resolved
Hide resolved
mathetake marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Call OnLog with the given headers. | ||
| host.CallOnLogForAccessLogger(types.Headers{ | ||
yskopets marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| {":path", "/this/is/path"}, | ||
| }, nil) | ||
|
|
||
| // Check the Envoy logs. | ||
| logs := host.GetLogs(types.LogLevelInfo) | ||
| require.Contains(t, logs, "OnLog: :path = /this/is/path") | ||
| } | ||
1 change: 1 addition & 0 deletions
1
data/extension/init/templates/tinygo/envoy.filters.http/default/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /build/ |
8 changes: 8 additions & 0 deletions
8
data/extension/init/templates/tinygo/envoy.filters.http/default/go.mod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| module {{ .Extension.Name }} | ||
|
|
||
| go 1.15 | ||
|
|
||
| require ( | ||
| github.com/stretchr/testify v1.6.1 | ||
| github.com/tetratelabs/proxy-wasm-go-sdk v0.0.16-0.20210312030126-362d11ef7757 | ||
| ) |
13 changes: 13 additions & 0 deletions
13
data/extension/init/templates/tinygo/envoy.filters.http/default/go.sum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
| github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
| github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
| github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
| github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= | ||
| github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
| github.com/tetratelabs/proxy-wasm-go-sdk v0.0.16-0.20210312030126-362d11ef7757 h1:97z8twAIBLEll7HkPHVaMCLs/+OyzCJyazAFTCiHKhA= | ||
| github.com/tetratelabs/proxy-wasm-go-sdk v0.0.16-0.20210312030126-362d11ef7757/go.mod h1:y1ZQT4bQEBnR8Do4nSOzb3roczzPvcAp8UrF6NEYWNY= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
| gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | ||
| gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
98 changes: 98 additions & 0 deletions
98
data/extension/init/templates/tinygo/envoy.filters.http/default/main.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package main | ||
mathetake marked this conversation as resolved.
Show resolved
Hide resolved
mathetake marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "bufio" | ||
| "bytes" | ||
| "strings" | ||
|
|
||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm" | ||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types" | ||
| ) | ||
|
|
||
| var ( | ||
| requestCounterName = "my_http_filter.request_counter" | ||
| counter proxywasm.MetricCounter | ||
| ) | ||
|
|
||
| func main() { | ||
| proxywasm.SetNewRootContext(newRootContext) | ||
| } | ||
|
|
||
| type rootContext struct { | ||
| // You'd better embed the default root context | ||
| // so that you don't need to reimplement all the methods by yourself. | ||
| proxywasm.DefaultRootContext | ||
| contextID uint32 | ||
| additionalHeaders map[string]string | ||
| } | ||
|
|
||
| func newRootContext(rootContextID uint32) proxywasm.RootContext { | ||
| return &rootContext{contextID: rootContextID, additionalHeaders: map[string]string{"additional": "header"}} | ||
| } | ||
|
|
||
| // Override proxywasm.DefaultRootContext | ||
| func (ctx *rootContext) OnPluginStart(configurationSize int) types.OnPluginStartStatus { | ||
| // Initialize the counter. | ||
| counter = proxywasm.DefineCounterMetric(requestCounterName) | ||
|
|
||
| // Read plugin configuration provided in Envoy configuration. | ||
| data, err := proxywasm.GetPluginConfiguration(configurationSize) | ||
| if err != nil && err != types.ErrorStatusNotFound { | ||
| proxywasm.LogCriticalf("failed to load config: %v", err) | ||
| return types.OnPluginStartStatusFailed | ||
| } | ||
|
|
||
| // Each line in the configuration is in the "KEY=VALUE" format. | ||
| scanner := bufio.NewScanner(bytes.NewReader(data)) | ||
| for scanner.Scan() { | ||
| tokens := strings.Split(scanner.Text(), "=") | ||
| ctx.additionalHeaders[tokens[0]] = tokens[1] | ||
| } | ||
| return types.OnPluginStartStatusOK | ||
| } | ||
|
|
||
| // Override proxywasm.DefaultRootContext | ||
| func (ctx *rootContext) NewHttpContext(uint32) proxywasm.HttpContext { | ||
| return &httpContext{additionalHeaders: ctx.additionalHeaders} | ||
| } | ||
|
|
||
| type httpContext struct { | ||
| // You'd better embed the default http context | ||
| // so that you don't need to reimplement all the methods by yourself. | ||
| proxywasm.DefaultHttpContext | ||
| additionalHeaders map[string]string | ||
| } | ||
|
|
||
| // Override proxywasm.DefaultHttpContext | ||
| func (ctx *httpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action { | ||
| hs, err := proxywasm.GetHttpRequestHeaders() | ||
| if err != nil { | ||
| proxywasm.LogCriticalf("failed to get request headers: %v", err) | ||
| return types.ActionPause | ||
| } | ||
|
|
||
| proxywasm.LogInfo("observing request headers") | ||
| for _, h := range hs { | ||
| proxywasm.LogInfof("%s: %s", h[0], h[1]) | ||
| } | ||
|
|
||
| return types.ActionContinue | ||
| } | ||
|
|
||
| // Override proxywasm.DefaultHttpContext | ||
| func (ctx *httpContext) OnHttpResponseHeaders(numHeaders int, endOfStream bool) types.Action { | ||
| // Set additional headers in the response. | ||
| for key, value := range ctx.additionalHeaders { | ||
| if err := proxywasm.SetHttpResponseHeader(key, value); err != nil { | ||
| proxywasm.LogCriticalf("failed to add header: %v", err) | ||
| return types.ActionPause | ||
| } | ||
| proxywasm.LogInfof("header set: %s=%s", key, value) | ||
| } | ||
| return types.ActionContinue | ||
| } | ||
|
|
||
| // Override proxywasm.DefaultHttpContext | ||
| func (ctx *httpContext) OnHttpStreamDone() { | ||
| counter.Increment(1) | ||
| } | ||
53 changes: 53 additions & 0 deletions
53
data/extension/init/templates/tinygo/envoy.filters.http/default/main_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxytest" | ||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types" | ||
| ) | ||
|
|
||
| func TestHttpFilter_OnHttpRequestHeaders(t *testing.T) { | ||
| configuration := `HELLO=WORLD | ||
| ENVOY=ISTIO` | ||
| opt := proxytest.NewEmulatorOption(). | ||
| WithNewRootContext(newRootContext). | ||
| WithPluginConfiguration([]byte(configuration)) | ||
|
|
||
| host := proxytest.NewHostEmulator(opt) | ||
| // Release the host emulation lock so that other test cases can insert their own host emulation. | ||
| defer host.Done() | ||
|
|
||
| // Call OnPluginStart -> the metric is initialized. | ||
| status := host.StartPlugin() | ||
| // Check the status returned by OnPluginStart is OK. | ||
| require.Equal(t, types.OnPluginStartStatusOK, status) | ||
|
|
||
| // Create http context. | ||
| contextID := host.InitializeHttpContext() | ||
|
|
||
| // Call OnHttpRequestHeaders with the given headers. | ||
| hs := types.Headers{ | ||
| {"key1", "value1"}, | ||
| {"key2", "value2"}, | ||
| } | ||
| action := host.CallOnRequestHeaders(contextID, hs, false) | ||
| // Check the action returned by OnRequestHeaders is Continue. | ||
| require.Equal(t, types.ActionContinue, action) | ||
|
|
||
| // Call OnHttpResponseHeaders. | ||
| action = host.CallOnResponseHeaders(contextID, nil, false) | ||
| // Check the action returned by OnResponseHeaders is Continue. | ||
| require.Equal(t, types.ActionContinue, action) | ||
|
|
||
| // Check Envoy logs. | ||
| logs := host.GetLogs(types.LogLevelInfo) | ||
| require.Contains(t, logs, "header set: ENVOY=ISTIO") | ||
| require.Contains(t, logs, "header set: HELLO=WORLD") | ||
| require.Contains(t, logs, "header set: additional=header") | ||
| require.Contains(t, logs, "key2: value2") | ||
| require.Contains(t, logs, "key1: value1") | ||
| require.Contains(t, logs, "observing request headers") | ||
| } |
1 change: 1 addition & 0 deletions
1
data/extension/init/templates/tinygo/envoy.filters.network/default/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /build/ |
8 changes: 8 additions & 0 deletions
8
data/extension/init/templates/tinygo/envoy.filters.network/default/go.mod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| module {{ .Extension.Name }} | ||
|
|
||
| go 1.15 | ||
|
|
||
| require ( | ||
| github.com/stretchr/testify v1.6.1 | ||
| github.com/tetratelabs/proxy-wasm-go-sdk v0.0.16-0.20210312030126-362d11ef7757 | ||
| ) |
13 changes: 13 additions & 0 deletions
13
data/extension/init/templates/tinygo/envoy.filters.network/default/go.sum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
| github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
| github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
| github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
| github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= | ||
| github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
| github.com/tetratelabs/proxy-wasm-go-sdk v0.0.16-0.20210312030126-362d11ef7757 h1:97z8twAIBLEll7HkPHVaMCLs/+OyzCJyazAFTCiHKhA= | ||
| github.com/tetratelabs/proxy-wasm-go-sdk v0.0.16-0.20210312030126-362d11ef7757/go.mod h1:y1ZQT4bQEBnR8Do4nSOzb3roczzPvcAp8UrF6NEYWNY= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
| gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | ||
| gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
63 changes: 63 additions & 0 deletions
63
data/extension/init/templates/tinygo/envoy.filters.network/default/main.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package main | ||
mathetake marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm" | ||
| "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types" | ||
| ) | ||
|
|
||
| var ( | ||
| connectionCounterName = "my_network_filter.connection_counter" | ||
| counter proxywasm.MetricCounter | ||
| ) | ||
|
|
||
| func main() { | ||
| proxywasm.SetNewRootContext(newRootContext) | ||
| } | ||
|
|
||
| type rootContext struct { | ||
| // You'd better embed the default root context | ||
| // so that you don't need to reimplement all the methods by yourself. | ||
| proxywasm.DefaultRootContext | ||
| config string | ||
| } | ||
|
|
||
| func newRootContext(rootContextID uint32) proxywasm.RootContext { | ||
| return &rootContext{} | ||
| } | ||
|
|
||
| // Override proxywasm.DefaultRootContext | ||
| func (ctx *rootContext) OnPluginStart(configurationSize int) types.OnPluginStartStatus { | ||
| counter = proxywasm.DefineCounterMetric(connectionCounterName) | ||
|
|
||
| data, err := proxywasm.GetPluginConfiguration(configurationSize) | ||
| if err != nil && err != types.ErrorStatusNotFound { | ||
| proxywasm.LogCriticalf("failed to load config: %v", err) | ||
| return types.OnPluginStartStatusFailed | ||
| } | ||
| ctx.config = string(data) | ||
| return types.OnPluginStartStatusOK | ||
| } | ||
|
|
||
| // Override proxywasm.DefaultRootContext | ||
| func (ctx *rootContext) NewStreamContext(contextID uint32) proxywasm.StreamContext { | ||
| return &streamContext{newConnectionMessage: ctx.config} | ||
| } | ||
|
|
||
| type streamContext struct { | ||
| // You'd better embed the default stream context | ||
| // so that you don't need to reimplement all the methods by yourself. | ||
| proxywasm.DefaultStreamContext | ||
| newConnectionMessage string | ||
| } | ||
|
|
||
| // Override proxywasm.DefaultStreamContext | ||
| func (ctx *streamContext) OnNewConnection() types.Action { | ||
| proxywasm.LogInfo(ctx.newConnectionMessage) | ||
| return types.ActionContinue | ||
| } | ||
|
|
||
| // Override proxywasm.DefaultStreamContext | ||
| func (ctx *streamContext) OnStreamDone() { | ||
| counter.Increment(1) | ||
| proxywasm.LogInfof("connection complete!") | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.