mirror of https://github.com/helm/helm
Merge pull request #31194 from gjenkins8/gjenkins/plugin-integration/wasm_runtime
[HIP-0026] Plugin extism/v1 runtimepull/31210/head
commit
892e86182f
@ -0,0 +1,100 @@
|
|||||||
|
/*
|
||||||
|
Copyright The Helm Authors.
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
This file contains a "registry" of supported plugin types.
|
||||||
|
|
||||||
|
It enables "dyanmic" operations on the go type associated with a given plugin type (see: `helm.sh/helm/v4/internal/plugin/schema` package)
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
// Create a new instance of the output message type for a given plugin type:
|
||||||
|
|
||||||
|
pluginType := "cli/v1" // for example
|
||||||
|
ptm, ok := pluginTypesIndex[pluginType]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("unknown plugin type %q", pluginType)
|
||||||
|
}
|
||||||
|
|
||||||
|
outputMessageType := reflect.Zero(ptm.outputType).Interface()
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
// Create a new instance of the config type for a given plugin type
|
||||||
|
|
||||||
|
pluginType := "cli/v1" // for example
|
||||||
|
ptm, ok := pluginTypesIndex[pluginType]
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
config := reflect.New(ptm.configType).Interface().(Config) // `config` is variable of type `Config`, with
|
||||||
|
|
||||||
|
// validate
|
||||||
|
err := config.Validate()
|
||||||
|
if err != nil { // handle error }
|
||||||
|
|
||||||
|
// assert to concrete type if needed
|
||||||
|
cliConfig := config.(*schema.ConfigCLIV1)
|
||||||
|
|
||||||
|
```
|
||||||
|
*/
|
||||||
|
|
||||||
|
package plugin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"helm.sh/helm/v4/internal/plugin/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
type pluginTypeMeta struct {
|
||||||
|
pluginType string
|
||||||
|
inputType reflect.Type
|
||||||
|
outputType reflect.Type
|
||||||
|
configType reflect.Type
|
||||||
|
}
|
||||||
|
|
||||||
|
var pluginTypes = []pluginTypeMeta{
|
||||||
|
{
|
||||||
|
pluginType: "test/v1",
|
||||||
|
inputType: reflect.TypeOf(schema.InputMessageTestV1{}),
|
||||||
|
outputType: reflect.TypeOf(schema.OutputMessageTestV1{}),
|
||||||
|
configType: reflect.TypeOf(schema.ConfigTestV1{}),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pluginType: "cli/v1",
|
||||||
|
inputType: reflect.TypeOf(schema.InputMessageCLIV1{}),
|
||||||
|
outputType: reflect.TypeOf(schema.OutputMessageCLIV1{}),
|
||||||
|
configType: reflect.TypeOf(ConfigCLI{}),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pluginType: "getter/v1",
|
||||||
|
inputType: reflect.TypeOf(schema.InputMessageGetterV1{}),
|
||||||
|
outputType: reflect.TypeOf(schema.OutputMessageGetterV1{}),
|
||||||
|
configType: reflect.TypeOf(ConfigGetter{}),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var pluginTypesIndex = func() map[string]*pluginTypeMeta {
|
||||||
|
result := make(map[string]*pluginTypeMeta, len(pluginTypes))
|
||||||
|
for _, m := range pluginTypes {
|
||||||
|
result[m.pluginType] = &m
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}()
|
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
Copyright The Helm Authors.
|
||||||
|
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 plugin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"helm.sh/helm/v4/internal/plugin/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMakeOutputMessage(t *testing.T) {
|
||||||
|
ptm := pluginTypesIndex["getter/v1"]
|
||||||
|
outputType := reflect.Zero(ptm.outputType).Interface()
|
||||||
|
assert.IsType(t, schema.OutputMessageGetterV1{}, outputType)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMakeConfig(t *testing.T) {
|
||||||
|
ptm := pluginTypesIndex["getter/v1"]
|
||||||
|
config := reflect.New(ptm.configType).Interface().(Config)
|
||||||
|
assert.IsType(t, &ConfigGetter{}, config)
|
||||||
|
}
|
@ -0,0 +1,292 @@
|
|||||||
|
/*
|
||||||
|
Copyright The Helm Authors.
|
||||||
|
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 plugin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
extism "github.com/extism/go-sdk"
|
||||||
|
"github.com/tetratelabs/wazero"
|
||||||
|
)
|
||||||
|
|
||||||
|
const ExtismV1WasmBinaryFilename = "plugin.wasm"
|
||||||
|
|
||||||
|
// RuntimeConfigExtismV1Memory exposes the Wasm/Extism memory options for the plugin
|
||||||
|
type RuntimeConfigExtismV1Memory struct {
|
||||||
|
// The max amount of pages the plugin can allocate
|
||||||
|
// One page is 64Kib. e.g. 16 pages would require 1MiB.
|
||||||
|
// Default is 4 pages (256KiB)
|
||||||
|
MaxPages uint32 `yaml:"maxPages,omitempty"`
|
||||||
|
|
||||||
|
// The max size of an Extism HTTP response in bytes
|
||||||
|
// Default is 4096 bytes (4KiB)
|
||||||
|
MaxHTTPResponseBytes int64 `yaml:"maxHttpResponseBytes,omitempty"`
|
||||||
|
|
||||||
|
// The max size of all Extism vars in bytes
|
||||||
|
// Default is 4096 bytes (4KiB)
|
||||||
|
MaxVarBytes int64 `yaml:"maxVarBytes,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RuntimeConfigExtismV1FileSystem exposes filesystem options for the configuration
|
||||||
|
// TODO: should Helm expose AllowedPaths?
|
||||||
|
type RuntimeConfigExtismV1FileSystem struct {
|
||||||
|
// If specified, a temporary directory will be created and mapped to /tmp in the plugin's filesystem.
|
||||||
|
// Data written to the directory will be visible on the host filesystem.
|
||||||
|
// The directory will be removed when the plugin invocation completes.
|
||||||
|
CreateTempDir bool `yaml:"createTempDir,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// RuntimeConfigExtismV1 defines the user-configurable options the plugin's Extism runtime
|
||||||
|
// The format loosely follows the Extism Manifest format: https://extism.org/docs/concepts/manifest/
|
||||||
|
type RuntimeConfigExtismV1 struct {
|
||||||
|
// Describes the limits on the memory the plugin may be allocated.
|
||||||
|
Memory RuntimeConfigExtismV1Memory `yaml:"memory"`
|
||||||
|
|
||||||
|
// The "config" key is a free-form map that can be passed to the plugin.
|
||||||
|
// The plugin must interpret arbitrary data this map may contain
|
||||||
|
Config map[string]string `yaml:"config,omitempty"`
|
||||||
|
|
||||||
|
// An optional set of hosts this plugin can communicate with.
|
||||||
|
// This only has an effect if the plugin makes HTTP requests.
|
||||||
|
// If not specified, then no hosts are allowed.
|
||||||
|
AllowedHosts []string `yaml:"allowedHosts,omitempty"`
|
||||||
|
|
||||||
|
FileSystem RuntimeConfigExtismV1FileSystem `yaml:"fileSystem,omitempty"`
|
||||||
|
|
||||||
|
// The timeout in milliseconds for the plugin to execute
|
||||||
|
Timeout uint64 `yaml:"timeout,omitempty"`
|
||||||
|
|
||||||
|
// HostFunction names exposed in Helm the plugin may access
|
||||||
|
// see: https://extism.org/docs/concepts/host-functions/
|
||||||
|
HostFunctions []string `yaml:"hostFunctions,omitempty"`
|
||||||
|
|
||||||
|
// The name of entry function name to call in the plugin
|
||||||
|
// Defaults to "helm_plugin_main".
|
||||||
|
EntryFuncName string `yaml:"entryFuncName,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ RuntimeConfig = (*RuntimeConfigExtismV1)(nil)
|
||||||
|
|
||||||
|
func (r *RuntimeConfigExtismV1) Validate() error {
|
||||||
|
// TODO
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type RuntimeExtismV1 struct {
|
||||||
|
HostFunctions map[string]extism.HostFunction
|
||||||
|
CompilationCache wazero.CompilationCache
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ Runtime = (*RuntimeExtismV1)(nil)
|
||||||
|
|
||||||
|
func (r *RuntimeExtismV1) CreatePlugin(pluginDir string, metadata *Metadata) (Plugin, error) {
|
||||||
|
|
||||||
|
rc, ok := metadata.RuntimeConfig.(*RuntimeConfigExtismV1)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("invalid extism/v1 plugin runtime config type: %T", metadata.RuntimeConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
wasmFile := filepath.Join(pluginDir, ExtismV1WasmBinaryFilename)
|
||||||
|
if _, err := os.Stat(wasmFile); err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return nil, fmt.Errorf("wasm binary missing for extism/v1 plugin: %q", wasmFile)
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("failed to stat extism/v1 plugin wasm binary %q: %w", wasmFile, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ExtismV1PluginRuntime{
|
||||||
|
metadata: *metadata,
|
||||||
|
dir: pluginDir,
|
||||||
|
rc: rc,
|
||||||
|
r: r,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type ExtismV1PluginRuntime struct {
|
||||||
|
metadata Metadata
|
||||||
|
dir string
|
||||||
|
rc *RuntimeConfigExtismV1
|
||||||
|
r *RuntimeExtismV1
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ Plugin = (*ExtismV1PluginRuntime)(nil)
|
||||||
|
|
||||||
|
func (p *ExtismV1PluginRuntime) Metadata() Metadata {
|
||||||
|
return p.metadata
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *ExtismV1PluginRuntime) Dir() string {
|
||||||
|
return p.dir
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *ExtismV1PluginRuntime) Invoke(ctx context.Context, input *Input) (*Output, error) {
|
||||||
|
|
||||||
|
var tmpDir string
|
||||||
|
if p.rc.FileSystem.CreateTempDir {
|
||||||
|
tmpDirInner, err := os.MkdirTemp(os.TempDir(), "helm-plugin-*")
|
||||||
|
slog.Debug("created plugin temp dir", slog.String("dir", tmpDirInner), slog.String("plugin", p.metadata.Name))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create temp dir for extism compilation cache: %w", err)
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if err := os.RemoveAll(tmpDir); err != nil {
|
||||||
|
slog.Warn("failed to remove plugin temp dir", slog.String("dir", tmpDir), slog.String("plugin", p.metadata.Name), slog.String("error", err.Error()))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
tmpDir = tmpDirInner
|
||||||
|
}
|
||||||
|
|
||||||
|
manifest, err := buildManifest(p.dir, tmpDir, p.rc)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
config := buildPluginConfig(input, p.r)
|
||||||
|
|
||||||
|
hostFunctions, err := buildHostFunctions(p.r.HostFunctions, p.rc)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
pe, err := extism.NewPlugin(ctx, manifest, config, hostFunctions)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create existing plugin: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
pe.SetLogger(func(logLevel extism.LogLevel, s string) {
|
||||||
|
slog.Debug(s, slog.String("level", logLevel.String()), slog.String("plugin", p.metadata.Name))
|
||||||
|
})
|
||||||
|
|
||||||
|
inputData, err := json.Marshal(input.Message)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to json marshal plugin input message: %T: %w", input.Message, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Debug("plugin input", slog.String("plugin", p.metadata.Name), slog.String("inputData", string(inputData)))
|
||||||
|
|
||||||
|
entryFuncName := p.rc.EntryFuncName
|
||||||
|
if entryFuncName == "" {
|
||||||
|
entryFuncName = "helm_plugin_main"
|
||||||
|
}
|
||||||
|
|
||||||
|
exitCode, outputData, err := pe.Call(entryFuncName, inputData)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("plugin error: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if exitCode != 0 {
|
||||||
|
return nil, &InvokeExecError{
|
||||||
|
Code: int(exitCode),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Debug("plugin output", slog.String("plugin", p.metadata.Name), slog.Int("exitCode", int(exitCode)), slog.String("outputData", string(outputData)))
|
||||||
|
|
||||||
|
outputMessage := reflect.New(pluginTypesIndex[p.metadata.Type].outputType)
|
||||||
|
if err := json.Unmarshal(outputData, outputMessage.Interface()); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to json marshal plugin output message: %T: %w", outputMessage, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
output := &Output{
|
||||||
|
Message: outputMessage.Elem().Interface(),
|
||||||
|
}
|
||||||
|
|
||||||
|
return output, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildManifest(pluginDir string, tmpDir string, rc *RuntimeConfigExtismV1) (extism.Manifest, error) {
|
||||||
|
wasmFile := filepath.Join(pluginDir, ExtismV1WasmBinaryFilename)
|
||||||
|
|
||||||
|
allowedHosts := rc.AllowedHosts
|
||||||
|
if allowedHosts == nil {
|
||||||
|
allowedHosts = []string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
allowedPaths := map[string]string{}
|
||||||
|
if tmpDir != "" {
|
||||||
|
allowedPaths[tmpDir] = "/tmp"
|
||||||
|
}
|
||||||
|
|
||||||
|
return extism.Manifest{
|
||||||
|
Wasm: []extism.Wasm{
|
||||||
|
extism.WasmFile{
|
||||||
|
Path: wasmFile,
|
||||||
|
Name: wasmFile,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Memory: &extism.ManifestMemory{
|
||||||
|
MaxPages: rc.Memory.MaxPages,
|
||||||
|
MaxHttpResponseBytes: rc.Memory.MaxHTTPResponseBytes,
|
||||||
|
MaxVarBytes: rc.Memory.MaxVarBytes,
|
||||||
|
},
|
||||||
|
Config: rc.Config,
|
||||||
|
AllowedHosts: allowedHosts,
|
||||||
|
AllowedPaths: allowedPaths,
|
||||||
|
Timeout: rc.Timeout,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildPluginConfig(input *Input, r *RuntimeExtismV1) extism.PluginConfig {
|
||||||
|
mc := wazero.NewModuleConfig().
|
||||||
|
WithSysWalltime()
|
||||||
|
if input.Stdin != nil {
|
||||||
|
mc = mc.WithStdin(input.Stdin)
|
||||||
|
}
|
||||||
|
if input.Stdout != nil {
|
||||||
|
mc = mc.WithStdout(input.Stdout)
|
||||||
|
}
|
||||||
|
if input.Stderr != nil {
|
||||||
|
mc = mc.WithStderr(input.Stderr)
|
||||||
|
}
|
||||||
|
if len(input.Env) > 0 {
|
||||||
|
env := parseEnv(input.Env)
|
||||||
|
for k, v := range env {
|
||||||
|
mc = mc.WithEnv(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
config := extism.PluginConfig{
|
||||||
|
ModuleConfig: mc,
|
||||||
|
RuntimeConfig: wazero.NewRuntimeConfigCompiler().
|
||||||
|
WithCloseOnContextDone(true).
|
||||||
|
WithCompilationCache(r.CompilationCache),
|
||||||
|
EnableWasi: true,
|
||||||
|
EnableHttpResponseHeaders: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildHostFunctions(hostFunctions map[string]extism.HostFunction, rc *RuntimeConfigExtismV1) ([]extism.HostFunction, error) {
|
||||||
|
result := make([]extism.HostFunction, len(rc.HostFunctions))
|
||||||
|
for _, fnName := range rc.HostFunctions {
|
||||||
|
fn, ok := hostFunctions[fnName]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("plugin requested host function %q not found", fnName)
|
||||||
|
}
|
||||||
|
|
||||||
|
result = append(result, fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
@ -0,0 +1,124 @@
|
|||||||
|
/*
|
||||||
|
Copyright The Helm Authors.
|
||||||
|
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 plugin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
extism "github.com/extism/go-sdk"
|
||||||
|
|
||||||
|
"helm.sh/helm/v4/internal/plugin/schema"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
type pluginRaw struct {
|
||||||
|
Metadata Metadata
|
||||||
|
Dir string
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildLoadExtismPlugin(t *testing.T, dir string) pluginRaw {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
pluginFile := filepath.Join(dir, PluginFileName)
|
||||||
|
|
||||||
|
metadataData, err := os.ReadFile(pluginFile)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
m, err := loadMetadata(metadataData)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, "extism/v1", m.Runtime, "expected plugin runtime to be extism/v1")
|
||||||
|
|
||||||
|
cmd := exec.Command("make", "-C", dir)
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
require.NoError(t, cmd.Run(), "failed to build plugin in %q", dir)
|
||||||
|
|
||||||
|
return pluginRaw{
|
||||||
|
Metadata: *m,
|
||||||
|
Dir: dir,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRuntimeConfigExtismV1Validate(t *testing.T) {
|
||||||
|
rc := RuntimeConfigExtismV1{}
|
||||||
|
err := rc.Validate()
|
||||||
|
assert.NoError(t, err, "expected no error for empty RuntimeConfigExtismV1")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRuntimeExtismV1InvokePlugin(t *testing.T) {
|
||||||
|
r := RuntimeExtismV1{}
|
||||||
|
|
||||||
|
pr := buildLoadExtismPlugin(t, "testdata/src/extismv1-test")
|
||||||
|
require.Equal(t, "test/v1", pr.Metadata.Type)
|
||||||
|
|
||||||
|
p, err := r.CreatePlugin(pr.Dir, &pr.Metadata)
|
||||||
|
|
||||||
|
assert.NoError(t, err, "expected no error creating plugin")
|
||||||
|
assert.NotNil(t, p, "expected plugin to be created")
|
||||||
|
|
||||||
|
output, err := p.Invoke(t.Context(), &Input{
|
||||||
|
Message: schema.InputMessageTestV1{
|
||||||
|
Name: "Phippy",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
msg := output.Message.(schema.OutputMessageTestV1)
|
||||||
|
assert.Equal(t, "Hello, Phippy! (6)", msg.Greeting)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildManifest(t *testing.T) {
|
||||||
|
rc := &RuntimeConfigExtismV1{
|
||||||
|
Memory: RuntimeConfigExtismV1Memory{
|
||||||
|
MaxPages: 8,
|
||||||
|
MaxHTTPResponseBytes: 81920,
|
||||||
|
MaxVarBytes: 8192,
|
||||||
|
},
|
||||||
|
FileSystem: RuntimeConfigExtismV1FileSystem{
|
||||||
|
CreateTempDir: true,
|
||||||
|
},
|
||||||
|
Config: map[string]string{"CONFIG_KEY": "config_value"},
|
||||||
|
AllowedHosts: []string{"example.com", "api.example.com"},
|
||||||
|
Timeout: 5000,
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := extism.Manifest{
|
||||||
|
Wasm: []extism.Wasm{
|
||||||
|
extism.WasmFile{
|
||||||
|
Path: "/path/to/plugin/plugin.wasm",
|
||||||
|
Name: "/path/to/plugin/plugin.wasm",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Memory: &extism.ManifestMemory{
|
||||||
|
MaxPages: 8,
|
||||||
|
MaxHttpResponseBytes: 81920,
|
||||||
|
MaxVarBytes: 8192,
|
||||||
|
},
|
||||||
|
Config: map[string]string{"CONFIG_KEY": "config_value"},
|
||||||
|
AllowedHosts: []string{"example.com", "api.example.com"},
|
||||||
|
AllowedPaths: map[string]string{"/tmp/foo": "/tmp"},
|
||||||
|
Timeout: 5000,
|
||||||
|
}
|
||||||
|
|
||||||
|
manifest, err := buildManifest("/path/to/plugin", "/tmp/foo", rc)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, expected, manifest)
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
/*
|
||||||
|
Copyright The Helm Authors.
|
||||||
|
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 plugin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseEnv(t *testing.T) {
|
||||||
|
type testCase struct {
|
||||||
|
env []string
|
||||||
|
expected map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
testCases := map[string]testCase{
|
||||||
|
"empty": {
|
||||||
|
env: []string{},
|
||||||
|
expected: map[string]string{},
|
||||||
|
},
|
||||||
|
"single": {
|
||||||
|
env: []string{"KEY=value"},
|
||||||
|
expected: map[string]string{"KEY": "value"},
|
||||||
|
},
|
||||||
|
"multiple": {
|
||||||
|
env: []string{"KEY1=value1", "KEY2=value2"},
|
||||||
|
expected: map[string]string{"KEY1": "value1", "KEY2": "value2"},
|
||||||
|
},
|
||||||
|
"no_value": {
|
||||||
|
env: []string{"KEY1=value1", "KEY2="},
|
||||||
|
expected: map[string]string{"KEY1": "value1", "KEY2": ""},
|
||||||
|
},
|
||||||
|
"duplicate_keys": {
|
||||||
|
env: []string{"KEY=value1", "KEY=value2"},
|
||||||
|
expected: map[string]string{"KEY": "value2"}, // last value should overwrite
|
||||||
|
},
|
||||||
|
"empty_strings": {
|
||||||
|
env: []string{"", "KEY=value", ""},
|
||||||
|
expected: map[string]string{"KEY": "value"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, tc := range testCases {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
result := parseEnv(tc.env)
|
||||||
|
assert.Equal(t, tc.expected, result)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
Copyright The Helm Authors.
|
||||||
|
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 schema
|
||||||
|
|
||||||
|
type InputMessageTestV1 struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
type OutputMessageTestV1 struct {
|
||||||
|
Greeting string
|
||||||
|
}
|
||||||
|
|
||||||
|
type ConfigTestV1 struct{}
|
||||||
|
|
||||||
|
func (c *ConfigTestV1) Validate() error {
|
||||||
|
return nil
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
plugin.wasm
|
@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
.DEFAULT: build
|
||||||
|
.PHONY: build test vet
|
||||||
|
|
||||||
|
.PHONY: plugin.wasm
|
||||||
|
plugin.wasm:
|
||||||
|
GOOS=wasip1 GOARCH=wasm go build -buildmode=c-shared -o plugin.wasm .
|
||||||
|
|
||||||
|
build: plugin.wasm
|
||||||
|
|
||||||
|
vet:
|
||||||
|
GOOS=wasip1 GOARCH=wasm go vet ./...
|
@ -0,0 +1,5 @@
|
|||||||
|
module helm.sh/helm/v4/internal/plugin/src/extismv1-test
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
|
|
||||||
|
require github.com/extism/go-pdk v1.1.3
|
@ -0,0 +1,2 @@
|
|||||||
|
github.com/extism/go-pdk v1.1.3 h1:hfViMPWrqjN6u67cIYRALZTZLk/enSPpNKa+rZ9X2SQ=
|
||||||
|
github.com/extism/go-pdk v1.1.3/go.mod h1:Gz+LIU/YCKnKXhgge8yo5Yu1F/lbv7KtKFkiCSzW/P4=
|
@ -0,0 +1,68 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "embed"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
pdk "github.com/extism/go-pdk"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InputMessageTestV1 struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
type OutputMessageTestV1 struct {
|
||||||
|
Greeting string
|
||||||
|
}
|
||||||
|
|
||||||
|
type ConfigTestV1 struct{}
|
||||||
|
|
||||||
|
func runGetterPluginImpl(input InputMessageTestV1) (*OutputMessageTestV1, error) {
|
||||||
|
name := input.Name
|
||||||
|
|
||||||
|
greeting := fmt.Sprintf("Hello, %s! (%d)", name, len(name))
|
||||||
|
err := os.WriteFile("/tmp/greeting.txt", []byte(greeting), 0o600)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to write temp file: %w", err)
|
||||||
|
}
|
||||||
|
return &OutputMessageTestV1{
|
||||||
|
Greeting: greeting,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func RunGetterPlugin() error {
|
||||||
|
var input InputMessageTestV1
|
||||||
|
if err := pdk.InputJSON(&input); err != nil {
|
||||||
|
return fmt.Errorf("failed to parse input json: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
pdk.Log(pdk.LogDebug, fmt.Sprintf("Received input: %+v", input))
|
||||||
|
output, err := runGetterPluginImpl(input)
|
||||||
|
if err != nil {
|
||||||
|
pdk.Log(pdk.LogError, fmt.Sprintf("failed: %s", err.Error()))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
pdk.Log(pdk.LogDebug, fmt.Sprintf("Sending output: %+v", output))
|
||||||
|
if err := pdk.OutputJSON(output); err != nil {
|
||||||
|
return fmt.Errorf("failed to write output json: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//go:wasmexport helm_plugin_main
|
||||||
|
func HelmPlugin() uint32 {
|
||||||
|
pdk.Log(pdk.LogDebug, "running example-extism-getter plugin")
|
||||||
|
|
||||||
|
if err := RunGetterPlugin(); err != nil {
|
||||||
|
pdk.Log(pdk.LogError, err.Error())
|
||||||
|
pdk.SetError(err)
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {}
|
@ -0,0 +1,9 @@
|
|||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
type: test/v1
|
||||||
|
name: extismv1-test
|
||||||
|
version: 0.1.0
|
||||||
|
runtime: extism/v1
|
||||||
|
runtimeConfig:
|
||||||
|
fileSystem:
|
||||||
|
createTempDir: true
|
Loading…
Reference in new issue