Skip to content

Commit 715c563

Browse files
authored
Introduce VMContext, rename RootContext -> PluginContext. (tetratelabs#187)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
1 parent 530fc81 commit 715c563

56 files changed

Lines changed: 663 additions & 727 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,32 @@ import (
1313
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
1414
)
1515

16-
var counter proxywasm.MetricCounter
16+
type vmContext struct{}
1717

18-
type metricRootContext struct { types.DefaultRootContext }
18+
// Implement types.VMContext.
19+
func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext {
20+
return &pluginContext{}
21+
}
1922

20-
func (ctx *metricRootContext) OnVMStart(int) types.OnVMStartStatus {
21-
// Initialize the metric.
22-
counter = proxywasm.DefineCounterMetric("proxy_wasm_go.request_counter")
23-
return types.OnVMStartStatusOK
23+
type pluginContext struct {
24+
types.DefaultPluginContext
25+
counter proxywasm.MetricCounter
2426
}
2527

26-
func (*metricRootContext) NewHttpContext(contextID uint32) types.HttpContext {
27-
return &metricHttpContext{}
28+
// Implement types.PluginContext.
29+
func (*pluginContext) NewHttpContext(contextID uint32) types.HttpContext {
30+
return &httpContext{counter: proxywasm.DefineCounterMetric("proxy_wasm_go.request_counter")}
2831
}
2932

30-
type metricHttpContext struct { types.DefaultHttpContext }
33+
type httpContext struct {
34+
types.DefaultHttpContext
35+
counter proxywasm.MetricCounter
36+
}
3137

32-
func (ctx *metricHttpContext) OnHttpRequestHeaders(int, bool) types.Action {
38+
// Implement types.HttpContext.
39+
func (ctx *httpContext) OnHttpRequestHeaders(int, bool) types.Action {
3340
// Increment the request counter when we receive request headers.
34-
counter.Increment(1)
41+
ctx.counter.Increment(1)
3542
return types.ActionContinue
3643
}
3744
```
@@ -47,30 +54,21 @@ Please follow the official instruction [here](https://tinygo.org/getting-started
4754
| proxy-wasm-go-sdk| proxy-wasm ABI version |istio/proxyv2| Envoy upstream|
4855
|:-------------:|:-------------:|:-------------:|:-------------:|
4956
| main | 0.2.0| 1.9, 1.10 | 1.18 |
50-
| v0.2.0 | 0.2.0| 1.8, 1.9 | 1.17 |
57+
| v0.3.0 | 0.2.0| 1.8, 1.9 | 1.17 |
5158

52-
## Run examples
53-
54-
build:
59+
## Development
5560

5661
```bash
5762
# Build all examples.
5863
make build.examples
5964

6065
# Build a specific example.
6166
make build.example name=helloworld
62-
```
6367

64-
run:
65-
66-
```bash
68+
# Run a specific example.
6769
# This requires you to have Envoy binary locally.
6870
make run name=helloworld
69-
```
7071

71-
## SDK development
72-
73-
```bash
7472
# Run local tests without running envoy processes.
7573
make test
7674

e2e/e2e_test.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,6 @@ import (
2626
"github.com/stretchr/testify/require"
2727
)
2828

29-
func Test_configuration_from_root(t *testing.T) {
30-
stdErr, kill := startEnvoy(t, 8001)
31-
defer kill()
32-
require.Eventually(t, func() bool {
33-
res, err := http.Get("http://localhost:18000")
34-
if err != nil {
35-
return false
36-
}
37-
defer res.Body.Close()
38-
return res.StatusCode == http.StatusOK
39-
}, 5*time.Second, time.Millisecond, "Endpoint not healthy.")
40-
require.Eventually(t, func() bool {
41-
return checkMessage(stdErr.String(), []string{
42-
"plugin config from root context",
43-
"name\": \"plugin configuration",
44-
}, nil)
45-
}, 5*time.Second, time.Millisecond, stdErr.String())
46-
}
47-
4829
func Test_dispatch_call_on_tick(t *testing.T) {
4930
stdErr, kill := startEnvoy(t, 8001)
5031
defer kill()

examples/configuration_from_root/README.md

Lines changed: 0 additions & 10 deletions
This file was deleted.

examples/configuration_from_root/envoy.yaml

Lines changed: 0 additions & 51 deletions
This file was deleted.

examples/configuration_from_root/main.go

Lines changed: 0 additions & 58 deletions
This file was deleted.

examples/configuration_from_root/main_test.go

Lines changed: 0 additions & 48 deletions
This file was deleted.

examples/dispatch_call_on_tick/main.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,40 @@ import (
2222
const tickMilliseconds uint32 = 1
2323

2424
func main() {
25-
proxywasm.SetNewRootContextFn(newRootContext)
25+
proxywasm.SetVMContext(&vmContext{})
2626
}
2727

28-
type rootContext struct {
28+
type vmContext struct{}
29+
30+
// Implement types.VMContext.
31+
func (*vmContext) OnVMStart(vmConfigurationSize int) types.OnVMStartStatus {
32+
return types.OnVMStartStatusOK
33+
}
34+
35+
// Implement types.VMContext.
36+
func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext {
37+
return &pluginContext{contextID: contextID}
38+
}
39+
40+
type pluginContext struct {
2941
// Embed the default root context here,
3042
// so that we don't need to reimplement all the methods.
31-
types.DefaultRootContext
43+
types.DefaultPluginContext
3244
contextID uint32
3345
}
3446

35-
func newRootContext(contextID uint32) types.RootContext {
36-
return &rootContext{contextID: contextID}
37-
}
38-
39-
// Override DefaultRootContext.
40-
func (ctx *rootContext) OnVMStart(vmConfigurationSize int) types.OnVMStartStatus {
47+
// Override DefaultPluginContext.
48+
func (ctx *pluginContext) OnPluginStart(vmConfigurationSize int) types.OnPluginStartStatus {
4149
if err := proxywasm.SetTickPeriodMilliSeconds(tickMilliseconds); err != nil {
4250
proxywasm.LogCriticalf("failed to set tick period: %v", err)
43-
return types.OnVMStartStatusFailed
51+
return types.OnPluginStartStatusFailed
4452
}
4553
proxywasm.LogInfof("set tick period milliseconds: %d", tickMilliseconds)
46-
return types.OnVMStartStatusOK
54+
return types.OnPluginStartStatusOK
4755
}
4856

49-
// Override DefaultRootContext.
50-
func (ctx *rootContext) OnTick() {
57+
// Override DefaultPluginContext.
58+
func (ctx *pluginContext) OnTick() {
5159
hs := [][2]string{
5260
{":method", "GET"}, {":authority", "some_authority"}, {":path", "/path/to/service"}, {"accept", "*/*"},
5361
}

examples/dispatch_call_on_tick/main_test.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,18 @@ import (
1010
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
1111
)
1212

13-
func TestRootContext_OnTick(t *testing.T) {
14-
opt := proxytest.NewEmulatorOption().
15-
WithNewRootContext(newRootContext)
13+
func TestPluginContext_OnTick(t *testing.T) {
14+
opt := proxytest.NewEmulatorOption().WithVMContext(&vmContext{})
1615
host, reset := proxytest.NewHostEmulator(opt)
1716
defer reset()
1817

1918
// Call OnVMStart.
20-
require.Equal(t, types.OnVMStartStatusOK, host.StartVM())
19+
require.Equal(t, types.OnPluginStartStatusOK, host.StartPlugin())
2120
require.Equal(t, tickMilliseconds, host.GetTickPeriod())
2221

2322
for i := 1; i < 10; i++ {
2423
host.Tick() // call OnTick
25-
attrs := host.GetCalloutAttributesFromContext(proxytest.RootContextID)
24+
attrs := host.GetCalloutAttributesFromContext(proxytest.PluginContextID)
2625
// Verify DispatchHttpCall is called
2726
require.Equal(t, len(attrs), i)
2827
// Receive callout response.
@@ -34,13 +33,12 @@ func TestRootContext_OnTick(t *testing.T) {
3433

3534
}
3635

37-
func TestRootContext_OnVMStart(t *testing.T) {
38-
opt := proxytest.NewEmulatorOption().
39-
WithNewRootContext(newRootContext)
36+
func TestPluginContext_OnVMStart(t *testing.T) {
37+
opt := proxytest.NewEmulatorOption().WithVMContext(&vmContext{})
4038
host, reset := proxytest.NewHostEmulator(opt)
4139
defer reset()
4240

4341
// Call OnVMStart.
44-
require.Equal(t, types.OnVMStartStatusOK, host.StartVM())
42+
require.Equal(t, types.OnPluginStartStatusOK, host.StartPlugin())
4543
require.Equal(t, tickMilliseconds, host.GetTickPeriod())
4644
}

0 commit comments

Comments
 (0)