Skip to content
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

Support TinyGo SDK for extension toolkit #104

Merged
merged 43 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
73a23a2
Add tinygo lang
mathetake Dec 22, 2020
11ee7a8
allow abi 0.1.0
mathetake Dec 22, 2020
d6f8eba
Fix e2e
mathetake Dec 22, 2020
ebb1b11
Fix e2e
mathetake Dec 22, 2020
9dce679
allow abi 0.1.0
mathetake Dec 22, 2020
fbc011b
Fix e2e
mathetake Dec 22, 2020
f53036f
Fix e2e
mathetake Dec 22, 2020
f4f46d3
fix e2e on mac
mathetake Dec 22, 2020
e3c77b0
fix e2e
mathetake Dec 22, 2020
29743bb
fix e2e
mathetake Dec 22, 2020
98e0f9e
fix e2e
mathetake Dec 22, 2020
e568706
Clean up e2e
mathetake Dec 22, 2020
54a9e7a
Run tinygo with 1.17.0
mathetake Jan 14, 2021
98ebe99
Merge branch 'tinygo-extension' of github.com:tetratelabs/getenvoy in…
mathetake Jan 14, 2021
3292cfd
Update tinygo builder
mathetake Jan 14, 2021
e046f90
Fix e2e
mathetake Jan 14, 2021
f4df444
Merge branch 'master' of github.com:tetratelabs/getenvoy into tinygo-…
mathetake Jan 17, 2021
62d5aa7
merge origin/master
mathetake Mar 8, 2021
25e93d8
Update TinyGo version
mathetake Mar 8, 2021
7afb2c2
Fix e2e
mathetake Mar 8, 2021
dc70c79
merge origin/master
mathetake Mar 8, 2021
d5e430c
review: fix styles
mathetake Mar 9, 2021
20e83c0
review: fix styles part 2
mathetake Mar 9, 2021
93b40f6
improve access logger example: reading config
mathetake Mar 9, 2021
f250078
improve http example: reading config
mathetake Mar 9, 2021
482909d
improve network example: reading config
mathetake Mar 9, 2021
2f146fd
Fix unittests
mathetake Mar 9, 2021
6f2e72c
Improve comments and namings
mathetake Mar 9, 2021
2a300e9
Remove left 'exec'
mathetake Mar 9, 2021
b1b223d
Remove unnecessary rm in clean command
mathetake Mar 9, 2021
bbb29b3
Reivew: use enumed OnPluginStart, host.GetCounterMetric
mathetake Mar 10, 2021
f75cff7
Add go mod download
mathetake Mar 10, 2021
f960379
Re: fix entrypoint
mathetake Mar 10, 2021
23e8fbc
Revert: add go.sum
mathetake Mar 10, 2021
7443281
Revert debug code
mathetake Mar 10, 2021
45a02f3
Add .gitignore, and set extension name in go.mod
mathetake Mar 12, 2021
b9024b0
default output to build/extension.wasm
mathetake Mar 12, 2021
436d507
Update SDK: fix error
mathetake Mar 12, 2021
b485f05
Fix permission on host
mathetake Mar 12, 2021
5ce9aa6
Fix unittest
mathetake Mar 12, 2021
b3c33e1
Fix comment
mathetake Mar 12, 2021
52b7944
Fix permission, showcase config in access logger
mathetake Mar 15, 2021
9715e55
Update SDK
mathetake Mar 15, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
ENVOY = standard:1.11.1
HUB ?= docker.io/getenvoy
GETENVOY_TAG ?= dev
BUILDERS_LANGS := rust
BUILDERS_LANGS := rust tinygo
BUILDERS_TAG ?= latest
EXTRA_TAG ?=

Expand Down Expand Up @@ -139,7 +139,7 @@ builder/$(1):
$(if $(USE_DOCKER_BUILDKIT_CACHE),--build-arg BUILDKIT_INLINE_CACHE=1,) \
$(if $(USE_DOCKER_BUILDKIT_CACHE),--cache-from $(call EXTENSION_BUILDER_IMAGE,$(1),$(EXTENSION_BUILDER_IMAGE_LATEST_VERSION)),) \
-t $(call EXTENSION_BUILDER_IMAGE,$(1),$(BUILDERS_TAG)) \
images/extension-builders/$(1)
-f images/extension-builders/$(1)/Dockerfile images/extension-builders
endef
$(foreach lang,$(BUILDERS_LANGS),$(eval $(call GEN_BUILD_EXTENSION_BUILDER_IMAGE_TARGET,$(lang))))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module my.filters.http
mathetake marked this conversation as resolved.
Show resolved Hide resolved

