Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
rtribotte committed Jun 24, 2024
1 parent 3212495 commit 05be069
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 31 deletions.
2 changes: 1 addition & 1 deletion docs/content/reference/static-configuration/cli-ref.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ Local plugins configuration. (Default: ```false```)
Plugin's module name.

`--experimental.localplugins.<name>.settings`:
Plugin's settings.
Plugin's settings (works only for wasm plugins).

`--experimental.localplugins.<name>.settings.envs`:
Environment variables to forward to the wasm guest.
Expand Down
2 changes: 1 addition & 1 deletion docs/content/reference/static-configuration/env-ref.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ Local plugins configuration. (Default: ```false```)
Plugin's module name.

`TRAEFIK_EXPERIMENTAL_LOCALPLUGINS_<NAME>_SETTINGS`:
Plugin's settings.
Plugin's settings (works only for wasm plugins).

`TRAEFIK_EXPERIMENTAL_LOCALPLUGINS_<NAME>_SETTINGS_ENVS`:
Environment variables to forward to the wasm guest.
Expand Down
1 change: 1 addition & 0 deletions pkg/plugins/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func newMiddlewareBuilder(ctx context.Context, goPath string, manifest *Manifest
if err != nil {
return nil, fmt.Errorf("wasm path: %w", err)
}

return newWasmMiddlewareBuilder(goPath, moduleName, wasmPath, settings)

case runtimeYaegi, "":
Expand Down
19 changes: 10 additions & 9 deletions pkg/plugins/middlewarewasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func newWasmMiddlewareBuilder(goPath, moduleName, wasmPath string, settings Sett
}

rt := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig().WithCompilationCache(cache))
_, err = rt.CompileModule(ctx, code)
if err != nil {
return nil, fmt.Errorf("compiling Wasm binary: %w", err)
if _, err = rt.CompileModule(ctx, code); err != nil {
return nil, fmt.Errorf("compiling guest module: %w", err)
}

return &wasmMiddlewareBuilder{path: path, cache: cache, settings: settings}, nil
}

