Skip to content

Commit

Permalink
update tinygo sdk, add network filter category for tinygosdk
Browse files Browse the repository at this point in the history
Signed-off-by: mathetake <takeshi@tetrate.io>
  • Loading branch information
mathetake committed Sep 22, 2020
1 parent d7e3091 commit 5c60724
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion images/extension-builders/tinygo/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/extension/init/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 5c60724

Please sign in to comment.