Skip to content

Commit 4abe3c9

Browse files
committed
update tinygo sdk, add network filter category for tinygosdk
Signed-off-by: mathetake <[email protected]>
1 parent d7e3091 commit 4abe3c9

File tree

8 files changed

+98
-3
lines changed

8 files changed

+98
-3
lines changed

data/extension/init/templates/tinygo/envoy.filters.http/default/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ go 1.15
44

55
require (
66
github.com/stretchr/testify v1.6.1
7-
github.com/tetratelabs/proxy-wasm-go-sdk v0.0.1
7+
github.com/tetratelabs/proxy-wasm-go-sdk v0.0.2
88
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module envoy.filters.http
2+
3+
go 1.15
4+
5+
require (
6+
github.com/stretchr/testify v1.6.1
7+
github.com/tetratelabs/proxy-wasm-go-sdk v0.0.2
8+
)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
5+
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
6+
)
7+
8+
var (
9+
connectionCounterName = "proxy_wasm_go.connection_counter"
10+
counter proxywasm.MetricCounter
11+
)
12+
13+
type context struct{ proxywasm.DefaultContext }
14+
15+
func main() {
16+
proxywasm.SetNewRootContext(func(contextID uint32) proxywasm.RootContext { return context{} })
17+
proxywasm.SetNewStreamContext(func(contextID uint32) proxywasm.StreamContext { return context{} })
18+
}
19+
20+
func (ctx context) OnVMStart(int) bool {
21+
var err error
22+
counter, err = proxywasm.DefineCounterMetric(connectionCounterName)
23+
if err != nil {
24+
proxywasm.LogCritical("failed to initialize connection counter: ", err.Error())
25+
}
26+
return true
27+
}
28+
29+
func (ctx context) OnNewConnection() types.Action {
30+
proxywasm.LogInfo("new connection!")
31+
return types.ActionContinue
32+
}
33+
34+
func (ctx context) OnDone() bool {
35+
err := counter.Increment(1)
36+
if err != nil {
37+
proxywasm.LogCritical("failed to increment connection counter: ", err.Error())
38+
}
39+
proxywasm.LogInfo("connection complete!")
40+
return true
41+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
"github.com/tetratelabs/proxy-wasm-go-sdk/proxytest"
9+
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
10+
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
11+
)
12+
13+
func newStreamContext(uint32) proxywasm.StreamContext {
14+
return context{}
15+
}
16+
17+
func TestNetwork_OnNewConnection(t *testing.T) {
18+
host, done := proxytest.NewNetworkFilterHost(newStreamContext)
19+
defer done() // release the host emulation lock so that other test cases can insert their own host emulation
20+
21+
_ = host.InitConnection() // OnNewConnection is called
22+
23+
logs := host.GetLogs(types.LogLevelInfo) // retrieve logs emitted to Envoy
24+
assert.Equal(t, logs[0], "new connection!")
25+
}
26+
27+
func TestNetwork_counter(t *testing.T) {
28+
host, done := proxytest.NewNetworkFilterHost(newStreamContext)
29+
defer done() // release the host emulation lock so that other test cases can insert their own host emulation
30+
31+
context{}.OnVMStart(0) // init metric
32+
33+
contextID := host.InitConnection()
34+
host.CompleteConnection(contextID) // call OnDone on contextID -> increment the connection counter
35+
36+
logs := host.GetLogs(types.LogLevelInfo)
37+
require.Greater(t, len(logs), 0)
38+
39+
assert.Equal(t, "connection complete!", logs[len(logs)-1])
40+
actual, err := counter.Get()
41+
require.NoError(t, err)
42+
assert.Equal(t, uint64(1), actual)
43+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ require (
2929
github.com/tetratelabs/getenvoy-package v0.0.0-20190730071641-da31aed4333e
3030
github.com/tetratelabs/log v0.0.0-20190710134534-eb04d1e84fb8
3131
github.com/tetratelabs/multierror v1.1.0
32+
github.com/tetratelabs/proxy-wasm-go-sdk v0.0.2 // indirect
3233
gotest.tools v2.2.0+incompatible
3334
istio.io/api v0.0.0-20200227213531-891bf31f3c32
3435
istio.io/istio v0.0.0-20200304114959-c3c353285578

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,8 @@ github.com/tetratelabs/log v0.0.0-20190710134534-eb04d1e84fb8 h1:a7FN/XPymdzttMa
653653
github.com/tetratelabs/log v0.0.0-20190710134534-eb04d1e84fb8/go.mod h1:w+dEBsxcYEFg0I6whrgkMzjD8GBBQgmDq9hykB30pt8=
654654
github.com/tetratelabs/multierror v1.1.0 h1:cKmV/Pbf42K5wp8glxa2YIausbxIraPN8fzru9Pn1Cg=
655655
github.com/tetratelabs/multierror v1.1.0/go.mod h1:kH3SzI/z+FwEbV9bxQDx4GiIgE2djuyb8wiB2DaUBnY=
656+
github.com/tetratelabs/proxy-wasm-go-sdk v0.0.2 h1:nJVDdX6v/Mn0IbiogBv4cJvil4mHmBubML+3KZIWe/A=
657+
github.com/tetratelabs/proxy-wasm-go-sdk v0.0.2/go.mod h1:oriMCq3KvyEkgWVKr5B9DauvzpQ4Qy5eb32hxPa83Dw=
656658
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
657659
github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
658660
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=

images/extension-builders/tinygo/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ ENV GOMODCACHE=/source/.gomodcache
2828
ENV XDG_CACHE_HOME=/source/.cache
2929

3030
ENV TINYGO_SDK_NAME=github.com/tetratelabs/proxy-wasm-go-sdk
31-
ENV TINYGO_SDK_VERSION=v0.0.1
31+
ENV TINYGO_SDK_VERSION=v0.0.2
3232
ENV TINYGO_SDK_PATH=${GOMODCACHE}/${TINYGO_SDK_NAME}@${TINYGO_SDK_VERSION}
3333

3434
RUN mkdir -p ${TINYGO_SDK_PATH} && git clone https://${TINYGO_SDK_NAME} -b ${TINYGO_SDK_VERSION} ${TINYGO_SDK_PATH}

pkg/cmd/extension/init/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var (
3535
},
3636
extension.LanguageTinyGo.String(): {
3737
{Value: extension.EnvoyHTTPFilter.String(), DisplayText: "HTTP Filter"},
38-
// {Value: extension.EnvoyNetworkFilter.String(), DisplayText: "Network Filter"},
38+
{Value: extension.EnvoyNetworkFilter.String(), DisplayText: "Network Filter"},
3939
},
4040
}
4141
// SupportedLanguages ... programming languages supported by the `init` command.

0 commit comments

Comments
 (0)