diff --git a/data/extension/init/templates/tinygo/envoy.filters.http/default/go.mod b/data/extension/init/templates/tinygo/envoy.filters.http/default/go.mod index b28bfeec..17a45c3c 100644 --- a/data/extension/init/templates/tinygo/envoy.filters.http/default/go.mod +++ b/data/extension/init/templates/tinygo/envoy.filters.http/default/go.mod @@ -4,5 +4,5 @@ go 1.15 require ( github.com/stretchr/testify v1.6.1 - github.com/tetratelabs/proxy-wasm-go-sdk v0.0.1 + github.com/tetratelabs/proxy-wasm-go-sdk v0.0.2 ) diff --git a/data/extension/init/templates/tinygo/envoy.filters.network/default/go.mod b/data/extension/init/templates/tinygo/envoy.filters.network/default/go.mod new file mode 100644 index 00000000..17a45c3c --- /dev/null +++ b/data/extension/init/templates/tinygo/envoy.filters.network/default/go.mod @@ -0,0 +1,8 @@ +module envoy.filters.http + +go 1.15 + +require ( + github.com/stretchr/testify v1.6.1 + github.com/tetratelabs/proxy-wasm-go-sdk v0.0.2 +) diff --git a/data/extension/init/templates/tinygo/envoy.filters.network/default/main.go b/data/extension/init/templates/tinygo/envoy.filters.network/default/main.go new file mode 100644 index 00000000..d4896f09 --- /dev/null +++ b/data/extension/init/templates/tinygo/envoy.filters.network/default/main.go @@ -0,0 +1,55 @@ +// Copyright 2020 Tetrate +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm" + "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types" +) + +var ( + connectionCounterName = "proxy_wasm_go.connection_counter" + counter proxywasm.MetricCounter +) + +type context struct{ proxywasm.DefaultContext } + +func main() { + proxywasm.SetNewRootContext(func(contextID uint32) proxywasm.RootContext { return context{} }) + proxywasm.SetNewStreamContext(func(contextID uint32) proxywasm.StreamContext { return context{} }) +} + +func (ctx context) OnVMStart(int) bool { + var err error + counter, err = proxywasm.DefineCounterMetric(connectionCounterName) + if err != nil { + proxywasm.LogCritical("failed to initialize connection counter: ", err.Error()) + } + return true +} + +func (ctx context) OnNewConnection() types.Action { + proxywasm.LogInfo("new connection!") + return types.ActionContinue +} + +func (ctx context) OnDone() bool { + err := counter.Increment(1) + if err != nil { + proxywasm.LogCritical("failed to increment connection counter: ", err.Error()) + } + proxywasm.LogInfo("connection complete!") + return true +} diff --git a/data/extension/init/templates/tinygo/envoy.filters.network/default/main_test.go b/data/extension/init/templates/tinygo/envoy.filters.network/default/main_test.go new file mode 100644 index 00000000..fd3a9ad6 --- /dev/null +++ b/data/extension/init/templates/tinygo/envoy.filters.network/default/main_test.go @@ -0,0 +1,57 @@ +// Copyright 2020 Tetrate +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/tetratelabs/proxy-wasm-go-sdk/proxytest" + "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm" + "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types" +) + +func newStreamContext(uint32) proxywasm.StreamContext { + return context{} +} + +func TestNetwork_OnNewConnection(t *testing.T) { + host, done := proxytest.NewNetworkFilterHost(newStreamContext) + defer done() // release the host emulation lock so that other test cases can insert their own host emulation + + _ = host.InitConnection() // OnNewConnection is called + + logs := host.GetLogs(types.LogLevelInfo) // retrieve logs emitted to Envoy + assert.Equal(t, logs[0], "new connection!") +} + +func TestNetwork_counter(t *testing.T) { + host, done := proxytest.NewNetworkFilterHost(newStreamContext) + defer done() // release the host emulation lock so that other test cases can insert their own host emulation + + context{}.OnVMStart(0) // init metric + + contextID := host.InitConnection() + host.CompleteConnection(contextID) // call OnDone on contextID -> increment the connection counter + + logs := host.GetLogs(types.LogLevelInfo) + require.Greater(t, len(logs), 0) + + assert.Equal(t, "connection complete!", logs[len(logs)-1]) + actual, err := counter.Get() + require.NoError(t, err) + assert.Equal(t, uint64(1), actual) +} diff --git a/go.mod b/go.mod index 106449e3..fbf93e5a 100644 --- a/go.mod +++ b/go.mod @@ -29,6 +29,7 @@ require ( github.com/tetratelabs/getenvoy-package v0.0.0-20190730071641-da31aed4333e github.com/tetratelabs/log v0.0.0-20190710134534-eb04d1e84fb8 github.com/tetratelabs/multierror v1.1.0 + github.com/tetratelabs/proxy-wasm-go-sdk v0.0.2 // indirect gotest.tools v2.2.0+incompatible istio.io/api v0.0.0-20200227213531-891bf31f3c32 istio.io/istio v0.0.0-20200304114959-c3c353285578 diff --git a/go.sum b/go.sum index 6b439690..e73eaf94 100644 --- a/go.sum +++ b/go.sum @@ -653,6 +653,8 @@ github.com/tetratelabs/log v0.0.0-20190710134534-eb04d1e84fb8 h1:a7FN/XPymdzttMa github.com/tetratelabs/log v0.0.0-20190710134534-eb04d1e84fb8/go.mod h1:w+dEBsxcYEFg0I6whrgkMzjD8GBBQgmDq9hykB30pt8= github.com/tetratelabs/multierror v1.1.0 h1:cKmV/Pbf42K5wp8glxa2YIausbxIraPN8fzru9Pn1Cg= github.com/tetratelabs/multierror v1.1.0/go.mod h1:kH3SzI/z+FwEbV9bxQDx4GiIgE2djuyb8wiB2DaUBnY= +github.com/tetratelabs/proxy-wasm-go-sdk v0.0.2 h1:nJVDdX6v/Mn0IbiogBv4cJvil4mHmBubML+3KZIWe/A= +github.com/tetratelabs/proxy-wasm-go-sdk v0.0.2/go.mod h1:oriMCq3KvyEkgWVKr5B9DauvzpQ4Qy5eb32hxPa83Dw= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= diff --git a/images/extension-builders/tinygo/Dockerfile b/images/extension-builders/tinygo/Dockerfile index cd178008..7d22bd26 100644 --- a/images/extension-builders/tinygo/Dockerfile +++ b/images/extension-builders/tinygo/Dockerfile @@ -28,7 +28,7 @@ ENV GOMODCACHE=/source/.gomodcache ENV XDG_CACHE_HOME=/source/.cache ENV TINYGO_SDK_NAME=github.com/tetratelabs/proxy-wasm-go-sdk -ENV TINYGO_SDK_VERSION=v0.0.1 +ENV TINYGO_SDK_VERSION=v0.0.2 ENV TINYGO_SDK_PATH=${GOMODCACHE}/${TINYGO_SDK_NAME}@${TINYGO_SDK_VERSION} RUN mkdir -p ${TINYGO_SDK_PATH} && git clone https://${TINYGO_SDK_NAME} -b ${TINYGO_SDK_VERSION} ${TINYGO_SDK_PATH} diff --git a/pkg/cmd/extension/init/cmd.go b/pkg/cmd/extension/init/cmd.go index ab19d460..78b9eaca 100644 --- a/pkg/cmd/extension/init/cmd.go +++ b/pkg/cmd/extension/init/cmd.go @@ -35,7 +35,7 @@ var ( }, extension.LanguageTinyGo.String(): { {Value: extension.EnvoyHTTPFilter.String(), DisplayText: "HTTP Filter"}, - // {Value: extension.EnvoyNetworkFilter.String(), DisplayText: "Network Filter"}, + {Value: extension.EnvoyNetworkFilter.String(), DisplayText: "Network Filter"}, }, } // SupportedLanguages ... programming languages supported by the `init` command.