Skip to content

Commit

Permalink
add a wasm krm function implement with wasip1
Browse files Browse the repository at this point in the history
  • Loading branch information
koba1t committed Oct 16, 2024
1 parent bb7a280 commit 2d1edcb
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 0 deletions.
11 changes: 11 additions & 0 deletions kyaml/fn/runtime/runtimeutil/functiontypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,12 @@ type FunctionSpec struct {

// ExecSpec is the spec for running a function as an executable
Exec ExecSpec `json:"exec,omitempty" yaml:"exec,omitempty"`

// Wasip1Spec is the spec for running a function as an wasm with wasm preview 1
Wasip1 Wasip1Spec `json:"wasip1,omitempty" yaml:"exec,omitempty"`
}

// ExecSpec defines a spec for running a function as an executable
type ExecSpec struct {
Path string `json:"path,omitempty" yaml:"path,omitempty"`
}
Expand All @@ -155,6 +159,13 @@ type ContainerSpec struct {
Env []string `json:"envs,omitempty" yaml:"envs,omitempty"`
}

// Wasip1Spec defines a spec for running a function as a wasm with wasm preview 1
type Wasip1Spec struct {
File string `json:"file,omitempty" yaml:"file,omitempty"`

Image string `json:"image,omitempty" yaml:"image,omitempty"`
}

// StorageMount represents a container's mounted storage option(s)
type StorageMount struct {
// Type of mount e.g. bind mount, local volume, etc.
Expand Down
64 changes: 64 additions & 0 deletions kyaml/fn/runtime/wasip1/wasip1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package wasip1

import (
"context"
"fmt"
"io"
"os"
"path/filepath"

"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/yaml"

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)

var _ kio.Filter = &Filter{}

type Filter struct {
runtimeutil.Wasip1Spec `json:",inline" yaml:",inline"`

WorkingDir string `yaml:"workingDir,omitempty"`

runtimeutil.FunctionFilter
}

func (c *Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
c.FunctionFilter.Run = c.Run
return c.FunctionFilter.Filter(nodes) //nolint:wrapcheck
}

func (c *Filter) Run(reader io.Reader, writer io.Writer) error {
// Create a new runtime
ctx := context.Background()
r := wazero.NewRuntime(ctx)
defer r.Close(ctx)

// Import the WASI snapshot preview1 module
wasi_snapshot_preview1.MustInstantiate(ctx, r)

// Load the WASI binary from the filesystem
wasmBytes, err := os.ReadFile(filepath.Join(c.WorkingDir, c.File))
if err != nil {
return fmt.Errorf("failed to read wasi binary(path: '%s'): %w", c.File, err)
}

// Create a new module config with stdin and stdout
config := wazero.NewModuleConfig().WithStdin(reader).WithStdout(writer)

// It will not work unless program name (arg[0]) is set.
module, err := r.InstantiateWithConfig(ctx, wasmBytes, config.WithArgs("krm"))
if err != nil {
return fmt.Errorf("failed to wasip1 instantiate module: %w", err)
}
defer module.Close(ctx)

return nil
}

// NewWasmFilter returns a new wasm filter
func NewWasmFilter(spec runtimeutil.Wasip1Spec) *Filter {
return &Filter{Wasip1Spec: spec}
}
1 change: 1 addition & 0 deletions kyaml/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/sergi/go-diff v1.2.0
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.9.0
github.com/tetratelabs/wazero v1.8.1
github.com/xlab/treeprint v1.2.0
golang.org/x/sys v0.21.0
google.golang.org/protobuf v1.33.0
Expand Down
2 changes: 2 additions & 0 deletions kyaml/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tetratelabs/wazero v1.8.1 h1:NrcgVbWfkWvVc4UtT4LRLDf91PsOzDzefMdwhLfA550=
github.com/tetratelabs/wazero v1.8.1/go.mod h1:yAI0XTsMBhREkM/YDAK/zNou3GoiAce1P6+rp/wQhjs=
github.com/xlab/treeprint v1.2.0 h1:HzHnuAF1plUN2zGlAFHbSQP2qJ0ZAD3XF5XD7OesXRQ=
github.com/xlab/treeprint v1.2.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
Expand Down
17 changes: 17 additions & 0 deletions kyaml/runfn/runfn.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"sigs.k8s.io/kustomize/kyaml/fn/runtime/container"
"sigs.k8s.io/kustomize/kyaml/fn/runtime/exec"
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
"sigs.k8s.io/kustomize/kyaml/fn/runtime/wasip1"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
"sigs.k8s.io/kustomize/kyaml/yaml"
Expand Down Expand Up @@ -462,6 +463,21 @@ func (r *RunFns) ffp(spec runtimeutil.FunctionSpec, api *yaml.RNode, currentUser
"results-%v.yaml", r.resultsCount))
atomic.AddUint32(&r.resultsCount, 1)
}

// if the function is a wasip1 function, run it as a wasip1 function
if spec.Wasip1.Image != "" || spec.Wasip1.File != "" {
wf := wasip1.NewWasmFilter(spec.Wasip1)

wf.WorkingDir = r.WorkingDir

wf.FunctionConfig = api
wf.GlobalScope = r.GlobalScope
wf.ResultsFile = resultsFile
wf.DeferFailure = spec.DeferFailure
return wf, nil
}

// if the function is a container, run it as a container
if !r.DisableContainers && spec.Container.Image != "" {
// TODO: Add a test for this behavior
uidgid, err := getUIDGID(r.AsCurrentUser, currentUser)
Expand Down Expand Up @@ -491,6 +507,7 @@ func (r *RunFns) ffp(spec runtimeutil.FunctionSpec, api *yaml.RNode, currentUser
return cf, nil
}

// if the function is an exec function, run it as an exec function
if r.EnableExec && spec.Exec.Path != "" {
ef := &exec.Filter{
Path: spec.Exec.Path,
Expand Down
1 change: 1 addition & 0 deletions plugin/builtin/prefixtransformer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/tetratelabs/wazero v1.8.1 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
golang.org/x/sys v0.21.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions plugin/builtin/prefixtransformer/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tetratelabs/wazero v1.8.1 h1:NrcgVbWfkWvVc4UtT4LRLDf91PsOzDzefMdwhLfA550=
github.com/tetratelabs/wazero v1.8.1/go.mod h1:yAI0XTsMBhREkM/YDAK/zNou3GoiAce1P6+rp/wQhjs=
github.com/xlab/treeprint v1.2.0 h1:HzHnuAF1plUN2zGlAFHbSQP2qJ0ZAD3XF5XD7OesXRQ=
github.com/xlab/treeprint v1.2.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
Expand Down

0 comments on commit 2d1edcb

Please sign in to comment.