go 1.15

require (
github.com/stretchr/testify v1.6.1
github.com/tetratelabs/proxy-wasm-go-sdk v0.0.15
yskopets marked this conversation as resolved.
Show resolved Hide resolved
)
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=
mathetake marked this conversation as resolved.
Show resolved Hide resolved
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.15 h1:GDMoxdhqcksWXW7ynM2gtTmrWYHC+Op/SqFVL4Trglg=
github.com/tetratelabs/proxy-wasm-go-sdk v0.0.15/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=
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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 must embed the default context.
mathetake marked this conversation as resolved.
Show resolved Hide resolved
proxywasm.DefaultRootContext
logMessage string
}

func newAccessLogger(contextID uint32) proxywasm.RootContext {
return &accessLogger{}
}

// Override proxywasm.DefaultRootContext
func (l *accessLogger) OnPluginStart(configurationSize int) bool {
// 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 false
mathetake marked this conversation as resolved.
Show resolved Hide resolved
}
l.logMessage = string(data)
return true
mathetake marked this conversation as resolved.
Show resolved Hide resolved
}

// 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)

}
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")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module my.filters.http
mathetake marked this conversation as resolved.
Show resolved Hide resolved

go 1.15

require (
github.com/stretchr/testify v1.6.1
github.com/tetratelabs/proxy-wasm-go-sdk v0.0.15
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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.14 h1:13uKVfbtQJGshUqrGN7ujlJZwKYCS8N46NvBwKu5VvU=
github.com/tetratelabs/proxy-wasm-go-sdk v0.0.14/go.mod h1:y1ZQT4bQEBnR8Do4nSOzb3roczzPvcAp8UrF6NEYWNY=
github.com/tetratelabs/proxy-wasm-go-sdk v0.0.15 h1:GDMoxdhqcksWXW7ynM2gtTmrWYHC+Op/SqFVL4Trglg=
github.com/tetratelabs/proxy-wasm-go-sdk v0.0.15/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=
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
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 {
// we must embed the default context
mathetake marked this conversation as resolved.
Show resolved Hide resolved
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) bool {
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 false
}

// 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 true
}

// Override proxywasm.DefaultRootContext
func (ctx *rootContext) NewHttpContext(uint32) proxywasm.HttpContext {
return &httpContext{additionalHeaders: ctx.additionalHeaders}
}

type httpContext struct {
// You must embed the default context.
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 {
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)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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.
host.StartPlugin()

// Create http context.
contextID := host.InitializeHttpContext()

// Call OnHttpRequestHeaders with the given headers.
hs := types.Headers{
{"key1", "value1"},
{"key2", "value2"},
}
host.CallOnRequestHeaders(contextID, hs, false)

// Call OnHttpResponseHeaders.
host.CallOnResponseHeaders(contextID, nil, false)

// 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")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module my.filters.http
mathetake marked this conversation as resolved.
Show resolved Hide resolved

go 1.15

require (
github.com/stretchr/testify v1.6.1
github.com/tetratelabs/proxy-wasm-go-sdk v0.0.15
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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.14 h1:13uKVfbtQJGshUqrGN7ujlJZwKYCS8N46NvBwKu5VvU=
github.com/tetratelabs/proxy-wasm-go-sdk v0.0.14/go.mod h1:y1ZQT4bQEBnR8Do4nSOzb3roczzPvcAp8UrF6NEYWNY=
github.com/tetratelabs/proxy-wasm-go-sdk v0.0.15 h1:GDMoxdhqcksWXW7ynM2gtTmrWYHC+Op/SqFVL4Trglg=
github.com/tetratelabs/proxy-wasm-go-sdk v0.0.15/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=
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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 must embed the default context.
mathetake marked this conversation as resolved.
Show resolved Hide resolved
proxywasm.DefaultRootContext
config string
}

func newRootContext(rootContextID uint32) proxywasm.RootContext {
return &rootContext{}
}

// Override proxywasm.DefaultRootContext
func (ctx *rootContext) OnPluginStart(configurationSize int) bool {
counter = proxywasm.DefineCounterMetric(connectionCounterName)

data, err := proxywasm.GetPluginConfiguration(configurationSize)
if err != nil && err != types.ErrorStatusNotFound {
proxywasm.LogCriticalf("failed to load config: %v", err)
return false
}
ctx.config = string(data)
return true
}

// Override proxywasm.DefaultRootContext
func (ctx *rootContext) NewStreamContext(contextID uint32) proxywasm.StreamContext {
return &streamContext{newConnectionMessage: ctx.config}
}

type streamContext struct {
// You must embed the default context.
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!")
}
Loading