Expand All @@ -53,7 +53,7 @@ func (b wasmMiddlewareBuilder) newMiddleware(config map[string]interface{}, midd
func (b wasmMiddlewareBuilder) newHandler(ctx context.Context, next http.Handler, cfg reflect.Value, middlewareName string) (http.Handler, error) {
h, applyCtx, err := b.buildMiddleware(ctx, next, cfg, middlewareName)
if err != nil {
return nil, fmt.Errorf("building middleware: %w", err)
return nil, fmt.Errorf("building Wasm middleware: %w", err)
}

return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
Expand All @@ -64,18 +64,19 @@ func (b wasmMiddlewareBuilder) newHandler(ctx context.Context, next http.Handler
func (b *wasmMiddlewareBuilder) buildMiddleware(ctx context.Context, next http.Handler, cfg reflect.Value, middlewareName string) (http.Handler, func(ctx context.Context) context.Context, error) {
code, err := os.ReadFile(b.path)
if err != nil {
return nil, nil, fmt.Errorf("loading Wasm binary: %w", err)
return nil, nil, fmt.Errorf("loading binary: %w", err)
}

rt := host.NewRuntime(wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig().WithCompilationCache(b.cache)))
mod, err := rt.CompileModule(ctx, code)

guestModule, err := rt.CompileModule(ctx, code)
if err != nil {
return nil, nil, fmt.Errorf("compiling module: %w", err)
return nil, nil, fmt.Errorf("compiling guest module: %w", err)
}

applyCtx, err := Instantiate(ctx, rt, mod, b.settings)
applyCtx, err := InstantiateHost(ctx, rt, guestModule, b.settings)
if err != nil {
return nil, nil, fmt.Errorf("instantiating module: %w", err)
return nil, nil, fmt.Errorf("instantiating host module: %w", err)
}

logger := middlewares.GetLogger(ctx, middlewareName, "wasm")
Expand Down
7 changes: 5 additions & 2 deletions pkg/plugins/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ type Descriptor struct {
// Version (required)
Version string `description:"plugin's version." json:"version,omitempty" toml:"version,omitempty" yaml:"version,omitempty" export:"true"`

// Settings (optional)
Settings Settings `description:"Plugin's settings (works only for wasm plugins)." json:"settings,omitempty" toml:"settings,omitempty" yaml:"settings,omitempty" export:"true"`
}

// LocalDescriptor The static part of a local plugin configuration.
type LocalDescriptor struct {
// ModuleName (required)
ModuleName string `description:"Plugin's module name." json:"moduleName,omitempty" toml:"moduleName,omitempty" yaml:"moduleName,omitempty" export:"true"`
Settings Settings `description:"Plugin's settings." json:"settings,omitempty" toml:"settings,omitempty" yaml:"settings,omitempty" export:"true"`
ModuleName string `description:"Plugin's module name." json:"moduleName,omitempty" toml:"moduleName,omitempty" yaml:"moduleName,omitempty" export:"true"`

// Settings (optional)
Settings Settings `description:"Plugin's settings (works only for wasm plugins)." json:"settings,omitempty" toml:"settings,omitempty" yaml:"settings,omitempty" export:"true"`
}

// Manifest The plugin manifest.
Expand Down
17 changes: 8 additions & 9 deletions pkg/plugins/wasip.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ import (

type ContextApplier func(ctx context.Context) context.Context

func Instantiate(ctx context.Context, runtime wazero.Runtime, mod wazero.CompiledModule, settings Settings) (ContextApplier, error) {
socketsExtension := imports.DetectSocketsExtension(mod)
if socketsExtension != nil {
hostModule := wazergo_wasip1.NewHostModule(*socketsExtension)

builder := imports.NewBuilder().WithSocketsExtension("auto", mod)

var envs []string
// InstantiateHost instantiates the Host module according to the guest requirements (for now only SocketExtensions).
func InstantiateHost(ctx context.Context, runtime wazero.Runtime, mod wazero.CompiledModule, settings Settings) (ContextApplier, error) {
if extension := imports.DetectSocketsExtension(mod); extension != nil {
envs := []string{}
for _, env := range settings.Envs {
envs = append(envs, fmt.Sprintf("%s=%s", env, os.Getenv(env)))
}

builder := imports.NewBuilder().WithSocketsExtension("auto", mod)
if len(envs) > 0 {
builder.WithEnv(envs...)
}
Expand All @@ -40,7 +38,7 @@ func Instantiate(ctx context.Context, runtime wazero.Runtime, mod wazero.Compile
return nil, err
}

inst, err := wazergo.Instantiate(ctx, runtime, hostModule, wazergo_wasip1.WithWASI(sys))
inst, err := wazergo.Instantiate(ctx, runtime, wazergo_wasip1.NewHostModule(*extension), wazergo_wasip1.WithWASI(sys))
if err != nil {
return nil, fmt.Errorf("wazergo instantiation: %w", err)
}
Expand All @@ -54,6 +52,7 @@ func Instantiate(ctx context.Context, runtime wazero.Runtime, mod wazero.Compile
if err != nil {
return nil, fmt.Errorf("wazero instantiation: %w", err)
}

return func(ctx context.Context) context.Context {
return ctx
}, nil
Expand Down
3 changes: 2 additions & 1 deletion pkg/plugins/wasip_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (

type ContextApplier func(ctx context.Context) context.Context

func Instantiate(ctx context.Context, runtime wazero.Runtime, mod wazero.CompiledModule, settings Settings) (ContextApplier, error) {
// InstantiateHost instantiates the Host module.
func InstantiateHost(ctx context.Context, runtime wazero.Runtime, mod wazero.CompiledModule, settings Settings) (ContextApplier, error) {
return func(ctx context.Context) context.Context {
return ctx
}, nil
Expand Down
20 changes: 16 additions & 4 deletions pkg/redactor/redactor_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,22 +907,34 @@ func TestDo_staticConfiguration(t *testing.T) {
"Descriptor0": {
ModuleName: "foobar",
Version: "foobar",
Settings: plugins.Settings{},
Settings: plugins.Settings{
Envs: []string{"a", "b"},
Mounts: []string{"a", "b"},
},
},
"Descriptor1": {
ModuleName: "foobar",
Version: "foobar",
Settings: plugins.Settings{},
Settings: plugins.Settings{
Envs: []string{"a", "b"},
Mounts: []string{"a", "b"},
},
},
},
LocalPlugins: map[string]plugins.LocalDescriptor{
"Descriptor0": {
ModuleName: "foobar",
Settings: plugins.Settings{},
Settings: plugins.Settings{
Envs: []string{"a", "b"},
Mounts: []string{"a", "b"},
},
},
"Descriptor1": {
ModuleName: "foobar",
Settings: plugins.Settings{},
Settings: plugins.Settings{
Envs: []string{"a", "b"},
Mounts: []string{"a", "b"},
},
},
},
}
Expand Down
44 changes: 40 additions & 4 deletions pkg/redactor/testdata/anonymized-static-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -389,22 +389,58 @@
"Descriptor0": {
"moduleName": "foobar",
"version": "foobar",
"settings": {}
"settings": {
"envs": [
"xxxx",
"xxxx"
],
"mounts": [
"xxxx",
"xxxx"
]
}
},
"Descriptor1": {
"moduleName": "foobar",
"version": "foobar",
"settings": {}
"settings": {
"envs": [
"xxxx",
"xxxx"
],
"mounts": [
"xxxx",
"xxxx"
]
}
}
},
"localPlugins": {
"Descriptor0": {
"moduleName": "foobar",
"settings": {}
"settings": {
"envs": [
"xxxx",
"xxxx"
],
"mounts": [
"xxxx",
"xxxx"
]
}
},
"Descriptor1": {
"moduleName": "foobar",
"settings": {}
"settings": {
"envs": [
"xxxx",
"xxxx"
],
"mounts": [
"xxxx",
"xxxx"
]
}
}
}
}
Expand Down

0 comments on commit 05be069

Please sign in to comment.